1.3.2 • Published 2 years ago

redis-sp v1.3.2

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

redis-sp

Redis SP (SP stands for Synchronization Primitives) is a set of synchronization primitives based on the Redlock algorithm.

Installation

npm install redis-sp

Usage

Mutex

Mutex implementation based on the Redlock algorithm

import RedisClient from 'ioredis';
import { RedisMutex } from 'redis-sp';

async function main() {
  const client = new RedisClient();

  const mutex = new RedisMutex([client], getResourceIdSomehow());
  await mutex.lock();

  try {
    // critical section of your code
    await maybeFails();
  } finally {
    await mutex.unlock();
  }
}

Counting Semaphore

Implementation of a counting semaphore according to the Fair semaphores | Redis with a slight difference (no race conditions due to the atomicity of Redis Lua scripts).

import RedisClient from 'ioredis';
import { RedisCountingSemaphore } from 'redis-sp';

async function main() {
  const client = new RedisClient();

  const semaphore = new RedisCountingSemaphore(
    [client],
    getSharedResourceIdSomehow(),
    getMaxSharedResourceOwnersSomehow(),
  );
  await semaphore.acquire();

  try {
    // critical section of your code
    await maybeFails();
  } finally {
    await semaphore.release();
  }
}

Stay tuned for RW lock in the next major release!

1.3.2

2 years ago

1.2.2

2 years ago

1.1.2

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago