egg-redlock-9
v1.0.2
Published
An easy-to-use egg plugin that implements distributed locks based on redlock
Downloads
3
Maintainers
Readme
egg-redlock-9
The plugin implements distributed locks by reference to redlock and adds two methods, run and once.
中文说明
Install
$ npm i egg-redlock-9 --save
Dependent plugin
Usage
// config/plugin.js
exports.redlock9 = {
enable: true,
package: 'egg-redlock-9',
};
// {app_root}/config/config.default.js
exports.redis = {
/* your redis configuration */
}
exports.redlock9 = {
clients: [ 'redis1', 'redis2', 'redis3' ],
logger: app => e => app.info(e.message), // logger when lock failed, can be false
options: {
driftFactor: 0.01, // time in ms
retryCount: 5,
retryDelay: 200, // time in ms
retryJitter: 50, // time in ms
};
// {app_root}/app/****.js
await app.redlock9.run(resouce, ttl, async (lock) => { /* do something */ }); // run in lock
const success = await app.redlock9.once(resouce, async (lock)=>{ /* do something */ }, wait, duration) // run only once during the duration
//You can also use the api of redlock as app.redlock.unlock(lock)
Api docs for redlock
Configuration
see config/config.default.js for more detail.
Unit tests
Run redis-server in localhost first
npm run test