mike-marcacci / node-redlock

A node.js redlock implementation for distributed, highly-available redis locks
MIT License
1.83k stars 172 forks source link

Abort scenario still requires waiting for release #309

Open ikonst opened 1 week ago

ikonst commented 1 week ago

When the 'aborted' signal is given, am I right to assume there's some certainty about the lock no longer being valid?

Currently Redlock.using will spend time trying to release the lock, eventually failing with "The operation was unable to achieve a quorum during its retry window", e.g.

function blockEventLoop(ms) {
  for (const start = Date.now(); Date.now() - start < ms; ) {}
}

async function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

async function test() {
  console.time("lock");
  try {
    const lock = await redlock.using(
      [id],
      200,
      {
        automaticExtensionThreshold: 100,
      },
      async (abortSignal) => {
        blockEventLoop(200); // prevent auto-extend
        await sleep(200); // allow auto-extend to be attempted
        if (abortSignal.aborted) {
          console.log('aborted');
          throw abortSignal.error;
        }
      },
    );
  } except (err) {
    console.log(err);
  } finally {
    console.timeEnd("lock"); // will print between 2000ms and 3000ms (per jitter)
  }
}

void test();

will print

aborted
The operation was unable to achieve a quorum during its retry window
lock: 2500 ms
ikonst commented 1 week ago

Appears to be addressed in https://github.com/babelcloud/node-redlock/pull/1