All Projects → boltsource → Microlock

boltsource / Microlock

Licence: mit
A dead simple distributed locking library for Node.js and Etcd

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Microlock

Gonet
go分布式服务器,基于内存mmo
Stars: ✭ 804 (+803.37%)
Mutual labels:  etcd
Zetcd
Serve the Apache Zookeeper API but back it with an etcd cluster
Stars: ✭ 1,025 (+1051.69%)
Mutual labels:  etcd
Rook
Storage Orchestration for Kubernetes
Stars: ✭ 9,369 (+10426.97%)
Mutual labels:  etcd
Blog
my blog, using markdown
Stars: ✭ 25 (-71.91%)
Mutual labels:  etcd
Etcd Manage Server
etcd-manage 服务端
Stars: ✭ 30 (-66.29%)
Mutual labels:  etcd
Etcd Cluster Operator
A controller to deploy and manage etcd clusters inside of Kubernetes
Stars: ✭ 63 (-29.21%)
Mutual labels:  etcd
Etcdkeeper
web ui client for etcd
Stars: ✭ 612 (+587.64%)
Mutual labels:  etcd
Casbin
An authorization library that supports access control models like ACL, RBAC, ABAC in Golang
Stars: ✭ 10,872 (+12115.73%)
Mutual labels:  etcd
Dister
dister(Distribution Cluster)是一款轻量级高性能的分布式集群管理软件,实现了分布式软件架构中的常用核心组件,包括:服务配置管理中心、服务注册与发现、服务健康检查、服务负载均衡。dister的灵感来源于ZooKeeper、Consul、Etcd,它们都实现了类似的分布式组件,但是dister更加的轻量级、低成本、易维护、架构清晰、简单实用、性能高效,这也是dister设计的初衷。
Stars: ✭ 41 (-53.93%)
Mutual labels:  etcd
Etcd
Development repository for the etcd cookbook
Stars: ✭ 71 (-20.22%)
Mutual labels:  etcd
Pyetcdlock
a mutux network lock based on etcd
Stars: ✭ 9 (-89.89%)
Mutual labels:  etcd
Example Api
A base API project to bootstrap and prototype quickly.
Stars: ✭ 27 (-69.66%)
Mutual labels:  etcd
Ekka
Autocluster and Autoheal for EMQ X Broker
Stars: ✭ 63 (-29.21%)
Mutual labels:  etcd
Kubeasz
使用Ansible脚本安装K8S集群,介绍组件交互原理,方便直接,不受国内网络环境影响
Stars: ✭ 7,629 (+8471.91%)
Mutual labels:  etcd
Vip Manager
Manages a virtual IP based on state kept in etcd or Consul
Stars: ✭ 75 (-15.73%)
Mutual labels:  etcd
Follow Me Install Kubernetes Cluster
和我一步步部署 kubernetes 集群
Stars: ✭ 6,662 (+7385.39%)
Mutual labels:  etcd
Etcd Play
etcd playground
Stars: ✭ 49 (-44.94%)
Mutual labels:  etcd
Learning Tools
A collection of tools and files for learning new technologies
Stars: ✭ 1,287 (+1346.07%)
Mutual labels:  etcd
Springboot Thrift Etcd Ribbon
基于springboot的thrift的rpc, 服务发现基于etcd,路由基于ribbon
Stars: ✭ 75 (-15.73%)
Mutual labels:  etcd
Etcd Mesos
self-healing etcd on mesos!
Stars: ✭ 68 (-23.6%)
Mutual labels:  etcd

Microlock

A dead simple distributed locking library for Node.js and etcd (via node-etcd)

NPM

CircleCI

What is Etcd?

Etcd is a distributed key-value store, built by the CoreOS team, that provides strong guarantees around consistency and partition tolerance. Data is duplicated to all nodes in a given cluster and remains consistent between node failures. Cluster leaders are elected via the Raft consensus algorithm. Etcd provides operations for atomic value swapping/removal based on criteria and TTL for values, making it a perfect media for distributed locks.

What is a distributed lock?

A distributed lock is a mechanism that provides serialized flow control on a context that is acted on by more than one process. These processes typically operate on different machines via Service Oriented Architecture. Each process uses an object called a distributed lock to "lock" access to the shared context, aliased by a key, so that only one process, each aliased by a node id, can act on it at a time, thereby ensuring consistency and preventing race conditions.

