How to use redlock - 8 common examples

To help you get started, we’ve selected a few redlock examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github GetStream / mongodb-activity-feed / src / index.js View on Github external
_createLock() {
		let redlock = new Redlock(
			// you should have one client for each independent redis node
			// or cluster
			[this.redisConnection],
			{
				// 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: 3,

				// the time in ms between attempts
				retryDelay: 300, // time in ms

				// the max time in ms randomly added to retries
github ConnextProject / indra / modules / node / src / redis / redis.provider.ts View on Github external
useFactory: (redis: Redis.Redis): Redlock => {
    const redlockClient = new Redlock([redis], {
      // 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: 350,

      // 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: 1000, // time in ms
    });
github ConnextProject / indra / modules / redis-lock / src / index.ts View on Github external
constructor(redisUrl: string) {
    const redis = new Redis(redisUrl, {
      retryStrategy: (times: number): number => {
        warn("Lost connection to redis. Retrying to connect...");
        const delay = Math.min(times * 50, 2000);
        return delay;
      },
    });

    this.redlock = new Redlock([redis], {
      // 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: 100,

      // 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: 1000, // time in ms
    });
github u-wave / core / src / plugins / booth.js View on Github external
async onStart() {
    this.locker = new RedLock([this.uw.redis]);

    const current = await this.getCurrentEntry();
    if (current && this.timeout === null) {
      // Restart the advance timer after a server restart, if a track was
      // playing before the server restarted.
      const duration = (current.media.end - current.media.start) * ms('1 second');
      const endTime = Number(current.playedAt) + duration;
      if (endTime > Date.now()) {
        this.timeout = setTimeout(
          () => this.advance(),
          endTime - Date.now(),
        );
      } else {
        this.advance();
      }
    }
github airbnb / bossbat / src / Bossbat.js View on Github external
constructor({ connection, prefix = JOB_PREFIX, ttl = JOB_TTL, tz, disableRedisConfig } = {}) {
    const DB_NUMBER = (connection && connection.db) || 0;

    this.prefix = prefix;
    this.ttl = ttl;
    this.tz = tz;

    this.client = new Redis(connection);
    this.subscriber = new Redis(connection);
    this.redlock = new Redlock([this.client], { retryCount: 0 });

    this.jobs = {};
    this.qas = [];

    if (!disableRedisConfig) {
      this.subscriber.config('SET', 'notify-keyspace-events', 'Ex');
    }

    // Subscribe to expiring keys on the jobs DB:
    this.subscriber.subscribe(`__keyevent@${DB_NUMBER}__:expired`);
    this.subscriber.on('message', (channel, message) => {
      // Check to make sure that the message is a job run request:
      if (!message.startsWith(`${this.prefix}:work:`)) return;

      const jobName = message.startsWith(`${this.prefix}:work:demand:`)
        ? message.replace(`${this.prefix}:work:demand:`, '')
github microsoft / ghcrawler / test / unit / requestTrackerTests.js View on Github external
    locker.unlock = sinon.spy(lock => { throw new redlock.LockError('fail!'); });
    const tracker = createTracker('test', redis, locker);
github davidmerfield / Blot / scripts / blog / unlock-sync.js View on Github external
client.get(resource, function(err, value) {
    var lock = new Redlock.Lock(redlock, resource, value, 1);
  
    console.log('Unlocking', resource, value);
  
    lock.unlock(function(err) {
      if (err) throw err;
      console.log("Unlocked", blog.id);
      sync(blog.id, function(err, folder, done) {
        if (err) throw err;
        done(null, function(err) {
          if (err) throw err;
          console.log('Successfully acquired and released sync for', blog.id);
          process.exit();
        });
      });
    });
  });
github OpenNeuroOrg / openneuro / packages / openneuro-server / libs / redis.js View on Github external
return new Promise(resolve => {
    if (!redis) {
      console.log(
        'Connecting to Redis "redis://%s:%d/0"',
        config.host,
        config.port,
      )
      redis = new Redis(config)
      redlock = new Redlock([redis])
      redis.on('connect', () => {
        resolve(redis)
      })
    } else {
      resolve(redis)
    }
  })
}

redlock

A node.js redlock implementation for distributed redis locks

MIT
Latest version published 3 years ago

Package Health Score

61 / 100
Full package analysis

Popular redlock functions