@sesamecare-oss/redlock
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -56,2 +56,6 @@ /// <reference types="node" /> | ||
}; | ||
export interface RedlockUsingContext { | ||
lock: Lock; | ||
extensions: number; | ||
} | ||
/** | ||
@@ -137,5 +141,5 @@ * A redlock object is instantiated with an array of at least one redis client | ||
*/ | ||
using<T>(resources: string[], duration: number, settings: Partial<Settings>, routine?: (signal: RedlockAbortSignal) => Promise<T>): Promise<T>; | ||
using<T>(resources: string[], duration: number, routine: (signal: RedlockAbortSignal) => Promise<T>): Promise<T>; | ||
using<T>(resources: string[], duration: number, settings: Partial<Settings>, routine?: (signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>): Promise<T>; | ||
using<T>(resources: string[], duration: number, routine: (signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>): Promise<T>; | ||
} | ||
export {}; |
@@ -338,3 +338,3 @@ "use strict"; | ||
function queue() { | ||
timeout = setTimeout(() => (extension = extend()), lock.expiration - Date.now() - settings.automaticExtensionThreshold); | ||
timeout = setTimeout(() => (extension = extend()), context.lock.expiration - Date.now() - settings.automaticExtensionThreshold); | ||
} | ||
@@ -344,3 +344,4 @@ async function extend() { | ||
try { | ||
lock = await lock.extend(duration); | ||
context.lock = await context.lock.extend(duration); | ||
context.extensions += 1; | ||
queue(); | ||
@@ -352,3 +353,3 @@ } | ||
} | ||
if (lock.expiration > Date.now()) { | ||
if (context.lock.expiration > Date.now()) { | ||
return (extension = extend()); | ||
@@ -362,6 +363,9 @@ } | ||
let extension; | ||
let lock = await this.acquire(resources, duration, settings); | ||
const context = { | ||
lock: await this.acquire(resources, duration, settings), | ||
extensions: 0, | ||
}; | ||
queue(); | ||
try { | ||
return await routine(signal); | ||
return await routine(signal, context); | ||
} | ||
@@ -383,3 +387,3 @@ finally { | ||
} | ||
await lock.release(); | ||
await context.lock.release(); | ||
} | ||
@@ -386,0 +390,0 @@ } |
{ | ||
"name": "@sesamecare-oss/redlock", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "A modern node.js redlock implementation for distributed redis locks", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -24,8 +24,2 @@ [![Node build, test and publish](https://github.com/sesamecare/redlock/actions/workflows/nodejs.yml/badge.svg)](https://github.com/sesamecare/redlock/actions/workflows/nodejs.yml) | ||
For Node.js v14 you should also install `node-abort-controller` | ||
```bash | ||
npm install --save node-abort-controller | ||
``` | ||
## Configuration | ||
@@ -39,3 +33,3 @@ | ||
import Client from "ioredis"; | ||
import Redlock from "@sesamecare-oss/redlock"; | ||
import { Redlock } from "@sesamecare-oss/redlock"; | ||
@@ -78,6 +72,6 @@ const redisA = new Client({ host: "a.redis.example.com" }); | ||
The first parameter is an array of resources to lock; the second is the requested lock duration in milliseconds, which MUST NOT contain values after the decimal. | ||
The first parameter is an array of resources to lock; the second is the requested lock duration in milliseconds, which MUST NOT contain values after the decimal. The routine you provide receives the signal and a context object which reflects the current lock (which has other metadata) and a count of the number of extensions that have been acquired. | ||
```ts | ||
await redlock.using([senderId, recipientId], 5000, async (signal) => { | ||
await redlock.using([senderId, recipientId], 5000, async (signal, context) => { | ||
// Do something... | ||
@@ -123,3 +117,3 @@ await something(); | ||
```ts | ||
const { default: Redlock } = require("@sesamecare-oss/redlock"); | ||
const { Redlock } = require("@sesamecare-oss/redlock"); | ||
``` | ||
@@ -126,0 +120,0 @@ |
@@ -116,2 +116,7 @@ import { randomBytes, createHash } from 'crypto'; | ||
export interface RedlockUsingContext { | ||
lock: Lock; | ||
extensions: number; | ||
} | ||
/** | ||
@@ -283,9 +288,3 @@ * A redlock object is instantiated with an array of at least one redis client | ||
return new Lock( | ||
this, | ||
existing.resources, | ||
existing.value, | ||
attempts, | ||
start + duration - drift, | ||
); | ||
return new Lock(this, existing.resources, existing.value, attempts, start + duration - drift); | ||
} | ||
@@ -505,3 +504,3 @@ | ||
settings: Partial<Settings>, | ||
routine?: (signal: RedlockAbortSignal) => Promise<T>, | ||
routine?: (signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>, | ||
): Promise<T>; | ||
@@ -512,3 +511,3 @@ | ||
duration: number, | ||
routine: (signal: RedlockAbortSignal) => Promise<T>, | ||
routine: (signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>, | ||
): Promise<T>; | ||
@@ -519,4 +518,7 @@ | ||
duration: number, | ||
settingsOrRoutine: undefined | Partial<Settings> | ((signal: RedlockAbortSignal) => Promise<T>), | ||
optionalRoutine?: (signal: RedlockAbortSignal) => Promise<T>, | ||
settingsOrRoutine: | ||
| undefined | ||
| Partial<Settings> | ||
| ((signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>), | ||
optionalRoutine?: (signal: RedlockAbortSignal, context: RedlockUsingContext) => Promise<T>, | ||
): Promise<T> { | ||
@@ -549,3 +551,3 @@ if (Math.floor(duration) !== duration) { | ||
// of an abort, the error object will be made available at `signal.error`. | ||
const controller= new AbortController(); | ||
const controller = new AbortController(); | ||
@@ -557,3 +559,3 @@ const signal = controller.signal as RedlockAbortSignal; | ||
() => (extension = extend()), | ||
lock.expiration - Date.now() - settings.automaticExtensionThreshold, | ||
context.lock.expiration - Date.now() - settings.automaticExtensionThreshold, | ||
); | ||
@@ -566,3 +568,4 @@ } | ||
try { | ||
lock = await lock.extend(duration); | ||
context.lock = await context.lock.extend(duration); | ||
context.extensions += 1; | ||
queue(); | ||
@@ -574,3 +577,3 @@ } catch (error) { | ||
if (lock.expiration > Date.now()) { | ||
if (context.lock.expiration > Date.now()) { | ||
return (extension = extend()); | ||
@@ -586,7 +589,10 @@ } | ||
let extension: undefined | Promise<void>; | ||
let lock = await this.acquire(resources, duration, settings); | ||
const context: RedlockUsingContext = { | ||
lock: await this.acquire(resources, duration, settings), | ||
extensions: 0, | ||
}; | ||
queue(); | ||
try { | ||
return await routine(signal); | ||
return await routine(signal, context); | ||
} finally { | ||
@@ -609,5 +615,5 @@ // Clean up the timer. | ||
await lock.release(); | ||
await context.lock.release(); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
114596
1284
177