Skip to content

Integrate timeouts into lock acqusition #2

@CMCDragonkai

Description

@CMCDragonkai

Specification

If we need to be able to "retry" locking due to deadlocks MatrixAI/Polykey#294 (comment).

Then we would need to integrate the withTimeout and tryAcquire decorators from async-mutex. They can be represented with parameters to our acquireRead and acquireWrite. But that would also mean we have to lambda-abstract them.

public read(timeout?: number): ResourceAcquire<RWLockWriter> {
  // by default timeout: undefined
  // but if 0, use tryAcquire
  // if above 0, use withTimeout
  return async () => {
    // same contents as this.acquireRead()
  };
}

Note that when timed out, an exception is thrown. We don't actually retry here. It is up to the user of the lock to attempt to call again, in case they need some random jitter delay.

We can keep the acquireRead method if we want to preserve the API. Or expect users to just do this.read() and this.write() with the relevant timeouts.

Additional context

Tasks

  1. Import withTimeout and tryAcquire from async-mutex
  2. Add in read and write methods that have a timeout parameter
  3. Usage is now like withF([rwLock.read(1000)], async ([lock]) => ...)
  4. Add timeout parameters to withRead and withWrite variants too as the second parameter after f and g for function and generator variants.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions