@imqueue/core 3.4.2 → 3.5.0
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/AGENTS.md +48 -9
- package/CHANGELOG.md +186 -0
- package/README.md +225 -0
- package/SECURITY.md +28 -0
- package/index.d.ts +10 -2
- package/index.js +10 -2
- package/package.json +5 -4
- package/src/ClusterManager.d.ts +5 -0
- package/src/ClusterManager.js +5 -0
- package/src/ClusteredRedisQueue.d.ts +62 -0
- package/src/ClusteredRedisQueue.js +69 -0
- package/src/IMessageQueue.d.ts +145 -51
- package/src/RedisQueue.d.ts +251 -58
- package/src/RedisQueue.js +412 -98
- package/src/UDPClusterManager.d.ts +35 -0
- package/src/UDPClusterManager.js +35 -0
- package/src/UDPWorker.d.ts +134 -1
- package/src/UDPWorker.js +140 -1
- package/src/helpers/copyEventEmitter.d.ts +21 -0
- package/src/helpers/copyEventEmitter.js +21 -0
- package/src/helpers/envTls.d.ts +31 -0
- package/src/helpers/envTls.js +158 -0
- package/src/helpers/index.d.ts +2 -0
- package/src/helpers/index.js +2 -0
- package/src/helpers/tlsFingerprint.d.ts +15 -0
- package/src/helpers/tlsFingerprint.js +88 -0
- package/src/index.d.ts +2 -0
- package/src/index.js +2 -0
- package/src/redis.d.ts +6 -0
- package/CLAUDE.md +0 -2
package/AGENTS.md
CHANGED
|
@@ -38,7 +38,8 @@ Redis blocking list moves and keyspace notifications.
|
|
|
38
38
|
```bash
|
|
39
39
|
npm install
|
|
40
40
|
npm run build # clean-compiled + tsc (emits alongside sources)
|
|
41
|
-
npm test # build + node:test over
|
|
41
|
+
npm test # build + node:test over test/unit/**/*.spec.js (mocked)
|
|
42
|
+
npm run test-integration # build + test/integration/**/*.spec.js (real redis)
|
|
42
43
|
npm run lint # oxlint
|
|
43
44
|
npm run format # oxfmt (write) | npm run format:check (verify)
|
|
44
45
|
npm run test-coverage # tests + experimental coverage summary
|
|
@@ -46,10 +47,36 @@ npm run test-lcov # writes coverage/lcov.info
|
|
|
46
47
|
npm run benchmark # message-throughput benchmark (see below)
|
|
47
48
|
```
|
|
48
49
|
|
|
49
|
-
|
|
50
|
-
`--experimental-test-module-mocks` and preload `./test/mocks/index.js`;
|
|
50
|
+
Unit specs live in `test/unit/` and use the native `node:test` runner with
|
|
51
|
+
`--experimental-test-module-mocks` and a preload of `./test/mocks/index.js`;
|
|
51
52
|
timeout is 15s per test. Run a single spec after a build with
|
|
52
|
-
`node --experimental-test-module-mocks --import ./test/mocks/index.js --test test/
|
|
53
|
+
`node --experimental-test-module-mocks --import ./test/mocks/index.js --test test/unit/RedisQueue.spec.js`.
|
|
54
|
+
|
|
55
|
+
`npm test` deliberately globs `test/unit` rather than `test`: the integration
|
|
56
|
+
specs must not run under a preload that replaces `ioredis` wholesale. Keep that
|
|
57
|
+
glob scoped when adding runners.
|
|
58
|
+
|
|
59
|
+
**The unit specs never open a socket** — `test/mocks/redis.ts` replaces
|
|
60
|
+
`ioredis` wholesale. That is fine for everything except the TLS option, whose
|
|
61
|
+
entire point is what happens during a handshake the mock does not perform.
|
|
62
|
+
`test/integration/` covers that against a real broker, and anything touching
|
|
63
|
+
`tls`, the connection literal in `RedisQueue.connect()`, the pool key or
|
|
64
|
+
connection teardown must be checked with `npm run test-integration` as well as
|
|
65
|
+
`npm test`.
|
|
66
|
+
|
|
67
|
+
`test/integration/tlsBroker.ts` issues a throwaway CA, server and client
|
|
68
|
+
certificate with `openssl`, then starts `redis-server` on a port picked at run
|
|
69
|
+
time with `--port 0 --tls-port <n>`. `--port 0` is the part that matters: with
|
|
70
|
+
no plaintext listener at all, a queue that reaches the broker has demonstrably
|
|
71
|
+
done so over TLS. It is started twice, once with `--tls-auth-clients no` and
|
|
72
|
+
once with `yes`, so both server-authenticated and mutual TLS are exercised.
|
|
73
|
+
|
|
74
|
+
**The integration specs skip, never fail, when the machine cannot host a
|
|
75
|
+
broker.** `startTlsBroker()` returns a reason string instead of throwing when
|
|
76
|
+
`redis-server` or `openssl` is missing, or when redis will not start with TLS
|
|
77
|
+
enabled, and that reason becomes the suite's `skip`. CI has no redis and must
|
|
78
|
+
stay green, so keep that contract: report a skip reason, do not throw, and do
|
|
79
|
+
not add these specs to a runner that CI invokes.
|
|
53
80
|
|
|
54
81
|
## Layout
|
|
55
82
|
|
|
@@ -71,12 +98,24 @@ timeout is 15s per test. Run a single spec after a build with
|
|
|
71
98
|
- **Two delivery modes**, selected by the `safeDelivery` option: *unreliable*
|
|
72
99
|
(fast; a message is lost if a consumer grabs it and dies) and *safe/guaranteed*
|
|
73
100
|
(1.5–2× slower; a grabbed-then-lost message is rescheduled). Safe delivery
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
101
|
+
covers the **processing**, not only the hand-off: the worker key is held until
|
|
102
|
+
the `message` listener is done, and **a listener says so by what it
|
|
103
|
+
returns** — a returned promise keeps the message checked out until it
|
|
104
|
+
settles, anything else releases it as the listener returns. Listeners are
|
|
105
|
+
therefore invoked through `rawListeners()` rather than `emit()`, which
|
|
106
|
+
discards return values. Safe delivery relies on Redis `LMOVE`/`BLMOVE` —
|
|
107
|
+
**Redis 6.2+ is required.** Unreliable delivery uses `BRPOP` alone and works
|
|
108
|
+
on 3.2+, so quote 6.2+ as the requirement unless the sentence is specifically
|
|
109
|
+
about the unreliable mode.
|
|
77
110
|
- **No polling / no timers.** Delivery uses blocking Redis ops + keyspace
|
|
78
|
-
notification events.
|
|
79
|
-
|
|
111
|
+
notification events. This is why a lease is reclaimed by asking the broker who
|
|
112
|
+
is still connected (`CLIENT LIST`, already read once per maintenance tick for
|
|
113
|
+
the cleanup pass) rather than by renewing a deadline: a per-message heartbeat
|
|
114
|
+
would be a timer, and would prove only that the event loop is free rather than
|
|
115
|
+
that work is progressing. `safeDeliveryTtl` bounds processing on top of that,
|
|
116
|
+
for the handler that wedges inside a process which is otherwise healthy. If
|
|
117
|
+
Redis has the `CONFIG` command disabled (e.g. AWS ElastiCache), keyspace
|
|
118
|
+
notifications must be enabled out of band:
|
|
80
119
|
`notify-keyspace-events Ex`.
|
|
81
120
|
- **Keyspace-notification flags are merged, never overwritten.**
|
|
82
121
|
`notify-keyspace-events` is server-global, so `watch()` reads the current
|
package/CHANGELOG.md
CHANGED
|
@@ -9,6 +9,192 @@ queue does has an entry.
|
|
|
9
9
|
|
|
10
10
|
This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
11
11
|
|
|
12
|
+
## [Unreleased]
|
|
13
|
+
|
|
14
|
+
### Added
|
|
15
|
+
|
|
16
|
+
- **TLS on connections to the redis broker.** A new `tls` option encrypts every
|
|
17
|
+
connection a queue opens — reader, writer, watcher and subscription alike.
|
|
18
|
+
Pass `true` for Node's defaults, or an object handed to `tls.connect()` as
|
|
19
|
+
given, so a private CA (`ca`) and mutual TLS (`cert`/`key`) both work. The
|
|
20
|
+
option was previously accepted by the type system and dropped on the way to
|
|
21
|
+
the client, which meant the bus could not be encrypted at all.
|
|
22
|
+
|
|
23
|
+
Cluster entries may carry their own `tls`, overriding the cluster-wide one
|
|
24
|
+
for that server alone; an entry that omits it falls back to the top level.
|
|
25
|
+
Per-entry `username` and `password` are still ignored, exactly as before —
|
|
26
|
+
honouring them would change what an existing cluster authenticates with, and
|
|
27
|
+
that has nothing to do with this feature.
|
|
28
|
+
|
|
29
|
+
With `tls` left unset the environment is consulted — `IMQ_REDIS_TLS`,
|
|
30
|
+
`IMQ_REDIS_TLS_CA_FILE`, `IMQ_REDIS_TLS_CERT_FILE`, `IMQ_REDIS_TLS_KEY_FILE`,
|
|
31
|
+
`IMQ_REDIS_TLS_KEY_PASSPHRASE`, `IMQ_REDIS_TLS_SERVERNAME` and
|
|
32
|
+
`IMQ_REDIS_TLS_REJECT_UNAUTHORIZED` — so a deployment can encrypt a fleet
|
|
33
|
+
without a code change. Certificate files are read as the queue is
|
|
34
|
+
constructed, and an unreadable one throws: an unmounted secret stops the
|
|
35
|
+
process rather than leaving it talking to the broker in the clear. Passing
|
|
36
|
+
`tls` explicitly always wins, `tls: false` included, and
|
|
37
|
+
`rejectUnauthorized: false` is warned about because it leaves a connection
|
|
38
|
+
encrypted but unauthenticated.
|
|
39
|
+
|
|
40
|
+
This covers the queue's own connections. `UDPClusterManager` announcements
|
|
41
|
+
remain unauthenticated UDP broadcast and are unaffected.
|
|
42
|
+
|
|
43
|
+
- **Integration specs covering TLS against a real broker**, in
|
|
44
|
+
`test/integration/`, run by `npm run test-integration`. They stand up a
|
|
45
|
+
throwaway TLS-only redis and assert what a mocked `ioredis` cannot: that the
|
|
46
|
+
handshake completes and is verified, that a message crosses it, that the
|
|
47
|
+
connection pool keeps differing TLS configurations apart, and that plaintext,
|
|
48
|
+
an unverifiable certificate and a wrong server name are all refused rather
|
|
49
|
+
than downgraded. They skip themselves, with a reason, wherever
|
|
50
|
+
`redis-server` and `openssl` are not both available, so a checkout without
|
|
51
|
+
redis still passes. `npm test` now globs `test/unit` and does not run them.
|
|
52
|
+
|
|
53
|
+
### Changed
|
|
54
|
+
|
|
55
|
+
- **Nothing changes for a queue that does not use TLS.** The option is absent
|
|
56
|
+
from `options` unless it was configured, the connection pool key stays the
|
|
57
|
+
plain `host:port`, the redis client is handed no `tls` option, and no new
|
|
58
|
+
warning is emitted. The only added work on that path is one environment
|
|
59
|
+
lookup per queue construction, about a microsecond, and nothing at all per
|
|
60
|
+
message. This is covered by its own group of specs rather than left as a
|
|
61
|
+
claim.
|
|
62
|
+
|
|
63
|
+
- **A connection failure no longer takes the process down during teardown.**
|
|
64
|
+
This is a behaviour change for everyone, not only TLS users: a socket that
|
|
65
|
+
reported a second failure after the one that closed it used to reach an
|
|
66
|
+
`error` with no listener and terminate the process. Such a process now stays
|
|
67
|
+
up, and the failure is still logged and emitted as it always was. Nothing
|
|
68
|
+
that previously succeeded behaves differently; a crash becomes a log line.
|
|
69
|
+
|
|
70
|
+
- **Shared writer and watcher connections are now keyed by TLS configuration as
|
|
71
|
+
well as by `host:port`.** These connections are shared per server within a
|
|
72
|
+
process; queues reaching one server under different TLS configurations now get
|
|
73
|
+
separate connections, so a queue that asked for encryption can never be handed
|
|
74
|
+
a plaintext socket another queue opened first, nor the reverse. Configurations
|
|
75
|
+
equal by value still share, and the public `redisKey` still reports the plain
|
|
76
|
+
`host:port` address.
|
|
77
|
+
|
|
78
|
+
- **`safeDeliveryTtl` is now the longest a message may be worked on**, and its
|
|
79
|
+
default moves from `5000` to `300000`. It used to be a hand-off recovery
|
|
80
|
+
deadline, which meant it bounded nothing that mattered: the key was deleted at
|
|
81
|
+
dispatch, so it never covered processing at all. It now bounds processing,
|
|
82
|
+
which is the only thing that recovers a message from a worker that is alive,
|
|
83
|
+
connected and serving other messages while one handler has wedged on this one.
|
|
84
|
+
Liveness cannot see that case, and restarting an otherwise healthy process is
|
|
85
|
+
not a recovery strategy.
|
|
86
|
+
|
|
87
|
+
Set it to the longest a handler in this system can legitimately take, with
|
|
88
|
+
headroom — a slow upstream is the usual reason for a large value, such as a
|
|
89
|
+
data vendor with no job API or screen-scraping behind an HTTP call. Too low
|
|
90
|
+
and a message is reclaimed from a worker still legitimately working on it.
|
|
91
|
+
|
|
92
|
+
The default rose because the meaning changed: 5000 was a sane hand-off
|
|
93
|
+
deadline and is a poor processing budget.
|
|
94
|
+
|
|
95
|
+
- **The maintenance sweep now runs on `watcherCheckDelay`** (5000 ms by
|
|
96
|
+
default) rather than on `safeDeliveryTtl`. How long a message may be worked on
|
|
97
|
+
and how often the watcher looks for abandoned ones are different questions,
|
|
98
|
+
and tying them would make a crashed worker's message wait out a budget meant
|
|
99
|
+
for a live one. `watcherCheckDelay` is therefore the worst-case latency for a
|
|
100
|
+
crashed worker's message coming back; with the watcher check disabled the
|
|
101
|
+
sweep falls back to `safeDeliveryTtl`.
|
|
102
|
+
|
|
103
|
+
- **The reader's blocking pop is half `safeDeliveryTtl` capped at 5000 ms.** A
|
|
104
|
+
lease deadline is stamped before the pop that fills the key, so an uncapped
|
|
105
|
+
wait would hand a message a sizeable part of its budget already spent. At the
|
|
106
|
+
old defaults both of these resolve to exactly what they were.
|
|
107
|
+
|
|
108
|
+
- The safe-delivery changes above alter meanings and defaults only —
|
|
109
|
+
`safeDelivery`, `safeDeliveryTtl` and `watcherCheckDelay` are the same three
|
|
110
|
+
options they were.
|
|
111
|
+
|
|
112
|
+
### Fixed
|
|
113
|
+
|
|
114
|
+
- **A failed connection could crash the process as it was being torn down.**
|
|
115
|
+
The redis client guards its socket with a one-shot `error` listener, which
|
|
116
|
+
the failure that brings the connection down spends. A socket that goes on to
|
|
117
|
+
report a second failure — a rejected TLS handshake is answered by an alert
|
|
118
|
+
that arrives after the rejection itself — then reached a socket with nothing
|
|
119
|
+
attached, and the unhandled `error` took the process with it. The queue now
|
|
120
|
+
installs a durable listener on the socket when a connection first fails and
|
|
121
|
+
again before teardown, and `destroyChannel` no longer strips the client's
|
|
122
|
+
own `error` handler before writing its `QUIT`.
|
|
123
|
+
|
|
124
|
+
Reachable without TLS in principle, but TLS is what makes it ordinary: a
|
|
125
|
+
service started against a broker it cannot verify would reject `start()` and
|
|
126
|
+
then die inside `destroy()` rather than reporting the misconfiguration.
|
|
127
|
+
|
|
128
|
+
- **`safeDelivery` lost a message when a worker died mid-handler.** It released
|
|
129
|
+
the message's worker key the instant the message was dispatched to the
|
|
130
|
+
`message` listener, so a worker killed while its handler was still running
|
|
131
|
+
took that message with it and nothing brought it back — the option guaranteed
|
|
132
|
+
the hand-off while its name and documentation promised guaranteed delivery.
|
|
133
|
+
The key is now held until the listener has finished with the message.
|
|
134
|
+
|
|
135
|
+
**A listener says it has finished by what it returns.** Return a promise and
|
|
136
|
+
the message stays checked out until it settles; return anything else and it is
|
|
137
|
+
released as the listener returns. Every registered listener is consulted, and
|
|
138
|
+
the message is released once all the promises they returned have settled —
|
|
139
|
+
settled, not fulfilled, because a handler that threw has still had its turn
|
|
140
|
+
and re-delivering on a rejection would retry a poison message forever.
|
|
141
|
+
|
|
142
|
+
Nothing needs changing to get this: a listener that already returns a promise
|
|
143
|
+
— as `@imqueue/rpc` and `@imqueue/job` both do — is covered as it stands, and
|
|
144
|
+
a synchronous listener behaves exactly as it did.
|
|
145
|
+
|
|
146
|
+
A message that cannot be unpacked releases its lease rather than coming back
|
|
147
|
+
on every sweep.
|
|
148
|
+
|
|
149
|
+
- **A dead worker's message is recovered by asking the broker, not a clock.**
|
|
150
|
+
The worker key now carries the identity of the process that took it, and the
|
|
151
|
+
watcher reclaims it once that process has left `CLIENT LIST` — detected as
|
|
152
|
+
fast as the socket closes, and needing nothing renewed to keep a live lease
|
|
153
|
+
alive. Reconnecting workers get one sweep of grace, mirroring what the cleanup
|
|
154
|
+
pass already gave them, so a reconnect backoff does not cost a duplicate. If
|
|
155
|
+
the client list cannot be read at all, liveness is unknown and a lease is left
|
|
156
|
+
to its budget rather than guessed at.
|
|
157
|
+
|
|
158
|
+
### Keeping the old behaviour
|
|
159
|
+
|
|
160
|
+
Release-at-dispatch is still available per listener, and exactly: **do not
|
|
161
|
+
return the handler's promise.**
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
// held until the handler finishes — the fix
|
|
165
|
+
queue.on('message', async message => { await handle(message); });
|
|
166
|
+
|
|
167
|
+
// released at dispatch, bit-for-bit the old behaviour
|
|
168
|
+
queue.on('message', message => { void handle(message); });
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Note that `safeDeliveryTtl` is not the lever for this. It never bounded a
|
|
172
|
+
dispatched message's lifetime before — the key was already deleted — and it does
|
|
173
|
+
not bound one now, so no value of it reproduces the old behaviour.
|
|
174
|
+
|
|
175
|
+
### Notes for upgrading
|
|
176
|
+
|
|
177
|
+
- A synchronous listener needs no change.
|
|
178
|
+
- A listener returning a promise now holds its message for the life of that
|
|
179
|
+
promise, bounded by `safeDeliveryTtl`. Check that budget is above your slowest
|
|
180
|
+
handler, since the default of five minutes is a guess about your workload and
|
|
181
|
+
nothing else.
|
|
182
|
+
- Messages that used to vanish when a worker died now come back, so a handler
|
|
183
|
+
that was quietly getting away with not being idempotent will start seeing
|
|
184
|
+
repeats. Delivery was documented as at-least-once throughout; this makes the
|
|
185
|
+
implementation match.
|
|
186
|
+
- Worker keys written by 3.x carry no owner and are still honoured by their
|
|
187
|
+
deadline, so a rolling upgrade does not strand them. New keys put the owner
|
|
188
|
+
*before* the deadline, so a 3.x watcher still reads the queue off the front
|
|
189
|
+
and a number off the end rather than parsing garbage. While a 3.x watcher is
|
|
190
|
+
the elected one it will still reclaim 4.x leases on that deadline, so expect
|
|
191
|
+
duplicates for the length of the upgrade and finish it promptly.
|
|
192
|
+
- No new redis traffic and no new timers: the maintenance tick already read
|
|
193
|
+
`CLIENT LIST` for the cleanup pass and lease recovery shares that one read.
|
|
194
|
+
Nothing is renewed, polled or scheduled per message — a heartbeat would prove
|
|
195
|
+
only that the event loop is free, not that work is progressing, and so would
|
|
196
|
+
reclaim exactly when a worker is busiest.
|
|
197
|
+
|
|
12
198
|
## [3.4.0] - 2026-08-20
|
|
13
199
|
|
|
14
200
|
### Added
|
package/README.md
CHANGED
|
@@ -38,6 +38,8 @@ With current implementation on RedisQueue:
|
|
|
38
38
|
no delays in delivery and low CPU usage on application workers. When idle,
|
|
39
39
|
it consumes no resources.
|
|
40
40
|
- **Supports Gzip compression for messages** (decreases traffic usage but is slower).
|
|
41
|
+
- **TLS on the wire**, including mutual TLS, on every connection the queue
|
|
42
|
+
opens — and switchable from the environment, without a code change.
|
|
41
43
|
- **Concurrent workers model supported**, the same queue can have multiple
|
|
42
44
|
consumers.
|
|
43
45
|
- **Delayed messages supported**, fast as ~10K of 1Kb messages per second on i7
|
|
@@ -113,6 +115,229 @@ import IMQ, { IMessageQueue, IJson } from '@imqueue/core';
|
|
|
113
115
|
})();
|
|
114
116
|
```
|
|
115
117
|
|
|
118
|
+
# Guaranteed delivery
|
|
119
|
+
|
|
120
|
+
`safeDelivery` moves a message atomically out of the queue into a worker-owned
|
|
121
|
+
key as it is popped, and keeps it there until the message has been handled. A
|
|
122
|
+
worker that dies at any point before then — before it starts, or halfway through
|
|
123
|
+
the handler — leaves the message behind to be re-queued rather than taking it
|
|
124
|
+
down.
|
|
125
|
+
|
|
126
|
+
**How a listener says it has finished is its return value.** Return a promise and
|
|
127
|
+
the message stays checked out until it settles:
|
|
128
|
+
|
|
129
|
+
```typescript
|
|
130
|
+
const queue = IMQ.create('Orders', { safeDelivery: true });
|
|
131
|
+
|
|
132
|
+
queue.on('message', async (message, id, fromQueue) => {
|
|
133
|
+
await handle(message); // the message is checked out for all of this
|
|
134
|
+
});
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Return anything else and the message is released as the listener returns. Every
|
|
138
|
+
registered listener is consulted, and the message is released once all the
|
|
139
|
+
promises they returned have settled — settled, not fulfilled: a handler that
|
|
140
|
+
throws has still had its turn, and re-delivering on a rejection would retry a
|
|
141
|
+
poison message forever.
|
|
142
|
+
|
|
143
|
+
That return value is also the lever for opting out. To have a message released
|
|
144
|
+
at dispatch, as releases before 4.0 did, simply do not return its promise:
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
queue.on('message', message => {
|
|
148
|
+
void handle(message); // released immediately; a crash loses it
|
|
149
|
+
});
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
`safeDeliveryTtl` is not that lever and never was — the key used to be deleted
|
|
153
|
+
at dispatch, so no value of it bounded a dispatched message's lifetime then, and
|
|
154
|
+
none bounds one now.
|
|
155
|
+
|
|
156
|
+
| option | default | meaning |
|
|
157
|
+
|---|---|---|
|
|
158
|
+
| `safeDelivery` | `false` | move messages through a worker-owned key, held until handled |
|
|
159
|
+
| `safeDeliveryTtl` | `300000` | the longest a message may be worked on |
|
|
160
|
+
|
|
161
|
+
A message comes back on either of two counts, because there are two ways to lose
|
|
162
|
+
one.
|
|
163
|
+
|
|
164
|
+
The **process dying** is caught by the broker: the worker key names the process
|
|
165
|
+
that took it, and the watcher reclaims the lease once that process leaves
|
|
166
|
+
`CLIENT LIST`. That is a fact the broker holds rather than an inference from a
|
|
167
|
+
clock, so it is noticed as fast as the socket closes, and nothing has to be
|
|
168
|
+
renewed to keep a live lease alive. Workers riding out a reconnect get one sweep
|
|
169
|
+
of grace, and if the client list cannot be read at all, leases are left to their
|
|
170
|
+
budget rather than guessed at.
|
|
171
|
+
|
|
172
|
+
**One handler wedging** is caught by `safeDeliveryTtl`, and only by it. A worker
|
|
173
|
+
can be up, connected and happily serving other messages while one handler is
|
|
174
|
+
stuck forever — liveness cannot see that, and restarting an otherwise healthy
|
|
175
|
+
process is not a recovery strategy. Set the budget to the longest a handler in
|
|
176
|
+
your system can legitimately take, with headroom: a slow upstream is the usual
|
|
177
|
+
reason for a large value — a data vendor with no job API, screen-scraping behind
|
|
178
|
+
an HTTP call, anything that can run for minutes. Set it too low and a message is
|
|
179
|
+
reclaimed from a worker still legitimately working on it.
|
|
180
|
+
|
|
181
|
+
Two internals deliberately do not scale with this value. The maintenance sweep
|
|
182
|
+
runs on `watcherCheckDelay` (5 s by default), so a dead worker is still found
|
|
183
|
+
within seconds however generous the budget — that, not `safeDeliveryTtl`, is the
|
|
184
|
+
latency for a crashed worker's message coming back. And the reader's blocking
|
|
185
|
+
pop is half the budget capped at 5 s, so a message is not born having already
|
|
186
|
+
spent much of it.
|
|
187
|
+
|
|
188
|
+
Delivery is **at-least-once** in every mode. Holding the lease narrows the window
|
|
189
|
+
in which in-flight work is lost — it does not close it, and `SIGKILL` on the
|
|
190
|
+
whole node, an OOM kill or a lost machine still take work with them — so handlers
|
|
191
|
+
must be idempotent.
|
|
192
|
+
|
|
193
|
+
# Transport encryption (TLS)
|
|
194
|
+
|
|
195
|
+
Set `tls` and every connection the queue opens — reader, writer, watcher and
|
|
196
|
+
subscription alike — is encrypted. `true` connects with Node's defaults,
|
|
197
|
+
verifying the broker against the system trust store; an object is handed to
|
|
198
|
+
`tls.connect()` as given:
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
import { readFileSync } from 'node:fs';
|
|
202
|
+
|
|
203
|
+
const queue = IMQ.create('Orders', {
|
|
204
|
+
host: 'redis.internal',
|
|
205
|
+
port: 6380,
|
|
206
|
+
password: process.env.REDIS_PASSWORD,
|
|
207
|
+
tls: {
|
|
208
|
+
ca: readFileSync('/etc/redis-tls/ca.crt'), // a private CA
|
|
209
|
+
cert: readFileSync('/etc/redis-tls/client.crt'), // mutual TLS,
|
|
210
|
+
key: readFileSync('/etc/redis-tls/client.key'), // if the broker
|
|
211
|
+
}, // asks for it
|
|
212
|
+
});
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
The broker has to be listening for TLS — `tls-port 6380` and a server
|
|
216
|
+
certificate, plus `tls-auth-clients yes` if you are using client certificates.
|
|
217
|
+
A server that is not will refuse the handshake, and the queue reports the
|
|
218
|
+
failure rather than falling back to plaintext: there is no downgrade path.
|
|
219
|
+
|
|
220
|
+
| option | default | meaning |
|
|
221
|
+
|---|---|---|
|
|
222
|
+
| `tls` | `undefined` | `true` for Node's defaults, an object for `tls.connect()`, `false` to force plaintext |
|
|
223
|
+
|
|
224
|
+
## Turning it on without a code change
|
|
225
|
+
|
|
226
|
+
With `tls` left unset, the environment is consulted, so a deployment can encrypt
|
|
227
|
+
a fleet it does not want to edit:
|
|
228
|
+
|
|
229
|
+
| variable | meaning |
|
|
230
|
+
|---|---|
|
|
231
|
+
| `IMQ_REDIS_TLS` | `1` enables TLS with default verification; `0` forces plaintext and wins over everything below |
|
|
232
|
+
| `IMQ_REDIS_TLS_CA_FILE` | PEM bundle to verify the broker against — implies `IMQ_REDIS_TLS=1` |
|
|
233
|
+
| `IMQ_REDIS_TLS_CERT_FILE`, `IMQ_REDIS_TLS_KEY_FILE` | client certificate and key for mutual TLS — imply `IMQ_REDIS_TLS=1` |
|
|
234
|
+
| `IMQ_REDIS_TLS_KEY_PASSPHRASE` | passphrase of an encrypted private key |
|
|
235
|
+
| `IMQ_REDIS_TLS_SERVERNAME` | expected certificate name, when it is not the host you connect to |
|
|
236
|
+
| `IMQ_REDIS_TLS_REJECT_UNAUTHORIZED` | `0` accepts an unverified certificate — see the warning below |
|
|
237
|
+
|
|
238
|
+
Passing `tls` explicitly always wins, `tls: false` included. The certificate
|
|
239
|
+
files are read as the queue is constructed, and an unreadable one throws: a
|
|
240
|
+
mistyped path or an unmounted secret stops the process rather than leaving it
|
|
241
|
+
talking to the broker in the clear.
|
|
242
|
+
|
|
243
|
+
## Two things that will bite you
|
|
244
|
+
|
|
245
|
+
**The certificate is checked against the host you connect to**, which is a
|
|
246
|
+
problem the moment you connect to an address. See below.
|
|
247
|
+
|
|
248
|
+
**`rejectUnauthorized: false` is not "TLS with less fuss".** It encrypts the
|
|
249
|
+
connection and then accepts whatever certificate answers, which is exactly what
|
|
250
|
+
an interceptor needs. The queue logs a warning when it sees it. Use it to poke
|
|
251
|
+
at a self-signed broker on your laptop, never in a deployment — supply the `ca`
|
|
252
|
+
instead, which is no harder and actually authenticates the server.
|
|
253
|
+
|
|
254
|
+
## Rotating certificates
|
|
255
|
+
|
|
256
|
+
Certificates expire, and the material is read **once**, as the queue is
|
|
257
|
+
constructed. A file replaced in place at the same path — which is what
|
|
258
|
+
cert-manager, Vault and a rotated Kubernetes secret all do — is not picked up
|
|
259
|
+
by a running process, and neither a reconnect nor a broker restart re-reads it.
|
|
260
|
+
|
|
261
|
+
So plan for a restart. Roll the pods when the secret changes and the new
|
|
262
|
+
material is loaded on the way up, which is what most deployments do anyway.
|
|
263
|
+
What you must not do is let the old certificate expire before that restart
|
|
264
|
+
happens.
|
|
265
|
+
|
|
266
|
+
Rotating the **certificate authority** is the case that needs care, because
|
|
267
|
+
every client must trust the new CA before the broker starts presenting
|
|
268
|
+
certificates signed by it. `ca` takes an array, so trust both across the
|
|
269
|
+
overlap:
|
|
270
|
+
|
|
271
|
+
```typescript
|
|
272
|
+
tls: { ca: [readFileSync('ca-old.crt'), readFileSync('ca-new.crt')] }
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Roll the fleet with both trusted, switch the broker, then drop the old one on
|
|
276
|
+
the next roll. `IMQ_REDIS_TLS_CA_FILE` points at a single file, but a PEM
|
|
277
|
+
bundle — several certificates concatenated into one file — works there and is
|
|
278
|
+
the usual way to do this from the environment.
|
|
279
|
+
|
|
280
|
+
## Brokers addressed by IP
|
|
281
|
+
|
|
282
|
+
Cluster discovery announces **addresses**, not names, so a queue dials
|
|
283
|
+
`10.1.2.3` and the default check compares that against the certificate — which
|
|
284
|
+
cannot carry an address nobody knew when it was issued. The handshake fails
|
|
285
|
+
with `ERR_TLS_CERT_ALTNAME_INVALID`.
|
|
286
|
+
|
|
287
|
+
Pin `servername` and the check runs against that name instead of the address:
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
tls: { ca, servername: 'redis-broker.internal' }
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
Or, with no code at all, `IMQ_REDIS_TLS_SERVERNAME=redis-broker.internal`.
|
|
294
|
+
|
|
295
|
+
This is not a weakening. Certificate verification is two independent checks —
|
|
296
|
+
*is this certificate signed by an authority I trust*, and *does it name who I
|
|
297
|
+
expected to reach*. Pinning `servername` changes only the second, from an
|
|
298
|
+
address that is an accident of scheduling to an identity that is stable. A
|
|
299
|
+
broker without a certificate from your CA is refused exactly as before; so is
|
|
300
|
+
one whose certificate names something else. The specs in `test/integration/`
|
|
301
|
+
assert all three.
|
|
302
|
+
|
|
303
|
+
Issuing an IP SAN instead works, and needs no `servername` — but only where
|
|
304
|
+
broker addresses are stable and known in advance. An autoscaled pool's are
|
|
305
|
+
neither, and that is the point: **the certificate names an identity, not a
|
|
306
|
+
location.** A broker that scales out, is rescheduled, or comes back on a
|
|
307
|
+
different address presents the same certificate as before, and the client
|
|
308
|
+
verifies it against the same pinned name. Nothing is issued, re-issued or
|
|
309
|
+
reloaded when an address changes — there is no per-pod PKI to run, and one
|
|
310
|
+
certificate serves the whole pool.
|
|
311
|
+
|
|
312
|
+
Concretely, on Kubernetes: one `Certificate` for `redis-broker.internal`, its
|
|
313
|
+
Secret mounted by every broker pod, its `ca.crt` mounted by every service pod,
|
|
314
|
+
and `IMQ_REDIS_TLS_CA_FILE` plus `IMQ_REDIS_TLS_SERVERNAME` set in the service
|
|
315
|
+
deployment. Scaling the broker pool changes nothing about any of it.
|
|
316
|
+
|
|
317
|
+
**This is also what makes UDP discovery safe to keep.** The announce channel is
|
|
318
|
+
unauthenticated, so anyone who can inject a broadcast can point a service at an
|
|
319
|
+
address of their choosing. With TLS and a private CA, the worst that address can
|
|
320
|
+
do is fail to complete a handshake: it cannot present a certificate your CA
|
|
321
|
+
signed, so a spoofed announcement costs you a connection rather than the
|
|
322
|
+
traffic on it. That converts an interception risk into a denial-of-service one.
|
|
323
|
+
It does not authenticate the announcements themselves.
|
|
324
|
+
|
|
325
|
+
## Connection sharing
|
|
326
|
+
|
|
327
|
+
Writer and watcher connections are shared per server within a process, and that
|
|
328
|
+
sharing accounts for TLS: queues reaching the same host and port under different
|
|
329
|
+
TLS configurations get separate connections. A queue that asked for encryption
|
|
330
|
+
can never be handed a plaintext socket another queue opened first, nor the
|
|
331
|
+
reverse. Configurations equal by value — the same anchors, the same client
|
|
332
|
+
certificate, the same callbacks — still share.
|
|
333
|
+
|
|
334
|
+
## What it does not cover
|
|
335
|
+
|
|
336
|
+
`UDPClusterManager` announces cluster membership over unauthenticated UDP
|
|
337
|
+
broadcast, and `tls` does not touch that: it secures the queue's connections to
|
|
338
|
+
the broker, not the discovery channel that tells it which brokers exist. Treat
|
|
339
|
+
UDP discovery as trusted-network-only regardless of this setting.
|
|
340
|
+
|
|
116
341
|
# Benchmarking
|
|
117
342
|
|
|
118
343
|
First, make sure redis-server is running on localhost. The current version of the
|
package/SECURITY.md
CHANGED
|
@@ -38,3 +38,31 @@ release before reporting.
|
|
|
38
38
|
The `@imqueue` framework is open source under GPL-3.0. This policy covers the code
|
|
39
39
|
in the `@imqueue/*` packages. Vulnerabilities in third-party dependencies should be
|
|
40
40
|
reported to those projects, though we're glad to help coordinate an upgrade.
|
|
41
|
+
|
|
42
|
+
## Transport security
|
|
43
|
+
|
|
44
|
+
Connections to the redis broker are plaintext unless you ask for TLS. Set the
|
|
45
|
+
`tls` option, or the `IMQ_REDIS_TLS*` environment variables, to encrypt every
|
|
46
|
+
connection a queue opens — see the TLS section of the README. When TLS is
|
|
47
|
+
configured there is no downgrade path: a broker that will not complete the
|
|
48
|
+
handshake is reported as a connection failure rather than reached in the clear.
|
|
49
|
+
|
|
50
|
+
Two limits are worth stating plainly:
|
|
51
|
+
|
|
52
|
+
- `UDPClusterManager` announces cluster membership over unauthenticated UDP
|
|
53
|
+
broadcast. The announcements are neither encrypted nor signed, and the `tls`
|
|
54
|
+
option does not apply to them.
|
|
55
|
+
|
|
56
|
+
It does, however, bound what a forged announcement can achieve. Because
|
|
57
|
+
brokers are announced by address, verification must be pinned to a name
|
|
58
|
+
(`servername`) rather than left to compare against the address; a forged
|
|
59
|
+
announcement then points a service at a host that cannot present a
|
|
60
|
+
certificate signed by your authority, so it fails to connect rather than
|
|
61
|
+
receiving traffic. Treat that as reducing an interception risk to a
|
|
62
|
+
denial-of-service one, not as authenticating discovery.
|
|
63
|
+
- `rejectUnauthorized: false` leaves a connection encrypted but unauthenticated,
|
|
64
|
+
and therefore open to interception. The queue warns when it is set. Supply the
|
|
65
|
+
`ca` for your private authority instead.
|
|
66
|
+
- TLS material is read once, when the queue is constructed. A certificate
|
|
67
|
+
rotated in place is not picked up by a running process, so rotation needs a
|
|
68
|
+
restart — see the README for the CA-overlap procedure.
|
package/index.d.ts
CHANGED
|
@@ -41,8 +41,16 @@
|
|
|
41
41
|
*
|
|
42
42
|
* Delivery is at-least-once, so message handlers must be idempotent. Within a
|
|
43
43
|
* single process, writer and watcher connections are shared per `host:port` and
|
|
44
|
-
* reference-counted, and exactly one queue per key prefix
|
|
45
|
-
* watcher that releases delayed messages and performs
|
|
44
|
+
* TLS configuration and reference-counted, and exactly one queue per key prefix
|
|
45
|
+
* is elected as the watcher that releases delayed messages and performs
|
|
46
|
+
* maintenance.
|
|
47
|
+
*
|
|
48
|
+
* Under {@link IMQOptions.safeDelivery} a message stays checked out to its
|
|
49
|
+
* worker until the `message` listener has finished with it, and what a listener
|
|
50
|
+
* **returns** is how it says so: return a promise and the message is held until
|
|
51
|
+
* that promise settles, so a worker that dies mid-handler leaves the message to
|
|
52
|
+
* be re-queued rather than taking it down. Return anything else — as the
|
|
53
|
+
* synchronous example below does — and it is released as the listener returns.
|
|
46
54
|
*
|
|
47
55
|
* @example
|
|
48
56
|
* ```typescript
|
package/index.js
CHANGED
|
@@ -41,8 +41,16 @@
|
|
|
41
41
|
*
|
|
42
42
|
* Delivery is at-least-once, so message handlers must be idempotent. Within a
|
|
43
43
|
* single process, writer and watcher connections are shared per `host:port` and
|
|
44
|
-
* reference-counted, and exactly one queue per key prefix
|
|
45
|
-
* watcher that releases delayed messages and performs
|
|
44
|
+
* TLS configuration and reference-counted, and exactly one queue per key prefix
|
|
45
|
+
* is elected as the watcher that releases delayed messages and performs
|
|
46
|
+
* maintenance.
|
|
47
|
+
*
|
|
48
|
+
* Under {@link IMQOptions.safeDelivery} a message stays checked out to its
|
|
49
|
+
* worker until the `message` listener has finished with it, and what a listener
|
|
50
|
+
* **returns** is how it says so: return a promise and the message is held until
|
|
51
|
+
* that promise settles, so a worker that dies mid-handler leaves the message to
|
|
52
|
+
* be re-queued rather than taking it down. Return anything else — as the
|
|
53
|
+
* synchronous example below does — and it is released as the listener returns.
|
|
46
54
|
*
|
|
47
55
|
* @example
|
|
48
56
|
* ```typescript
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.5.0",
|
|
4
4
|
"description": "Fast JSON message queue over Redis for inter-service communication in Node.js & TypeScript microservices — the transport layer of the @imqueue framework",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"message-queue",
|
|
@@ -25,10 +25,11 @@
|
|
|
25
25
|
"lint": "oxlint",
|
|
26
26
|
"format": "oxfmt \"index.ts\" \"src/**/*.ts\" \"test/**/*.ts\" \"benchmark/**/*.ts\"",
|
|
27
27
|
"format:check": "oxfmt --check \"index.ts\" \"src/**/*.ts\" \"test/**/*.ts\" \"benchmark/**/*.ts\"",
|
|
28
|
-
"test": "npm run build && node --experimental-test-module-mocks --import ./test/mocks/index.js --test --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
29
|
-
"test-coverage": "npm run build && node --experimental-test-module-mocks --enable-source-maps --import ./test/mocks/index.js --test --experimental-test-coverage --test-timeout=15000 $(find test -name '*.spec.js')",
|
|
30
|
-
"test-lcov": "npm run build && mkdir -p coverage && node --experimental-test-module-mocks --enable-source-maps --import ./test/mocks/index.js --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
|
|
28
|
+
"test": "npm run build && node --experimental-test-module-mocks --import ./test/mocks/index.js --test --test-timeout=15000 $(find test/unit -name '*.spec.js')",
|
|
29
|
+
"test-coverage": "npm run build && node --experimental-test-module-mocks --enable-source-maps --import ./test/mocks/index.js --test --experimental-test-coverage --test-timeout=15000 $(find test/unit -name '*.spec.js')",
|
|
30
|
+
"test-lcov": "npm run build && mkdir -p coverage && node --experimental-test-module-mocks --enable-source-maps --import ./test/mocks/index.js --test --experimental-test-coverage --test-reporter=lcov --test-reporter-destination=coverage/lcov.info --test-timeout=15000 $(find test/unit -name '*.spec.js'); node scripts/strip-comment-coverage.mjs coverage/lcov.info",
|
|
31
31
|
"test-coverage-html": "npm run test-lcov; genhtml coverage/lcov.info --output-directory coverage/html --ignore-errors inconsistent,corrupt,format,mismatch && echo \"Coverage report: file://$(pwd)/coverage/html/index.html\"",
|
|
32
|
+
"test-integration": "npm run build && node --test --test-timeout=60000 $(find test/integration -name '*.spec.js')",
|
|
32
33
|
"test-dev": "npm run test && npm run clean-js && npm run clean-typedefs && npm run clean-maps",
|
|
33
34
|
"clean-typedefs": "find . -name '*.d.ts' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
|
34
35
|
"clean-maps": "find . -name '*.js.map' -not -wholename '*node_modules*' -not -wholename '*generator*' -type f -delete",
|
package/src/ClusterManager.d.ts
CHANGED
|
@@ -51,6 +51,11 @@ export declare abstract class ClusterManager {
|
|
|
51
51
|
* Clusters currently registered with this manager.
|
|
52
52
|
*/
|
|
53
53
|
protected clusters: InitializedCluster[];
|
|
54
|
+
/**
|
|
55
|
+
* Protected so that a manager is only ever obtained through a subclass —
|
|
56
|
+
* this base class holds the registry but implements no transport of its
|
|
57
|
+
* own.
|
|
58
|
+
*/
|
|
54
59
|
protected constructor();
|
|
55
60
|
/**
|
|
56
61
|
* Registers a cluster's membership callbacks with this manager and returns
|
package/src/ClusterManager.js
CHANGED
|
@@ -41,6 +41,11 @@ export class ClusterManager {
|
|
|
41
41
|
* Clusters currently registered with this manager.
|
|
42
42
|
*/
|
|
43
43
|
clusters = [];
|
|
44
|
+
/**
|
|
45
|
+
* Protected so that a manager is only ever obtained through a subclass —
|
|
46
|
+
* this base class holds the registry but implements no transport of its
|
|
47
|
+
* own.
|
|
48
|
+
*/
|
|
44
49
|
constructor() { }
|
|
45
50
|
/**
|
|
46
51
|
* Registers a cluster's membership callbacks with this manager and returns
|