2pl 中文文档教程
2PL
异步 JavaScript 函数的内存中两阶段锁定。
npm install 2pl
在锁实例的生命周期内可以获取多个锁密钥, 在一次操作中完成释放。
var lock = require('2pl')
var shared = {} // in-memory lock object shared across instances
var l = lock(shared, { ttl: 5000 }) // new instance, 5 seconds timeout
l.acquire('a', function (err) {
// a is locked
l.acquire('b', function (err) {
// b is locked
l.release(function (err) {
// a and b are released
})
})
})
API
var l = lock(shared, [options])
使用持有内存锁的 shared
对象创建一个新实例。 接受以下选项:
ttl
: Time to live (milliseconds) of lock instance for liveness. Defaults to 20 seconds.
l.acquire(key, [callback])
尝试获取锁,给定字符串 key
。 接受一个可选的回调函数,在成功或错误时调用。
l.extend(ttl, [callback])
尝试以毫秒为单位扩展锁定的 ttl
。 接受一个可选的回调函数,在成功或错误时调用。
l.release([callback])
尝试释放锁。 接受一个可选的回调函数,在成功或错误时调用。
var createLock = lock.creator([options])
返回在同一个共享锁对象下创建实例的工厂函数 createLock([options])
。
var createLock = lock.creator({ ttl: 5000 })
var l1 = createLock() // ttl 5000
var l2 = createLock({ ttl: 500 }) // ttl 500
License
麻省理工学院
2PL
In-memory two-phase locking for asynchronous JavaScript functions.
npm install 2pl
Multiple lock keys can be acquired during the lifetime of a lock instance, where release is done in one operation.
var lock = require('2pl')
var shared = {} // in-memory lock object shared across instances
var l = lock(shared, { ttl: 5000 }) // new instance, 5 seconds timeout
l.acquire('a', function (err) {
// a is locked
l.acquire('b', function (err) {
// b is locked
l.release(function (err) {
// a and b are released
})
})
})
API
var l = lock(shared, [options])
Create a new instance with shared
object holding the in-memory locks. Accepts following options:
ttl
: Time to live (milliseconds) of lock instance for liveness. Defaults to 20 seconds.
l.acquire(key, [callback])
Attempts to acquire a lock, given a string key
. Accepts an optional callback function, invoked on success or error.
l.extend(ttl, [callback])
Attempts to extend ttl
of lock in milliseconds. Accepts an optional callback function, invoked on success or error.
l.release([callback])
Attempts to release lock. Accepts an optional callback function, invoked on success or error.
var createLock = lock.creator([options])
Returns a factory function createLock([options])
that create instances under the same shared lock object.
var createLock = lock.creator({ ttl: 5000 })
var l1 = createLock() // ttl 5000
var l2 = createLock({ ttl: 500 }) // ttl 500
License
MIT