A distributed lock implementation using redis, supporting promise and typescript
def lock(lockId, retry):
value = redis.get(lockId)
if value is null:
redis.set(lockId, True)
else:
if retry < 3:
sleep 100 seconds
lock(lockId, retry++)
else
throw Error
lock(lockId='pid1', retry=0)
import { DistLock } from 'distlock';
import * as redis from 'redis';
async tryToDoSomething(): Promise<void> {
let l: DistLock;
try {
l = new DistLock(redis.redisClient, 'lockName');
await l.lock();
await this.doSomething()
catch (e) {
console.log(e);
} finally {
await l.unlock();
}
}