@orpc/experimental-lock 2.0.0-beta.46 → 2.0.0-beta.48
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -4
- package/dist/adapters/base-redis.d.mts +58 -62
- package/dist/adapters/base-redis.mjs +35 -37
- package/dist/adapters/memory.d.mts +31 -35
- package/dist/adapters/memory.mjs +72 -68
- package/dist/adapters/redis.d.mts +22 -26
- package/dist/adapters/redis.mjs +39 -36
- package/dist/adapters/upstash.d.mts +21 -25
- package/dist/adapters/upstash.mjs +26 -26
- package/dist/error.d.mts +12 -0
- package/dist/error.mjs +10 -0
- package/dist/index.d.mts +3 -81
- package/dist/index.mjs +3 -49
- package/dist/middleware.d.mts +69 -0
- package/dist/middleware.mjs +46 -0
- package/dist/types.d.mts +43 -0
- package/dist/types.mjs +1 -0
- package/package.json +5 -5
- package/dist/adapters/base-redis.d.ts +0 -66
- package/dist/adapters/memory.d.ts +0 -38
- package/dist/adapters/redis.d.ts +0 -28
- package/dist/adapters/upstash.d.ts +0 -27
- package/dist/index.d.ts +0 -81
- package/dist/shared/experimental-lock.CoYbYjCJ.mjs +0 -13
- package/dist/shared/experimental-lock.D9u4cJfq.d.mts +0 -46
- package/dist/shared/experimental-lock.D9u4cJfq.d.ts +0 -46
package/README.md
CHANGED
|
@@ -156,10 +156,11 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
156
156
|
<td align="center"><a href="https://github.com/christ12938?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="christ12938"><img src="https://avatars.githubusercontent.com/u/25758598?v=4" width="167" alt="christ12938"/><br />christ12938</a></td>
|
|
157
157
|
<td align="center"><a href="https://github.com/Ryanjso?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ryan Soderberg"><img src="https://avatars.githubusercontent.com/u/39172778?u=5ed913c31d57e7221b75784abcad48c7ebddde27&v=4" width="167" alt="Ryan Soderberg"/><br />Ryan Soderberg</a></td>
|
|
158
158
|
<td align="center"><a href="https://github.com/itigoore01?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="shota"><img src="https://avatars.githubusercontent.com/u/11831107?u=c976a6dc7e055eb026304c46c99100ed22b0c8e0&v=4" width="167" alt="shota"/><br />shota</a></td>
|
|
159
|
+
<td align="center"><a href="https://github.com/Nic13Gamer?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Nicholas"><img src="https://avatars.githubusercontent.com/u/54724556?u=56a7ab430ce7a80d648ab6eba051d454a818ed0b&v=4" width="167" alt="Nicholas"/><br />Nicholas</a></td>
|
|
159
160
|
<td align="center"><a href="https://github.com/ellis-driscoll?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Ellis Driscoll"><img src="https://avatars.githubusercontent.com/u/70685966?u=c5f95bc33b5991d9744abe00052542e4a2ed3cb9&v=4" width="167" alt="Ellis Driscoll"/><br />Ellis Driscoll</a></td>
|
|
160
|
-
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
161
161
|
</tr>
|
|
162
162
|
<tr>
|
|
163
|
+
<td align="center"><a href="https://github.com/hoangbn?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Hoang Nguyen"><img src="https://avatars.githubusercontent.com/u/38968280?u=c90084c6de65c56facabab7ba13a72a49ddbc3e4&v=4" width="167" alt="Hoang Nguyen"/><br />Hoang Nguyen</a></td>
|
|
163
164
|
<td align="center"><a href="https://opencollective.com/guest-ac41de3b?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Orestis Ioannou"><img src="https://images.opencollective.com/guest-ac41de3b/avatar/460.png" width="167" alt="Orestis Ioannou"/><br />Orestis Ioannou</a></td>
|
|
164
165
|
<td align="center"><a href="https://automatio.ai/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Stefan Smiljkovic"><img src="https://avatars.githubusercontent.com/u/1984909?u=b7bf5bc40ed49df3c22f69d2da7b8d78709c49ed&v=4" width="167" alt="Stefan Smiljkovic"/><br />Stefan Smiljkovic</a></td>
|
|
165
166
|
</tr>
|
|
@@ -190,10 +191,9 @@ Like what we build over at [middleapi](https://github.com/middleapi)? You can he
|
|
|
190
191
|
<td align="center"><a href="https://kellychan.im/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Kelly Peilin Chan"><img src="https://avatars.githubusercontent.com/u/520852?u=6b0f7105f694e7b5cacf410a3f04c7044b469dc8&v=4" width="139" alt="Kelly Peilin Chan"/><br />Kelly Peilin Chan</a></td>
|
|
191
192
|
<td align="center"><a href="https://guyariely.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Guy Ariely"><img src="https://avatars.githubusercontent.com/u/42813496?u=edb6b7f563bf28e160a290832e7da57c0506f8ca&v=4" width="139" alt="Guy Ariely"/><br />Guy Ariely</a></td>
|
|
192
193
|
<td align="center"><a href="https://paulsenon.com/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="PaulSenon"><img src="https://avatars.githubusercontent.com/u/19531087?u=6385741eb91d200b8c513ec6045482e301767ca6&v=4" width="139" alt="PaulSenon"/><br />PaulSenon</a></td>
|
|
193
|
-
<td align="center"><a href="https://
|
|
194
|
+
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
194
195
|
</tr>
|
|
195
196
|
<tr>
|
|
196
|
-
<td align="center"><a href="https://piscis.dev/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Alex"><img src="https://avatars.githubusercontent.com/u/326163?u=b245f368bd940cf51d08c0b6bf55f8257f359437&v=4" width="139" alt="Alex"/><br />Alex</a></td>
|
|
197
197
|
<td align="center"><a href="https://opensource.gubanov.eu/?ref=middleapi&utm_source=middleapi&utm_medium=sponsor" target="_blank" rel="noopener sponsored" title="Andrey Gubanov"><img src="https://avatars.githubusercontent.com/u/1082083?u=c5f2daf7ebece498e85c83367bb37b4e10e2649d&v=4" width="139" alt="Andrey Gubanov"/><br />Andrey Gubanov</a></td>
|
|
198
198
|
</tr>
|
|
199
199
|
</table>
|
|
@@ -206,7 +206,9 @@ With thanks to [40 past sponsors](https://orpc.dev/sponsors#past-sponsors) who h
|
|
|
206
206
|
|
|
207
207
|
<table>
|
|
208
208
|
<tr>
|
|
209
|
-
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude
|
|
209
|
+
<td align="center"><a href="https://claude.com/contact-sales/claude-for-oss" target="_blank" rel="noopener" title="Claude for Open Source"><img src="https://cdn.simpleicons.org/claude" width="64" alt="Claude"/><br />Claude</a></td>
|
|
210
|
+
<td align="center"><a href="https://pullfrog.com/for-oss" target="_blank" rel="noopener" title="Pullfrog for OSS"><img src="https://avatars.githubusercontent.com/in/1768019?s=128&v=4" width="64" alt="Pullfrog"/><br />Pullfrog</a></td>
|
|
211
|
+
<td align="center"><a href="https://macroscope.com/open-source" target="_blank" rel="noopener" title="Macroscope for Open Source"><img src="https://avatars.githubusercontent.com/in/900172?s=128&v=4" width="64" alt="Macroscope"/><br />Macroscope</a></td>
|
|
210
212
|
</tr>
|
|
211
213
|
</table>
|
|
212
214
|
|
|
@@ -1,66 +1,62 @@
|
|
|
1
|
-
import { Promisable } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { Promisable } from "@orpc/shared";
|
|
2
|
+
import type { LockCallbackOptions, Locker, LockOptions } from "../types.mjs";
|
|
4
3
|
/**
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
interface BaseRedisLockerOptions {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
4
|
+
* Options shared by every Redis-backed locker adapter.
|
|
5
|
+
*
|
|
6
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
7
|
+
*/
|
|
8
|
+
export interface BaseRedisLockerOptions {
|
|
9
|
+
/**
|
|
10
|
+
* The prefix to use for Redis keys.
|
|
11
|
+
*
|
|
12
|
+
* @default ''
|
|
13
|
+
*/
|
|
14
|
+
prefix?: string | undefined;
|
|
15
|
+
/**
|
|
16
|
+
* How long a lock is held before it expires automatically, in milliseconds.
|
|
17
|
+
* Guards against holders that never release the lock, such as a crashed process.
|
|
18
|
+
* Can be overridden per call.
|
|
19
|
+
*/
|
|
20
|
+
ttl: number;
|
|
21
|
+
/**
|
|
22
|
+
* How long to wait for a lock to become available, in milliseconds.
|
|
23
|
+
* Can be overridden per call.
|
|
24
|
+
*
|
|
25
|
+
* @default 10000
|
|
26
|
+
*/
|
|
27
|
+
timeout?: number | undefined;
|
|
28
|
+
/**
|
|
29
|
+
* How long to wait between acquisition attempts while the lock
|
|
30
|
+
* is held by someone else, in milliseconds.
|
|
31
|
+
*
|
|
32
|
+
* @default 100
|
|
33
|
+
*/
|
|
34
|
+
retryInterval?: number | undefined;
|
|
36
35
|
}
|
|
37
36
|
/**
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
declare abstract class BaseRedisLocker implements Locker {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
37
|
+
* Base class for Redis-backed locker adapters. It owns the key naming, the lock
|
|
38
|
+
* token, the acquisition loop, and the atomic Lua release, so every adapter built
|
|
39
|
+
* on it can share locks with the others regardless of the Redis client in use.
|
|
40
|
+
*
|
|
41
|
+
* Extend it and implement the abstract methods to support another Redis client.
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
44
|
+
*/
|
|
45
|
+
export declare abstract class BaseRedisLocker implements Locker {
|
|
46
|
+
protected readonly keyPrefix: string;
|
|
47
|
+
protected readonly ttl: number;
|
|
48
|
+
protected readonly timeout: number;
|
|
49
|
+
protected readonly retryInterval: number;
|
|
50
|
+
constructor(options: BaseRedisLockerOptions);
|
|
51
|
+
/**
|
|
52
|
+
* Sets `key` to `token` only when it does not exist yet, with an expiry
|
|
53
|
+
* of `ttl` milliseconds (`SET key token NX PX ttl`), and resolves with
|
|
54
|
+
* whether the key was set.
|
|
55
|
+
*/
|
|
56
|
+
protected abstract acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
57
|
+
/**
|
|
58
|
+
* Runs a Lua script (`EVAL script numkeys key [key ...] arg [arg ...]`).
|
|
59
|
+
*/
|
|
60
|
+
protected abstract evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
61
|
+
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
63
62
|
}
|
|
64
|
-
|
|
65
|
-
export { BaseRedisLocker };
|
|
66
|
-
export type { BaseRedisLockerOptions };
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { sleep, throwIfAborted } from "@orpc/shared";
|
|
2
|
+
import { LockTimeoutError } from "../error.mjs";
|
|
4
3
|
const RELEASE_LOCK_SCRIPT = `
|
|
5
4
|
if redis.call('GET', KEYS[1]) == ARGV[1] then
|
|
6
5
|
return redis.call('DEL', KEYS[1])
|
|
@@ -8,38 +7,37 @@ end
|
|
|
8
7
|
|
|
9
8
|
return 0
|
|
10
9
|
`;
|
|
11
|
-
class BaseRedisLocker {
|
|
12
|
-
keyPrefix;
|
|
13
|
-
ttl;
|
|
14
|
-
timeout;
|
|
15
|
-
retryInterval;
|
|
16
|
-
constructor(options) {
|
|
17
|
-
this.keyPrefix = options.prefix ?? "";
|
|
18
|
-
this.ttl = options.ttl;
|
|
19
|
-
this.timeout = options.timeout ?? 1e4;
|
|
20
|
-
this.retryInterval = options.retryInterval ?? 100;
|
|
21
|
-
}
|
|
22
|
-
async lock(key, fn, options = {}) {
|
|
23
|
-
const prefixedKey = `${this.keyPrefix}${key}`;
|
|
24
|
-
const ttl = options.ttl ?? this.ttl;
|
|
25
|
-
const deadline = Date.now() + (options.timeout ?? this.timeout);
|
|
26
|
-
const token = crypto.randomUUID();
|
|
27
|
-
let waited = false;
|
|
28
|
-
throwIfAborted(options.signal);
|
|
29
|
-
while (!await this.acquire(prefixedKey, token, ttl)) {
|
|
30
|
-
const remaining = deadline - Date.now();
|
|
31
|
-
if (remaining <= 0) {
|
|
32
|
-
throw new LockTimeoutError(key);
|
|
33
|
-
}
|
|
34
|
-
waited = true;
|
|
35
|
-
await sleep(Math.min(this.retryInterval, remaining), { signal: options.signal });
|
|
36
|
-
}
|
|
37
|
-
try {
|
|
38
|
-
return await fn({ waited });
|
|
39
|
-
} finally {
|
|
40
|
-
await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token]);
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
10
|
|
|
45
|
-
export
|
|
11
|
+
export class BaseRedisLocker {
|
|
12
|
+
keyPrefix;
|
|
13
|
+
ttl;
|
|
14
|
+
timeout;
|
|
15
|
+
retryInterval;
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.keyPrefix = options.prefix ?? "";
|
|
18
|
+
this.ttl = options.ttl;
|
|
19
|
+
this.timeout = options.timeout ?? 1e4;
|
|
20
|
+
this.retryInterval = options.retryInterval ?? 100;
|
|
21
|
+
}
|
|
22
|
+
async lock(key, fn, options = {}) {
|
|
23
|
+
const prefixedKey = `${this.keyPrefix}${key}`;
|
|
24
|
+
const ttl = options.ttl ?? this.ttl;
|
|
25
|
+
const deadline = Date.now() + (options.timeout ?? this.timeout);
|
|
26
|
+
const token = crypto.randomUUID();
|
|
27
|
+
let waited = false;
|
|
28
|
+
throwIfAborted(options.signal);
|
|
29
|
+
while (!await this.acquire(prefixedKey, token, ttl)) {
|
|
30
|
+
const remaining = deadline - Date.now();
|
|
31
|
+
if (remaining <= 0) {
|
|
32
|
+
throw new LockTimeoutError(key);
|
|
33
|
+
}
|
|
34
|
+
waited = true;
|
|
35
|
+
await sleep(Math.min(this.retryInterval, remaining), { signal: options.signal });
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
return await fn({ waited });
|
|
39
|
+
} finally {
|
|
40
|
+
await this.evalScript(RELEASE_LOCK_SCRIPT, [prefixedKey], [token]);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -1,38 +1,34 @@
|
|
|
1
|
-
import { Promisable } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
timeout?: number | undefined;
|
|
1
|
+
import type { Promisable } from "@orpc/shared";
|
|
2
|
+
import type { LockCallbackOptions, Locker, LockOptions } from "../types.mjs";
|
|
3
|
+
export interface MemoryLockerOptions {
|
|
4
|
+
/**
|
|
5
|
+
* How long a lock is held before it expires automatically, in milliseconds.
|
|
6
|
+
* Guards against holders that never release the lock.
|
|
7
|
+
* Can be overridden per call.
|
|
8
|
+
*
|
|
9
|
+
* @default undefined (held until released)
|
|
10
|
+
*/
|
|
11
|
+
ttl?: number | undefined;
|
|
12
|
+
/**
|
|
13
|
+
* How long to wait for a lock to become available, in milliseconds.
|
|
14
|
+
* Can be overridden per call.
|
|
15
|
+
*
|
|
16
|
+
* @default 10000
|
|
17
|
+
*/
|
|
18
|
+
timeout?: number | undefined;
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
declare class MemoryLocker implements Locker {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
21
|
+
* Locker adapter backed by in-memory storage, so locks are only shared within
|
|
22
|
+
* the current process. Waiters acquire the lock in order, without polling.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
25
|
+
*/
|
|
26
|
+
export declare class MemoryLocker implements Locker {
|
|
27
|
+
private readonly ttl;
|
|
28
|
+
private readonly timeout;
|
|
29
|
+
private readonly entries;
|
|
30
|
+
constructor(options?: MemoryLockerOptions);
|
|
31
|
+
lock<T>(key: string, fn: (options: LockCallbackOptions) => Promisable<T>, options?: LockOptions): Promise<T>;
|
|
32
|
+
private wait;
|
|
33
|
+
private release;
|
|
35
34
|
}
|
|
36
|
-
|
|
37
|
-
export { MemoryLocker };
|
|
38
|
-
export type { MemoryLockerOptions };
|
package/dist/adapters/memory.mjs
CHANGED
|
@@ -1,70 +1,74 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { promiseWithResolvers, throwIfAborted } from "@orpc/shared";
|
|
2
|
+
import { LockTimeoutError } from "../error.mjs";
|
|
3
3
|
|
|
4
|
-
class MemoryLocker {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
4
|
+
export class MemoryLocker {
|
|
5
|
+
ttl;
|
|
6
|
+
timeout;
|
|
7
|
+
entries = new Map();
|
|
8
|
+
constructor(options = {}) {
|
|
9
|
+
this.ttl = options.ttl;
|
|
10
|
+
this.timeout = options.timeout ?? 1e4;
|
|
11
|
+
}
|
|
12
|
+
async lock(key, fn, options = {}) {
|
|
13
|
+
const ttl = options.ttl ?? this.ttl;
|
|
14
|
+
const timeout = options.timeout ?? this.timeout;
|
|
15
|
+
const token = {};
|
|
16
|
+
throwIfAborted(options.signal);
|
|
17
|
+
let entry = this.entries.get(key);
|
|
18
|
+
const waited = entry !== undefined;
|
|
19
|
+
if (entry) {
|
|
20
|
+
await this.wait(key, entry, token, timeout, options.signal);
|
|
21
|
+
} else {
|
|
22
|
+
entry = {
|
|
23
|
+
holder: token,
|
|
24
|
+
waiters: new Set()
|
|
25
|
+
};
|
|
26
|
+
this.entries.set(key, entry);
|
|
27
|
+
}
|
|
28
|
+
if (ttl !== undefined) {
|
|
29
|
+
entry.expiry = setTimeout(() => this.release(key, token), ttl);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
return await fn({ waited });
|
|
33
|
+
} finally {
|
|
34
|
+
this.release(key, token);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
wait(key, entry, token, timeout, signal) {
|
|
38
|
+
if (timeout <= 0) {
|
|
39
|
+
return Promise.reject(new LockTimeoutError(key));
|
|
40
|
+
}
|
|
41
|
+
const { promise, resolve, reject } = promiseWithResolvers();
|
|
42
|
+
const waiter = {
|
|
43
|
+
token,
|
|
44
|
+
resolve
|
|
45
|
+
};
|
|
46
|
+
entry.waiters.add(waiter);
|
|
47
|
+
const fail = (reason) => {
|
|
48
|
+
entry.waiters.delete(waiter);
|
|
49
|
+
reject(reason);
|
|
50
|
+
};
|
|
51
|
+
const timer = setTimeout(() => fail(new LockTimeoutError(key)), timeout);
|
|
52
|
+
const abortListener = () => fail(signal?.reason);
|
|
53
|
+
signal?.addEventListener("abort", abortListener, { once: true });
|
|
54
|
+
return promise.finally(() => {
|
|
55
|
+
clearTimeout(timer);
|
|
56
|
+
signal?.removeEventListener("abort", abortListener);
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
release(key, token) {
|
|
60
|
+
const entry = this.entries.get(key);
|
|
61
|
+
if (entry?.holder !== token) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
clearTimeout(entry.expiry);
|
|
65
|
+
const [next] = entry.waiters;
|
|
66
|
+
if (!next) {
|
|
67
|
+
this.entries.delete(key);
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
entry.waiters.delete(next);
|
|
71
|
+
entry.holder = next.token;
|
|
72
|
+
next.resolve();
|
|
73
|
+
}
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
export { MemoryLocker };
|
|
@@ -1,28 +1,24 @@
|
|
|
1
|
-
import { RedisClientType } from
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import '../shared/experimental-lock.D9u4cJfq.mjs';
|
|
5
|
-
|
|
1
|
+
import type { RedisClientType } from "redis";
|
|
2
|
+
import type { BaseRedisLockerOptions } from "./base-redis.mjs";
|
|
3
|
+
import { BaseRedisLocker } from "./base-redis.mjs";
|
|
6
4
|
/**
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
declare class RedisLocker extends BaseRedisLocker {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
5
|
+
* Locker adapter for Redis. Acquires locks with `SET NX PX` and releases them
|
|
6
|
+
* atomically with a Lua script, so every instance using the same server shares
|
|
7
|
+
* the same locks.
|
|
8
|
+
*
|
|
9
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
10
|
+
*/
|
|
11
|
+
export declare class RedisLocker extends BaseRedisLocker {
|
|
12
|
+
private readonly redis;
|
|
13
|
+
private readonly options;
|
|
14
|
+
constructor(redis: RedisClientType<any, any, any, any, any>, options: BaseRedisLockerOptions);
|
|
15
|
+
/**
|
|
16
|
+
* Creates a locker with `prefix` appended to this locker's prefix, sharing its Redis client.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link https://orpc.dev/docs/helpers/lock#adapters | Lock Helpers - Adapters}
|
|
19
|
+
*/
|
|
20
|
+
prefix(prefix: string): RedisLocker;
|
|
21
|
+
protected acquire(key: string, token: string, ttl: number): Promise<boolean>;
|
|
22
|
+
protected evalScript(script: string, keys: string[], args: string[]): Promise<unknown>;
|
|
23
|
+
private connectIfNeeded;
|
|
26
24
|
}
|
|
27
|
-
|
|
28
|
-
export { RedisLocker };
|
package/dist/adapters/redis.mjs
CHANGED
|
@@ -1,38 +1,41 @@
|
|
|
1
|
-
import { BaseRedisLocker } from
|
|
2
|
-
import '@orpc/shared';
|
|
3
|
-
import '../shared/experimental-lock.CoYbYjCJ.mjs';
|
|
1
|
+
import { BaseRedisLocker } from "./base-redis.mjs";
|
|
4
2
|
|
|
5
|
-
class RedisLocker extends BaseRedisLocker {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
3
|
+
export class RedisLocker extends BaseRedisLocker {
|
|
4
|
+
redis;
|
|
5
|
+
options;
|
|
6
|
+
constructor(redis, options) {
|
|
7
|
+
super(options);
|
|
8
|
+
this.redis = redis;
|
|
9
|
+
this.options = options;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
prefix(prefix) {
|
|
13
|
+
return new RedisLocker(this.redis, {
|
|
14
|
+
...this.options,
|
|
15
|
+
prefix: `${this.keyPrefix}${prefix}`
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
async acquire(key, token, ttl) {
|
|
19
|
+
await this.connectIfNeeded();
|
|
20
|
+
const result = await this.redis.set(key, token, {
|
|
21
|
+
condition: "NX",
|
|
22
|
+
expiration: {
|
|
23
|
+
type: "PX",
|
|
24
|
+
value: ttl
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
return result === "OK";
|
|
28
|
+
}
|
|
29
|
+
async evalScript(script, keys, args) {
|
|
30
|
+
await this.connectIfNeeded();
|
|
31
|
+
return await this.redis.eval(script, {
|
|
32
|
+
keys,
|
|
33
|
+
arguments: args
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async connectIfNeeded() {
|
|
37
|
+
if (!this.redis.isOpen) {
|
|
38
|
+
await this.redis.connect();
|
|
39
|
+
}
|
|
40
|
+
}
|
|
36
41
|
}
|
|
37
|
-
|
|
38
|
-
export { RedisLocker };
|