Why not Redlock?

Redis is great for a lot of things. Caching, keeping processes stateless, and fast access to simply structured data are all cases where Redis shines. However, implementing a distributed lock with Redis via Redlock has several caveats that are unsuitable for many cases. Namely, if you need strong guarantees that a lock will not be acquired by multiple nodes at once even in the event of failure, Redlock isn't a viable option.

Notes

Microlock is currently compatible with Etd 2.2.x. Work on support for Etcd 2.2.x - 3.x is in progress.

Install

Requires NodeJS >= 4.0

$ npm install microlock

Basic usage

ES5

var os = require('os');
var Etcd = require('node-etcd');
var Microlock = require('microlock');

var key = 'foo'; //name of the lock
var id = os.hostname(); //id of *this* node
var ttl = 5; //5 second lease on lock

var etcd = new Etcd();
var foo = new Microlock.default(etcd, key, id, ttl);

foo.lock().then(function () {
  // foo is locked by this node

  // do some stuff...

  // release the lock
  return foo.unlock();
}, function (e) {
  if (e instanceof Microlock.AlreadyLockedError) {
    // foo is already locked by a different node
  }
});

ES2015 (with babel)

import { hostname } from 'os';
import Etcd from 'node-etcd';
import Microlock, { AlreadyLockedError } from 'microlock';

const key = 'foo'; //name of the lock
const id = hostname(); //id of *this* node
const ttl = 5; //5 second lease on lock

const etcd = new Etcd();
const foo = new Microlock(etcd, key, id, ttl);

foo.lock().then(() => {
  // foo is locked by this node

  // do some stuff...

  // release the lock
  return foo.unlock();
}, (e) => {
  if (e instanceof AlreadyLockedError) {
    // foo is already locked by a different node
  }
});

ES2016/2017 (with babel)

import { hostname } from 'os';
import Etcd from 'node-etcd';
import Microlock, { AlreadyLockedError } from 'microlock';

async function main () {

  const key = 'foo'; //name of the lock
  const id = hostname(); //id of *this* node
  const ttl = 5; //5 second lease on lock

  const etcd = new Etcd();
  const foo = new Microlock(etcd, key, id, ttl);

  try {
    await foo.lock();
    // foo is locked by this node

    // do some stuff...

    // release the lock
    await foo.unlock();
  } catch (e) {
    if (e instanceof AlreadyLockedError) {
      // foo is already locked by a different node
    }
  }
}

main();

Methods

All methods (except destroy) return promises, making it easy to use features like async/await with ES2016/ES2017 via Babel.

Microlock(etcd, key, node_id, [ttl = 1])

Creates a microlock client for a lock key.

var Etcd = require('node-etcd');
var Microlock = require('microlock');

var etcd = new Etcd();
var foo = new Microlock.default(microlock, 'foo', 'bar');

.lock()

Attempts to lock the key for the node_id.

foo.lock().then(function () {
  // foo is locked by this node
}, function (e) {
  if (e instanceof Microlock.AlreadyLockedError) {
    // foo is already locked by a different node
  }
});

.unlock()

Attempts to release the key for the node_id.

foo.unlock().then(function () {
  // foo is unlocked
}, function (e) {
  if (e instanceof Microlock.LockNotOwnedError) {
    // foo is not locked by `node_id`
  }
})

.renew()

Attempts to renew the lock on key for the node_id.

foo.renew().then(function () {
  // foo lease is renewed... ttl is refreshed
}, function (e) {
  if (e instanceof Microlock.LockNotOwnedError) {
    // foo is not locked by `node_id`
  }
})

.destroy()

Unbinds listeners/watchers from this client

Events

unlock

Emits when the key is unlocked (node agnostic)

foo.on(Microlock.events.unlocked, function () {
  //handle unlocked with constant
});

foo.on('unlocked', function () {
  //handle unlocked with string
});

locked

Emits when the key is locked (node agnostic)

foo.on(Microlock.events.locked, function () {
  //handle locked with constant
});

foo.on('locked', function () {
  //handle locked with string
});

Contributing

Installing packages

  $ make install

Building

  $ make

Linting

  $ make lint

Running unit tests

  $ make unit

Running integration tests

Docker Compose is required

  $ make integration etcd_image_version=v2.2.2

You can use whatever version you'd like to test against in the command above.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].