node-redlock
node-redlock copied to clipboard
Lock allowing multiple entrants
Hi, finding that redlock is allowing multiple parties into the lock. Running a basic redis:latest
docker image locally.
The snippet below is throwing the Should not be in use
exception every time.
const redlock = new Redlock(
client,
{
// The expected clock drift; for more details see:
// http://redis.io/topics/distlock
driftFactor: 0.01, // multiplied by lock ttl to determine drift time
// The max number of times Redlock will attempt to lock a resource
// before erroring.
retryCount: 10000,
// the time in ms between attempts
retryDelay: 200, // 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
// The minimum remaining time on a lock before an extension is automatically
// attempted with the `using` API.
automaticExtensionThreshold: 500, // time in ms
}
);
const key = Date.now().toString();
let inUse = false;
const operationTimeMs = 10 * 1000;
const op = async (): Promise<unknown> => {
if (inUse) {
throw new Error("Should not be in use");
}
inUse = true;
await sleep(operationTimeMs);
inUse = false;
return;
};
let ops: ReadonlyArray<Promise<unknown>> = [];
for (let i = 0; i < 3; i++) {
// Request half of operation time to ensure extension is requested.
ops = [...ops, redlock.using([key], operationTimeMs/2, op)];
}
await Promise.all(ops);
Is there anything I'm doing wrong?