You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 742cd0d
Browse filesBrowse the repository at this point in the historyBrowse files
feat(publisher, lock, bun)!: align the Redis extension surface and lock options (#2529)
Pre-stable API cleanup for the publisher and lock helpers.
## Publisher
- `BaseRedisPublisher`'s protected `eventPrefix` is now `keyPrefix`,
matching lock and ratelimit.
- `xTrimExactness` is `private` instead of `protected`. It is a test
hook.
- The `subscribe` JSDoc example now awaits the subscription and the
unsubscribe call.
## Lock
- Added `RedisLockerOptions`, `UpstashLockerOptions` and
`experimental_BunRedisLockerOptions`.
- The `lock` middleware accepts an `error` option, like `ratelimit`. The
default is still `CONFLICT`.
- `MemoryLocker` now requires `ttl`, like every other locker.
## Breaking
- Subclasses of `BaseRedisPublisher` must rename `eventPrefix` to
`keyPrefix` and can no longer read `xTrimExactness`.
- `new MemoryLocker()` without `ttl` no longer compiles.
## Notes for review
- The default Redis `prefix` stays `''`. The first commit changed it and
the second reverts that, so squash on merge.
- Requiring `ttl` in `MemoryLocker` is for consistency only. A memory
lock dies with its process, so it could stay optional.
- Not run locally: the Bun test suite and the live Redis/Upstash
integration tests.
-`ttl`: how long the lock is held before it expires automatically, in milliseconds. Guards against holders that never release the lock. Each adapter has its own default.
52
-
-`timeout`: how long to wait for the lock to become available, in milliseconds. `lock` rejects with a `LockTimeoutError` once it elapses.
51
+
-`ttl`: how long the lock is held before it expires automatically, in milliseconds. Guards against holders that never release the lock. Defaults to the adapter's `ttl`.
52
+
-`timeout`: how long to wait for the lock to become available, in milliseconds. `lock` rejects with a `LockTimeoutError` once it elapses. Defaults to the adapter's `timeout`.
53
53
-`signal`: stops waiting early and rejects with the abort reason. Has no effect once the lock is acquired.
Every adapter requires a `ttl`, since the right value depends on how long your work takes.
65
+
:::
66
+
63
67
## Lock Middleware
64
68
65
69
The `lock` helper creates middleware that runs [procedures](/docs/procedure) under a lock, so that calls sharing the same key never run concurrently. When the lock cannot be acquired before the timeout elapses, the procedure rejects with a `CONFLICT` error. The request `signal` is forwarded, so a call stops waiting as soon as the client disconnects.
Use `error` to throw your own error instead of `CONFLICT`. A custom code responds with `400` unless you map it in [`errorStatusMap`](/docs/rpc/handler#custom-error-response), and is [defined](/docs/error-handling#orpcerror-compatibility) when its `code` and `data` match an error the procedure declares.
The lock middleware does not support streaming responses, such as procedures that return an [AsyncIteratorObject](/docs/async-iterator-object) (for example `async function*` handlers) or a [`ReadableStream<Uint8Array>`](/docs/binary-data#readablestreamuint8array). The lock is released once the handler returns the stream, so the work the stream does while it is consumed, such as the body of an `async function*` handler, runs without the lock. Streams sharing a key can then run concurrently. Nested calls made from inside the stream with the same context, `locker`, and `key` also skip locking through deduplication, even though no lock is held.
108
125
:::
@@ -124,8 +141,6 @@ const locker = new MemoryLocker({
124
141
/**
125
142
* How long a lock is held before it expires automatically, in milliseconds.
0 commit comments