Home

Awesome

swim-hashring

Application-level sharding for node.js, similar to ringpop. You can use it to maintain in-memory state between a cluster of nodes, and it allows you to route your requests accordingly.

swim-hashring is a library that implements a distributed sharding system using a gossip membership protocol (swim) and a consistent hash ring based on farmhash.

This library does not assume any particular application protocol.

<a name="install"></a>

Install

npm i swim-hashring -g

<a name="api"></a> ## API

<a name="constructor"></a>

hashring(opts)

Create a new hashring.

Options:

Events:

<a name="lookup"></a>

instance.lookup(key)

Lookup the peer handling a given key, which it can be a String, a Buffer or an integer. The integer needs to be the result of instance.hash(key).

It returns:

{
  id: '192.168.0.1',
  meta: {
    // all metadata specified in
  },
  points: [
    // n integers, where n is the number of replica points
  ]
}

<a name="next"></a>

instance.next(key[, skip])

Lookup the next peer in the hashring for the given key. It is possible to specify a skip list of ids of peers. Because of the skip list, it is possible to implement a circuit breaker to avoid messages that keeps flowing in infinite loops.

It returns:

{
  id: '192.168.0.1',
  meta: {
    // all metadata specified in
  },
  points: [
    // n integers, where n is the number of replica points
  ]
}

<a name="whoami"></a>

instance.whoami()

The id of the current peer. It will throw if the node has not emitted 'up' yet.

<a name="mymeta"></a>

instance.mymeta()

It returns the info of the current node in the same format of lookup().

<a name="allocatedToMe"></a>

instance.allocatedToMe(key)

Similar to lookup(key), but returns true or false depending if the given key has been allocated to this node or not.

<a name="hash"></a>

instance.hash(key)

Hashes the given key using the same hash function used to calculate replica points. It returns an integer.

<a name="peers"></a>

instance.peers(myself)

All the other peers, in the format of lookup. if myself is set to true, the current instance is returned as well.

<a name="close"></a>

instance.close()

Close the instance, detaching it from the gossip network.

License

MIT