node-redlock icon indicating copy to clipboard operation
node-redlock copied to clipboard

Abort scenario still requires waiting for release

Open ikonst opened this issue 4 months ago • 1 comments

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 avatar Oct 04 '24 13:10 ikonst