1.0.0 • Published 2 years ago

egg-redis-redlock v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

egg-rds-lock

NPM version build status Test coverage David deps Known Vulnerabilities npm download

Install

$ npm i egg-rds-lock --save

Usage

// {app_root}/config/plugin.js
exports.rdsLock = {
  enable: true,
  package: 'egg-rds-lock',
};

Configuration

// {app_root}/config/config.default.js
exports.rdsLock = {
    // DO NOT modify, or invoke a bad affect for the live lock, 
    // @see <https://github.com/mike-marcacci/node-redlock#configuration>
    client:{
        nodes:[
        {
            port: 6379,
            host: '127.0.0.1',
            password: '',
            db: 2,
        }]
    },
    options:{
		// the expected clock drift; for more details
		// see http://redis.io/topics/distlock
		driftFactor: 0.01, // time in ms
		// the max number of times Redlock will attempt
		// to lock a resource before erroring
		retryCount:  5,
		// the time in ms between attempts
		retryDelay:  100, // time in ms
		// the max time in ms randomly added to retries
		// to improve performance under high contention
		// see https://www.awsarchitectureblog.com/2015/03/backoff.html
		retryJitter:  200 // time in ms
    },
    app:true,
    agent:false // not enable on agent process
};

see config/config.default.js for more detail.

Example

const {Controller} = require('egg');
class Ctrl extends Controller {
  async test() {
    const {app,ctx} = this;
    let resource = "lock:test";
    try{
      let lock = await app.rdsLock.lock(resource,20*1000);
      // todo something
      await lock.unlock();
    } catch (e) {
      ctx.body = "unavailable";
      return;
    }
    ctx.body = "available";
  }
}

Questions & Suggestions

Please open an issue here.

License

MIT