Home

Awesome

Skiff

Skiff

Raft Consensus Algorithm implementation for Node.js.

npm version Build Status

Installation

$ npm install skiff --save

Usage

const Skiff = require('skiff')

const options = {
  db: require('memdown'), // in memory database
  peers: [ // peer addresses
    '/ip4/127.0.0.1/tcp/9491',
    '/ip4/127.0.0.1/tcp/9492'
  ]
}
const skiff = Skiff('/ip4/127.0.0.1/tcp/9490', options)

// expose the cluster as a Levelup-compatible database
const db = skiff.levelup()

skiff.start(err => {
  if (err) {
    console.error('Error starting skiff node: ', err.message)
  } else {
    console.log('Skiff node started')

    db.put('key', 'value', (err) => {
      // ...
    })
  }
})

API

Skiff (address, options)

Returns a new skiff node.

Arguments:

(You can use this to create a in-memory database using Memdown)

skiff.start (callback)

Starts the node, initializing. Calls back with no argument when started, or with error in the first argument.

skiff.stop (callback)

Stops the node, shutting down server, disconnects from all peers and stops activity. Calls back once all this is done, or when an error is encountered, with an error in the first argument.

skiff.levelup ()

Returns a new Levelup-compatible object for you to interact with the cluster.

skiff.leveldown ()

Returns a new Leveldown-compatible object for you to interact with the cluster.

skiff.join (peerAddress, callback)

Adds a peer to the cluster. Calls back once the cluster reaches consensus, or with an error if no consensus can be reached.

skiff.leave (peerAddress, callback)

Removes a peer from the cluster. Calls back once the cluster reaches consensus, or with an error if no consensus can be reached.

skiff.stats ()

Returns some interesting stats for this node.

skiff.peers (callback)

Invokes the error-first callback function with the cluster peers and some interesting stats from each.

skiff.term ()

Returns the current term (integer).

skiff.weaken (durationMS)

Weakens the node for the duration. During this period, the node transitions to a special weakened state, in which the node does not react to election timeouts. This period ends once it learns a new leader or the period runs out.

skiff.readConsensus(callback)

Asks for read consensus from the cluster. Calls back when there is an error (with the error as the first argument) or succeeded.

Events

A skiff instance emits the following events:

Skiff.createNetwork (options)

Creates a network you can share amongst several Skiff nodes in the same process.

Options:

Sponsors

Development of Skiff is sponsored by YLD.

License

MIT

Copyright

Copyright (c) 2016 Pedro Teixeira