Code Monkey home page Code Monkey logo

etcdjs's Introduction

etcdjs

Low level etcd v2 & v3 client written in Javascript with failover support

npm install etcdjs

Usage

Pass a connection string

var etcdjs = require('etcdjs')
var store = etcdjs('127.0.0.1:4001')

store.set('hello', 'world', function(err, result) {
  store.get('hello', function(err, result) {
    console.log('hello:', result.node.value)
  })
})

If you have more than run instance of etcd running you can pass an array to load balance

var store = etcdjs(['127.0.0.1:4001', '127.0.0.1:4002', '127.0.0.1:4003'])

If you have a discovery token from https://discovery.etcd.io/ you can also pass that

var store = etcdjs('https://discovery.etcd.io/my-token')

etcdjs will automatically refresh its internal host list every 30s so you can transparently add more machines to your cluster without updating your seed host list.

API

store = etcd(host, opts)

host should be a etcd host (or an array of hosts) and opts default to

{
  refresh: false,        // refresh the interval host list automatically
  timeout: 60 * 1000,    // default timeout for ops
  json: false            // stringify/parse all values as JSON
}

Note: The refresh option will try to discover additional etcd hosts via the etcd /machines endpoint which may not always return hostnames which are routable. Make sure the endpoint returns what you expect before turning on this feature.

store.get(key, [opts], cb)

Get a key. opts defaults to

{
  recursive: false,
  sorted: false,
  json: false,      // parse value as JSON for this request
  wait: false,
  waitIndex: (none)
}

store.set(key, value, [opts], [cb])

Set a key. opts defaults to

{
  ttl: (none),
  dir: false,
  json: false,      // stringify value as JSON for this request
  prevExist: (none),
  prevValue: (none),
  prevIndex: (none)
}

store.update(key, value, [opts], [cb])

Set a key if it already exists. Same as set(key, value, {prevExists: true})

store.del(key, [opts], [cb])

Delete a key. opts defaults to

{
  recursive: false,
  dir: false,
  prevExist: (none),
  prevValue: (none),
  prevIndex: (none)
}

store.mkdir(key, [opts], [cb])

Create a directory. Same as set(key, null, {dir: true})

store.rmdir(key, [opts], [cb])

Remove a directory. Same as del(key, {dir: true})

store.wait(key, [opts], [cb])

Wait a key to change. Same as get(key, {wait: true}) except the callback is called with a third argument next that will wait for the next change.

store.wait('hello', function onchange (err, result, next) {
  console.log('change!', result);
  next(onchange); // next will set waitIndex so we do not miss events
});

.wait returns a destroy function which can be used to kill a waiting request.

var destroy = store.wait('hello', function onchange (err, result, next) {
  // ... do stuff ..
})

destroy()
store.set('key', 'value') // won't trigger the wait

store.compareAndSwap(key, value, prevValue, [opts], [cb])

Only set if prevValue matches previous value. Similar to set(key, value, {prevValue: prevValue})

store.compareAndDelete(key, prevValue, [opts], [cb])

Only delete if prevValue matches previous value. Similar to del(key, value, {prevValue: prevValue})

store.push(key, value, [opts], [cb])

Create an in-order key that is guaranteed to be greater than the previous push. Check result.key to see the actual key.

store.machines(cb)

Returns an array of all machines in the cluster

store.leader(cb)

Returns the leader of the cluster

store.destroy()

Destroy the client and all open connections

Stats

store.stats.self([node], cb)

Returns node stats

store.stats.store(cb)

Returns store stats

store.stats.leader(cb)

Returns leader stats

License

MIT

etcdjs's People

Contributors

binocarlos avatar brycebaril avatar defunctzombie avatar flenter avatar joedoyle23 avatar mafintosh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

etcdjs's Issues

abort a '.wait' request

I am working on a service registry module that would use etcd as the backing store. For this module I watch specific keys. One feature I need is the ability to kill/stop waiting on a given key.

I think one way to implement this would be for the .wait method to return a baton object that would have a function kill or stop. Interested in this approach or have a recommendation for a better idea?

Look into a better solution for missing indicies after watch timeout

See #8 for context.

Not sure that a good solution exist beyond what is in the PR. The etcd docs talk about how you are suppose to re-fetch the value after missing indices, but when using the wait api in this module, a re-fetch would need to be implemented as a strait update I suppose.

What I described might also be a non-issue. Needs clarification and possible research.

Automatic fetching of machines is causing requests to break

https://github.com/mafintosh/etcdjs/blob/master/index.js#L206

When the default interval logic to refresh the list of machines kicks in, it causes future requests to etcd to break because they will go to an incorrect machine.

This issue surfaced itself because I am running etcd on a boot2docker vm. To access etcd I use the boot2docker IP address. However, the /v2/machines endpoint for etcd returns http://127.0.0.1:40001 because etcd thinks it is running as localhost only (in the docker container).

I think we should change the default refresh behavior to off and document that by turning refresh on, you need to make sure that your etcd cluster is reporting routable addresses in the v2/machines endpoint.

This issue is tricky to debug since all you get from nodejs is ECONNRESET and don't realize that it is etcdjs that has now changed the hosts array you originally passed in.

Alternatively, if the user specifies a host or list of hosts we may want to default to off and if they do not, then do the refresh logic. I personally think it is best to turn off the refresh logic as it is a silent killer.

etcd Leader API removed?

Running the tests against etcd v2.2.0, I get a failed test for the leader function calling /v2/leader. Investigating it, I don't see that url in the etcd docs. Am I missing something or it just from an older version of etcd? Thanks.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.