@imqueue/core 3.8.0 → 3.9.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/CHANGELOG.md +39 -0
- package/README.md +23 -1
- package/package.json +1 -1
- package/src/ClusteredRedisQueue.d.ts +91 -16
- package/src/ClusteredRedisQueue.js +156 -35
- package/src/IMessageQueue.d.ts +42 -0
- package/src/helpers/index.d.ts +1 -0
- package/src/helpers/index.js +1 -0
- package/src/helpers/nextRoundRobin.d.ts +35 -0
- package/src/helpers/nextRoundRobin.js +46 -0
- package/.claude/settings.local.json +0 -9
package/CHANGELOG.md
CHANGED
|
@@ -11,6 +11,45 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|
|
11
11
|
|
|
12
12
|
## [Unreleased]
|
|
13
13
|
|
|
14
|
+
### Changed
|
|
15
|
+
|
|
16
|
+
- **A clustered `publish()` goes to one broker of the fleet, not to every
|
|
17
|
+
broker.** `subscribe()` already opens the channel on every broker,
|
|
18
|
+
including brokers that join later, so an event published on any one of them
|
|
19
|
+
reaches every subscriber. Publishing on all of them put the whole pub/sub
|
|
20
|
+
stream on every broker — adding brokers took no pub/sub load off the others
|
|
21
|
+
— and ran every subscriber's handler once per broker. Now the broker is
|
|
22
|
+
picked by health-aware round-robin, as for `send()`: publishes spread across
|
|
23
|
+
the fleet, and each subscriber receives an event once. A broker that was
|
|
24
|
+
just discovered is not picked until it has been known for
|
|
25
|
+
`publishSettleTime`, so that subscribers have had time to discover it and
|
|
26
|
+
subscribe on it; when no broker is that old, any ready one is used. A
|
|
27
|
+
publish that fails on its broker is retried once on another ready broker
|
|
28
|
+
before the call rejects, and a broker whose publishes fail is reported once,
|
|
29
|
+
on entering that state. Brokers listed in a static `cluster` are settled from
|
|
30
|
+
the start. An empty cluster still resolves without publishing and logs
|
|
31
|
+
`knownServers=0`.
|
|
32
|
+
|
|
33
|
+
An event now reaches only the subscribers subscribed on the broker it went
|
|
34
|
+
to, so a subscriber that has just started, or whose subscription on that
|
|
35
|
+
broker is still being retried, can miss one that a fan-out would have
|
|
36
|
+
delivered through another broker. A fleet whose subscribers do not each hold
|
|
37
|
+
every broker — a single-host queue, or a cluster manager limited to part of
|
|
38
|
+
the fleet — keeps the old behavior with `publishFanOut: true` on the
|
|
39
|
+
publisher.
|
|
40
|
+
|
|
41
|
+
### Added
|
|
42
|
+
|
|
43
|
+
- **`IMQOptions.publishSettleTime`**, how long (ms) a clustered queue must
|
|
44
|
+
have known a discovered broker before a publish prefers it. Defaults to
|
|
45
|
+
`IMQ_PUBLISH_SETTLE_TIME`, or 7000: the UDP announcement interval (1 s) plus
|
|
46
|
+
the default `aliveTimeoutCorrection` (5 s), plus one interval for the
|
|
47
|
+
subscription.
|
|
48
|
+
- **`IMQOptions.publishFanOut`**, which makes a clustered `publish()` write
|
|
49
|
+
every event to every broker, as before.
|
|
50
|
+
|
|
51
|
+
## [3.8.0] - 2026-11-24
|
|
52
|
+
|
|
14
53
|
### Fixed
|
|
15
54
|
|
|
16
55
|
- **A broker kept after its announcements lapsed was never checked again, so
|
package/README.md
CHANGED
|
@@ -47,7 +47,8 @@ With current implementation on RedisQueue:
|
|
|
47
47
|
- **Safe, predictable scaling of queues**. Scaling the number of workers does not
|
|
48
48
|
increase traffic usage.
|
|
49
49
|
- **Round-robin message balancing between multiple Redis instances**. This
|
|
50
|
-
allows easy horizontal scaling of the messaging queue across Redis instances
|
|
50
|
+
allows easy horizontal scaling of the messaging queue across Redis instances,
|
|
51
|
+
for queued messages and pub/sub events alike.
|
|
51
52
|
- **TypeScript included!**
|
|
52
53
|
|
|
53
54
|
# Requirements
|
|
@@ -192,6 +193,27 @@ in which in-flight work is lost — it does not close it, and `SIGKILL` on the
|
|
|
192
193
|
whole node, an OOM kill or a lost machine still take work with them — so handlers
|
|
193
194
|
must be idempotent.
|
|
194
195
|
|
|
196
|
+
# Pub/sub across several brokers
|
|
197
|
+
|
|
198
|
+
A clustered queue subscribes on every broker it knows, including brokers that
|
|
199
|
+
join later, and publishes each event on **one** of them, picked by the same
|
|
200
|
+
health-aware round-robin that `send()` uses. So every subscriber receives each
|
|
201
|
+
event once, and the event stream splits across the brokers: adding a broker
|
|
202
|
+
takes pub/sub load off the others, as it does for queued messages.
|
|
203
|
+
|
|
204
|
+
A broker that was just discovered is not picked until it has been known for
|
|
205
|
+
`publishSettleTime` (7 s by default, or `IMQ_PUBLISH_SETTLE_TIME`), giving
|
|
206
|
+
every subscriber time to discover it and subscribe on it; when no broker is
|
|
207
|
+
that old, any ready one is used. A publish that fails on its broker is retried
|
|
208
|
+
once on another ready broker before it rejects.
|
|
209
|
+
|
|
210
|
+
An event reaches only the subscribers that are subscribed on the broker it
|
|
211
|
+
went to. Where some subscribers do not hold the whole fleet — a single-host
|
|
212
|
+
queue, or a cluster manager limited to part of it — set `publishFanOut: true`
|
|
213
|
+
on the publisher to write every event to every broker, as releases before
|
|
214
|
+
3.9.0 did. A clustered subscriber then receives each event once per broker
|
|
215
|
+
and has to deduplicate.
|
|
216
|
+
|
|
195
217
|
# Transport encryption (TLS)
|
|
196
218
|
|
|
197
219
|
Set `tls` and every connection the queue opens — reader, writer, watcher and
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@imqueue/core",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.9.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",
|
|
@@ -57,11 +57,14 @@ export interface ClusterServer extends IMessageQueueConnection {
|
|
|
57
57
|
*
|
|
58
58
|
* @remarks
|
|
59
59
|
* Distribution is asymmetric, and this is the most important thing to know about
|
|
60
|
-
* the class: {@link ClusteredRedisQueue.send}
|
|
61
|
-
*
|
|
60
|
+
* the class: {@link ClusteredRedisQueue.send} and
|
|
61
|
+
* {@link ClusteredRedisQueue.publish} route each message to exactly one
|
|
62
|
+
* server, chosen by health-aware round-robin that skips instances whose
|
|
62
63
|
* writer connection is not ready. Every other operation — `start`, `stop`,
|
|
63
|
-
* `clear`, `destroy`, `
|
|
64
|
-
*
|
|
64
|
+
* `clear`, `destroy`, `subscribe`, `unsubscribe` and `queueLength` — fans out
|
|
65
|
+
* to every server. That pairing is what makes pub/sub split across the
|
|
66
|
+
* cluster: every subscriber listens on every server, so an event published on
|
|
67
|
+
* any one of them reaches each subscriber once.
|
|
65
68
|
*
|
|
66
69
|
* Fan-out normally uses `Promise.all`, with no rollback on failure. Destroy
|
|
67
70
|
* attempts every cleanup task and reports failures together in an AggregateError.
|
|
@@ -112,6 +115,27 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
112
115
|
* Current queue index (round-robin)
|
|
113
116
|
*/
|
|
114
117
|
private currentQueue;
|
|
118
|
+
/**
|
|
119
|
+
* Round-robin position of the next publish, kept apart from sends so that
|
|
120
|
+
* publishes spread evenly whatever the mix of the two
|
|
121
|
+
*/
|
|
122
|
+
private publishCursor;
|
|
123
|
+
/**
|
|
124
|
+
* How long (ms) a discovered server must have been known before a
|
|
125
|
+
* publish prefers it, see {@link IMQOptions.publishSettleTime}
|
|
126
|
+
*/
|
|
127
|
+
private readonly publishSettleTime;
|
|
128
|
+
/**
|
|
129
|
+
* When each server's queue joined this cluster (ms since the epoch), or 0
|
|
130
|
+
* for servers listed in the static configuration, which nobody has to
|
|
131
|
+
* discover
|
|
132
|
+
*/
|
|
133
|
+
private readonly joinedAt;
|
|
134
|
+
/**
|
|
135
|
+
* Servers whose last publish failed, so that a failing server is reported
|
|
136
|
+
* on entering the state only and not on every publish
|
|
137
|
+
*/
|
|
138
|
+
private readonly publishFailing;
|
|
115
139
|
/**
|
|
116
140
|
* Time (ms) send() waits for the first server when the cluster is empty
|
|
117
141
|
*/
|
|
@@ -514,24 +538,72 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
514
538
|
*/
|
|
515
539
|
listenerCount(...args: any[]): number;
|
|
516
540
|
/**
|
|
517
|
-
* Publishes the payload on
|
|
541
|
+
* Publishes the payload on one redis host of the cluster, chosen by
|
|
542
|
+
* health-aware round-robin among the hosts every subscriber can be
|
|
543
|
+
* expected to know.
|
|
518
544
|
*
|
|
519
545
|
* @param data - payload to publish as a channel message
|
|
520
546
|
* @param toName - optional different pub/sub name to publish to
|
|
521
|
-
* @throws TypeError propagated from
|
|
547
|
+
* @throws TypeError propagated from the chosen host when it has no writer
|
|
548
|
+
* connection and no other host is ready to take the publish
|
|
522
549
|
*
|
|
523
550
|
* @remarks
|
|
524
|
-
*
|
|
525
|
-
*
|
|
526
|
-
*
|
|
527
|
-
*
|
|
528
|
-
*
|
|
529
|
-
*
|
|
530
|
-
*
|
|
531
|
-
*
|
|
532
|
-
*
|
|
551
|
+
* Like {@link ClusteredRedisQueue.send}, the payload goes to a single host.
|
|
552
|
+
* One is enough: {@link ClusteredRedisQueue.subscribe} opens the channel on
|
|
553
|
+
* every host, including hosts that join later, so each subscriber receives
|
|
554
|
+
* the event exactly once, and the pub/sub stream splits across the cluster
|
|
555
|
+
* the way queued messages do.
|
|
556
|
+
*
|
|
557
|
+
* The host is picked in this order: a ready host known for at least
|
|
558
|
+
* {@link IMQOptions.publishSettleTime}, so that subscribers have had time
|
|
559
|
+
* to discover it; any ready host, when none has been known that long; and,
|
|
560
|
+
* when no host is ready, the next one in turn, which is known to be down.
|
|
561
|
+
* If publishing on the chosen host fails, the publish is retried once on
|
|
562
|
+
* another ready host, picked the same way, before the call rejects. A
|
|
563
|
+
* publish whose reply was lost after the host accepted it is therefore
|
|
564
|
+
* delivered twice — redis pub/sub is at-most-once per host, not
|
|
565
|
+
* exactly-once.
|
|
566
|
+
*
|
|
567
|
+
* An event reaches only subscribers that have discovered the chosen host
|
|
568
|
+
* and opened the channel on it. A subscriber that has just started, or
|
|
569
|
+
* whose subscription on that host is still being retried, misses it,
|
|
570
|
+
* where fanning out would have reached it through another host. Fleets
|
|
571
|
+
* whose subscribers do not each hold the whole cluster need
|
|
572
|
+
* {@link IMQOptions.publishFanOut}, which writes every event to every
|
|
573
|
+
* host as before — and then rejects if any host has no writer connection,
|
|
574
|
+
* even though other hosts may already have published.
|
|
575
|
+
*
|
|
576
|
+
* On an empty cluster it resolves without publishing anything — reporting
|
|
577
|
+
* that through the logger on entering the state — and unlike `send()` it
|
|
578
|
+
* does not wait for a server to appear.
|
|
533
579
|
*/
|
|
534
580
|
publish(data: JsonObject, toName?: string): Promise<void>;
|
|
581
|
+
/**
|
|
582
|
+
* Picks the host a publish goes to.
|
|
583
|
+
*
|
|
584
|
+
* @param failed - a host that has just failed this publish, which is
|
|
585
|
+
* excluded; with it, only a ready host is returned
|
|
586
|
+
* @returns the chosen host, or `undefined` when `failed` is given and no
|
|
587
|
+
* other host is ready
|
|
588
|
+
*
|
|
589
|
+
* @remarks
|
|
590
|
+
* Scans in round-robin order from where the previous publish stopped, so
|
|
591
|
+
* publishes spread across the hosts of each tier: ready hosts known for
|
|
592
|
+
* {@link IMQOptions.publishSettleTime} first, then any ready host. Without
|
|
593
|
+
* a host that just failed, and with no host ready at all, the next host in
|
|
594
|
+
* turn is returned anyway, as `send()` does: its publish then reports why.
|
|
595
|
+
*/
|
|
596
|
+
private selectPublishQueue;
|
|
597
|
+
/**
|
|
598
|
+
* Publishes on one host, reporting a host whose publishes fail on
|
|
599
|
+
* entering that state and clearing it on its next success.
|
|
600
|
+
*
|
|
601
|
+
* @param imq - the host to publish on
|
|
602
|
+
* @param data - payload to publish
|
|
603
|
+
* @param channel - name the publish goes to, for the report only
|
|
604
|
+
* @param toName - the caller's pub/sub name, passed through
|
|
605
|
+
*/
|
|
606
|
+
private publishOn;
|
|
535
607
|
/**
|
|
536
608
|
* Subscribes the given handler on every redis host in the cluster, and
|
|
537
609
|
* remembers the subscription so servers that join later are subscribed
|
|
@@ -570,7 +642,10 @@ export declare class ClusteredRedisQueue implements IMessageQueue, EventEmitter<
|
|
|
570
642
|
* that a host was unreachable when the call was made, not that it stays
|
|
571
643
|
* unsubscribed.
|
|
572
644
|
*
|
|
573
|
-
*
|
|
645
|
+
* Each event published through a {@link ClusteredRedisQueue} goes to one
|
|
646
|
+
* host, so the handler is invoked once per event — once per host only
|
|
647
|
+
* where the publisher uses {@link IMQOptions.publishFanOut}, or publishes
|
|
648
|
+
* the same event on several hosts by other means.
|
|
574
649
|
*/
|
|
575
650
|
subscribe(channel: string, handler: (data: JsonObject) => void): Promise<void>;
|
|
576
651
|
/**
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { EventEmitter } from 'node:events';
|
|
25
25
|
import {} from './ClusterManager.js';
|
|
26
|
-
import { buildOptions, copyEventEmitter, envInt, errorCode, } from './helpers/index.js';
|
|
26
|
+
import { buildOptions, copyEventEmitter, envInt, errorCode, nextRoundRobin, } from './helpers/index.js';
|
|
27
27
|
import { DEFAULT_IMQ_OPTIONS, IMQMode, RedisQueue, } from './index.js';
|
|
28
28
|
/**
|
|
29
29
|
* Time (ms) send() waits for the first cluster server to become available
|
|
@@ -66,6 +66,19 @@ const LAPSED_ANSWER_TIMEOUT = envInt('IMQ_LAPSED_ANSWER_TIMEOUT', 2000);
|
|
|
66
66
|
* the check into a PING loop.
|
|
67
67
|
*/
|
|
68
68
|
const LAPSED_RECHECK_INTERVAL = 1000;
|
|
69
|
+
/**
|
|
70
|
+
* Default for {@link IMQOptions.publishSettleTime}: how long (ms) a discovered
|
|
71
|
+
* server must have been known before a publish prefers it. Configurable via
|
|
72
|
+
* the IMQ_PUBLISH_SETTLE_TIME environment variable, defaults to 7,000.
|
|
73
|
+
*
|
|
74
|
+
* @remarks
|
|
75
|
+
* The UDP announcement interval (1 s) plus the manager's default
|
|
76
|
+
* `aliveTimeoutCorrection` (5 s) is the longest a subscriber may go without
|
|
77
|
+
* hearing a broker that the manager still treats as normal, so it is also how
|
|
78
|
+
* late a subscriber may discover a broker the publisher already knows. One
|
|
79
|
+
* more interval covers opening the channel on it once discovered.
|
|
80
|
+
*/
|
|
81
|
+
const PUBLISH_SETTLE_TIME = envInt('IMQ_PUBLISH_SETTLE_TIME', 7000);
|
|
69
82
|
/**
|
|
70
83
|
* Scales a single logical queue horizontally across several redis instances.
|
|
71
84
|
* This is what {@link IMQ.create} returns when {@link IMQOptions.cluster} or
|
|
@@ -73,11 +86,14 @@ const LAPSED_RECHECK_INTERVAL = 1000;
|
|
|
73
86
|
*
|
|
74
87
|
* @remarks
|
|
75
88
|
* Distribution is asymmetric, and this is the most important thing to know about
|
|
76
|
-
* the class: {@link ClusteredRedisQueue.send}
|
|
77
|
-
*
|
|
89
|
+
* the class: {@link ClusteredRedisQueue.send} and
|
|
90
|
+
* {@link ClusteredRedisQueue.publish} route each message to exactly one
|
|
91
|
+
* server, chosen by health-aware round-robin that skips instances whose
|
|
78
92
|
* writer connection is not ready. Every other operation — `start`, `stop`,
|
|
79
|
-
* `clear`, `destroy`, `
|
|
80
|
-
*
|
|
93
|
+
* `clear`, `destroy`, `subscribe`, `unsubscribe` and `queueLength` — fans out
|
|
94
|
+
* to every server. That pairing is what makes pub/sub split across the
|
|
95
|
+
* cluster: every subscriber listens on every server, so an event published on
|
|
96
|
+
* any one of them reaches each subscriber once.
|
|
81
97
|
*
|
|
82
98
|
* Fan-out normally uses `Promise.all`, with no rollback on failure. Destroy
|
|
83
99
|
* attempts every cleanup task and reports failures together in an AggregateError.
|
|
@@ -119,6 +135,27 @@ export class ClusteredRedisQueue {
|
|
|
119
135
|
* Current queue index (round-robin)
|
|
120
136
|
*/
|
|
121
137
|
currentQueue = 0;
|
|
138
|
+
/**
|
|
139
|
+
* Round-robin position of the next publish, kept apart from sends so that
|
|
140
|
+
* publishes spread evenly whatever the mix of the two
|
|
141
|
+
*/
|
|
142
|
+
publishCursor = 0;
|
|
143
|
+
/**
|
|
144
|
+
* How long (ms) a discovered server must have been known before a
|
|
145
|
+
* publish prefers it, see {@link IMQOptions.publishSettleTime}
|
|
146
|
+
*/
|
|
147
|
+
publishSettleTime;
|
|
148
|
+
/**
|
|
149
|
+
* When each server's queue joined this cluster (ms since the epoch), or 0
|
|
150
|
+
* for servers listed in the static configuration, which nobody has to
|
|
151
|
+
* discover
|
|
152
|
+
*/
|
|
153
|
+
joinedAt = new WeakMap();
|
|
154
|
+
/**
|
|
155
|
+
* Servers whose last publish failed, so that a failing server is reported
|
|
156
|
+
* on entering the state only and not on every publish
|
|
157
|
+
*/
|
|
158
|
+
publishFailing = new WeakSet();
|
|
122
159
|
/**
|
|
123
160
|
* Time (ms) send() waits for the first server when the cluster is empty
|
|
124
161
|
*/
|
|
@@ -229,6 +266,13 @@ export class ClusteredRedisQueue {
|
|
|
229
266
|
this.clusterEmitter = new EventEmitter();
|
|
230
267
|
this.options = buildOptions(DEFAULT_IMQ_OPTIONS, options);
|
|
231
268
|
this.logger = this.options.logger || console;
|
|
269
|
+
const settleTime = this.options.publishSettleTime;
|
|
270
|
+
this.publishSettleTime =
|
|
271
|
+
settleTime !== undefined &&
|
|
272
|
+
Number.isFinite(settleTime) &&
|
|
273
|
+
settleTime >= 0
|
|
274
|
+
? settleTime
|
|
275
|
+
: PUBLISH_SETTLE_TIME;
|
|
232
276
|
if (!this.options.cluster && !this.options.clusterManagers?.length) {
|
|
233
277
|
throw new TypeError('ClusteredRedisQueue: cluster ' + 'configuration is missing!');
|
|
234
278
|
}
|
|
@@ -327,14 +371,11 @@ export class ClusteredRedisQueue {
|
|
|
327
371
|
selectQueue() {
|
|
328
372
|
const count = this.imqLength;
|
|
329
373
|
const start = this.currentQueue % count;
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
this.noneAvailable = false;
|
|
336
|
-
return candidate;
|
|
337
|
-
}
|
|
374
|
+
const index = nextRoundRobin(this.imqs, start, imq => imq.available);
|
|
375
|
+
if (index >= 0) {
|
|
376
|
+
this.currentQueue = index + 1;
|
|
377
|
+
this.noneAvailable = false;
|
|
378
|
+
return this.imqs[index];
|
|
338
379
|
}
|
|
339
380
|
// every instance reports its connection as not ready, so the message
|
|
340
381
|
// goes to a host known to be down. Reported on entering the state
|
|
@@ -744,41 +785,114 @@ export class ClusteredRedisQueue {
|
|
|
744
785
|
return fn.apply(source, args);
|
|
745
786
|
}
|
|
746
787
|
/**
|
|
747
|
-
* Publishes the payload on
|
|
788
|
+
* Publishes the payload on one redis host of the cluster, chosen by
|
|
789
|
+
* health-aware round-robin among the hosts every subscriber can be
|
|
790
|
+
* expected to know.
|
|
748
791
|
*
|
|
749
792
|
* @param data - payload to publish as a channel message
|
|
750
793
|
* @param toName - optional different pub/sub name to publish to
|
|
751
|
-
* @throws TypeError propagated from
|
|
794
|
+
* @throws TypeError propagated from the chosen host when it has no writer
|
|
795
|
+
* connection and no other host is ready to take the publish
|
|
752
796
|
*
|
|
753
797
|
* @remarks
|
|
754
|
-
*
|
|
755
|
-
*
|
|
756
|
-
*
|
|
757
|
-
*
|
|
758
|
-
*
|
|
759
|
-
*
|
|
760
|
-
*
|
|
761
|
-
*
|
|
762
|
-
*
|
|
798
|
+
* Like {@link ClusteredRedisQueue.send}, the payload goes to a single host.
|
|
799
|
+
* One is enough: {@link ClusteredRedisQueue.subscribe} opens the channel on
|
|
800
|
+
* every host, including hosts that join later, so each subscriber receives
|
|
801
|
+
* the event exactly once, and the pub/sub stream splits across the cluster
|
|
802
|
+
* the way queued messages do.
|
|
803
|
+
*
|
|
804
|
+
* The host is picked in this order: a ready host known for at least
|
|
805
|
+
* {@link IMQOptions.publishSettleTime}, so that subscribers have had time
|
|
806
|
+
* to discover it; any ready host, when none has been known that long; and,
|
|
807
|
+
* when no host is ready, the next one in turn, which is known to be down.
|
|
808
|
+
* If publishing on the chosen host fails, the publish is retried once on
|
|
809
|
+
* another ready host, picked the same way, before the call rejects. A
|
|
810
|
+
* publish whose reply was lost after the host accepted it is therefore
|
|
811
|
+
* delivered twice — redis pub/sub is at-most-once per host, not
|
|
812
|
+
* exactly-once.
|
|
813
|
+
*
|
|
814
|
+
* An event reaches only subscribers that have discovered the chosen host
|
|
815
|
+
* and opened the channel on it. A subscriber that has just started, or
|
|
816
|
+
* whose subscription on that host is still being retried, misses it,
|
|
817
|
+
* where fanning out would have reached it through another host. Fleets
|
|
818
|
+
* whose subscribers do not each hold the whole cluster need
|
|
819
|
+
* {@link IMQOptions.publishFanOut}, which writes every event to every
|
|
820
|
+
* host as before — and then rejects if any host has no writer connection,
|
|
821
|
+
* even though other hosts may already have published.
|
|
822
|
+
*
|
|
823
|
+
* On an empty cluster it resolves without publishing anything — reporting
|
|
824
|
+
* that through the logger on entering the state — and unlike `send()` it
|
|
825
|
+
* does not wait for a server to appear.
|
|
763
826
|
*/
|
|
764
827
|
async publish(data, toName) {
|
|
765
|
-
const
|
|
766
|
-
|
|
767
|
-
promises.push(imq.publish(data, toName));
|
|
768
|
-
}
|
|
769
|
-
// an empty cluster resolves an empty Promise.all, so the caller is
|
|
828
|
+
const channel = toName || this.name;
|
|
829
|
+
// an empty cluster has nowhere to publish, so the caller would be
|
|
770
830
|
// told the event went out while nothing was published at all.
|
|
771
831
|
// Reported on entering the state only
|
|
772
|
-
if (!
|
|
832
|
+
if (!this.imqs.length) {
|
|
773
833
|
if (!this.noPublishTargets) {
|
|
774
834
|
this.noPublishTargets = true;
|
|
775
|
-
this.logLine('error', `nothing published to channel ${
|
|
835
|
+
this.logLine('error', `nothing published to channel ${channel}: knownServers=0`);
|
|
836
|
+
}
|
|
837
|
+
return;
|
|
838
|
+
}
|
|
839
|
+
this.noPublishTargets = false;
|
|
840
|
+
if (this.options.publishFanOut) {
|
|
841
|
+
await Promise.all(this.imqs.map(imq => imq.publish(data, toName)));
|
|
842
|
+
return;
|
|
843
|
+
}
|
|
844
|
+
const target = this.selectPublishQueue();
|
|
845
|
+
try {
|
|
846
|
+
await this.publishOn(target, data, channel, toName);
|
|
847
|
+
}
|
|
848
|
+
catch (err) {
|
|
849
|
+
const fallback = this.selectPublishQueue(target);
|
|
850
|
+
if (!fallback) {
|
|
851
|
+
throw err;
|
|
776
852
|
}
|
|
853
|
+
await this.publishOn(fallback, data, channel, toName);
|
|
777
854
|
}
|
|
778
|
-
|
|
779
|
-
|
|
855
|
+
}
|
|
856
|
+
selectPublishQueue(failed) {
|
|
857
|
+
const settledSince = Date.now() - this.publishSettleTime;
|
|
858
|
+
const ready = (imq) => imq !== failed && imq.available;
|
|
859
|
+
const settled = (imq) => ready(imq) && (this.joinedAt.get(imq) ?? 0) <= settledSince;
|
|
860
|
+
for (const accept of [settled, ready]) {
|
|
861
|
+
const index = nextRoundRobin(this.imqs, this.publishCursor, accept);
|
|
862
|
+
if (index >= 0) {
|
|
863
|
+
this.publishCursor = index + 1;
|
|
864
|
+
return this.imqs[index];
|
|
865
|
+
}
|
|
780
866
|
}
|
|
781
|
-
|
|
867
|
+
if (failed) {
|
|
868
|
+
return undefined;
|
|
869
|
+
}
|
|
870
|
+
const index = this.publishCursor % this.imqs.length;
|
|
871
|
+
this.publishCursor = index + 1;
|
|
872
|
+
return this.imqs[index];
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* Publishes on one host, reporting a host whose publishes fail on
|
|
876
|
+
* entering that state and clearing it on its next success.
|
|
877
|
+
*
|
|
878
|
+
* @param imq - the host to publish on
|
|
879
|
+
* @param data - payload to publish
|
|
880
|
+
* @param channel - name the publish goes to, for the report only
|
|
881
|
+
* @param toName - the caller's pub/sub name, passed through
|
|
882
|
+
*/
|
|
883
|
+
async publishOn(imq, data, channel, toName) {
|
|
884
|
+
try {
|
|
885
|
+
await imq.publish(data, toName);
|
|
886
|
+
}
|
|
887
|
+
catch (err) {
|
|
888
|
+
if (!this.publishFailing.has(imq)) {
|
|
889
|
+
this.publishFailing.add(imq);
|
|
890
|
+
this.logLine('warn', `failed to publish to channel ${channel} on server ` +
|
|
891
|
+
`${imq.redisKey}, code ${errorCode(err)}`);
|
|
892
|
+
}
|
|
893
|
+
throw err;
|
|
894
|
+
}
|
|
895
|
+
this.publishFailing.delete(imq);
|
|
782
896
|
}
|
|
783
897
|
/**
|
|
784
898
|
* Subscribes the given handler on every redis host in the cluster, and
|
|
@@ -818,7 +932,10 @@ export class ClusteredRedisQueue {
|
|
|
818
932
|
* that a host was unreachable when the call was made, not that it stays
|
|
819
933
|
* unsubscribed.
|
|
820
934
|
*
|
|
821
|
-
*
|
|
935
|
+
* Each event published through a {@link ClusteredRedisQueue} goes to one
|
|
936
|
+
* host, so the handler is invoked once per event — once per host only
|
|
937
|
+
* where the publisher uses {@link IMQOptions.publishFanOut}, or publishes
|
|
938
|
+
* the same event on several hosts by other means.
|
|
822
939
|
*/
|
|
823
940
|
async subscribe(channel, handler) {
|
|
824
941
|
if (this.closed) {
|
|
@@ -1118,6 +1235,10 @@ export class ClusteredRedisQueue {
|
|
|
1118
1235
|
const imq = new RedisQueue(this.name, opts);
|
|
1119
1236
|
copyEventEmitter(this.templateEmitter, imq);
|
|
1120
1237
|
newServer.imq = imq;
|
|
1238
|
+
// a server from the static configuration is configured on every
|
|
1239
|
+
// process alike, so nobody has to discover it before it may carry
|
|
1240
|
+
// a publish. A discovered one is new to subscribers too
|
|
1241
|
+
this.joinedAt.set(imq, initializeQueue ? Date.now() : 0);
|
|
1121
1242
|
// registered before the catch-up run starts, so that run can test
|
|
1122
1243
|
// membership against `imqs` and see the host it is working on. Nothing
|
|
1123
1244
|
// can observe the order: there is no await between here and the call
|
package/src/IMessageQueue.d.ts
CHANGED
|
@@ -453,6 +453,48 @@ export interface IMQOptions extends Partial<IMessageQueueConnection> {
|
|
|
453
453
|
* {@link ClusterManager} to add your own.
|
|
454
454
|
*/
|
|
455
455
|
clusterManagers?: ClusterManager[];
|
|
456
|
+
/**
|
|
457
|
+
* How long (ms) a clustered queue must have known a server before
|
|
458
|
+
* {@link ClusteredRedisQueue.publish} prefers it as the one server an
|
|
459
|
+
* event goes to.
|
|
460
|
+
*
|
|
461
|
+
* @defaultValue the `IMQ_PUBLISH_SETTLE_TIME` environment variable, or
|
|
462
|
+
* 7000
|
|
463
|
+
*
|
|
464
|
+
* @remarks
|
|
465
|
+
* A clustered publish goes to one server, and reaches every subscriber
|
|
466
|
+
* only where each of them has already discovered that server and opened
|
|
467
|
+
* the channel on it. Subscribers discover a server at about the time the
|
|
468
|
+
* publisher does, but not at the same moment: a discovered server is
|
|
469
|
+
* therefore not preferred until it has been known this long. The default
|
|
470
|
+
* covers the UDP announcement interval (1 s) plus
|
|
471
|
+
* {@link UDPClusterManagerOptions.aliveTimeoutCorrection} (5 s) — the
|
|
472
|
+
* delay a manager already tolerates between two announcements it hears —
|
|
473
|
+
* plus one more interval for the subscription itself. Set it to match
|
|
474
|
+
* other intervals or corrections where your fleet uses them.
|
|
475
|
+
*
|
|
476
|
+
* Servers listed in {@link IMQOptions.cluster} are settled from the
|
|
477
|
+
* start: every process is configured with them, so nobody has to
|
|
478
|
+
* discover them. When no server has been known long enough — a process
|
|
479
|
+
* that has just started, say — any ready server is used. Ignored with
|
|
480
|
+
* {@link IMQOptions.publishFanOut}.
|
|
481
|
+
*/
|
|
482
|
+
publishSettleTime?: number;
|
|
483
|
+
/**
|
|
484
|
+
* Makes {@link ClusteredRedisQueue.publish} write every event to every
|
|
485
|
+
* server of the cluster, as it did before 3.9.0, instead of to one.
|
|
486
|
+
*
|
|
487
|
+
* @defaultValue false
|
|
488
|
+
*
|
|
489
|
+
* @remarks
|
|
490
|
+
* Only for a fleet whose subscribers do not each hold the whole cluster:
|
|
491
|
+
* a subscriber connected to one server alone (a single-host queue, or a
|
|
492
|
+
* cluster manager limited to part of the fleet) receives only what is
|
|
493
|
+
* published on that server. Fan-out costs every server the whole event
|
|
494
|
+
* stream, and delivers each event to a clustered subscriber once per
|
|
495
|
+
* server, so its handlers must deduplicate.
|
|
496
|
+
*/
|
|
497
|
+
publishFanOut?: boolean;
|
|
456
498
|
/**
|
|
457
499
|
* Enable process signal handling (SIGTERM, SIGINT, SIGABRT) by the queue.
|
|
458
500
|
* When enabled, the queue releases its watcher lock on these signals and
|
package/src/helpers/index.d.ts
CHANGED
package/src/helpers/index.js
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Helper: nextRoundRobin
|
|
3
|
+
*
|
|
4
|
+
* I'm Queue Software Project
|
|
5
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
* GNU General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* You should have received a copy of the GNU General Public License
|
|
18
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
|
+
*
|
|
20
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
21
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
22
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Finds the next item, in round-robin order, that a predicate accepts.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T - item type
|
|
28
|
+
* @param items - the items to choose from
|
|
29
|
+
* @param cursor - where the scan starts; taken modulo the number of items,
|
|
30
|
+
* so a cursor that ran past the end of a shrunken list is still valid
|
|
31
|
+
* @param accept - whether an item may be chosen
|
|
32
|
+
* @returns the index of the first accepted item at or after the cursor,
|
|
33
|
+
* wrapping around once, or -1 when none is accepted
|
|
34
|
+
*/
|
|
35
|
+
export declare function nextRoundRobin<T>(items: readonly T[], cursor: number, accept: (item: T) => boolean): number;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/*!
|
|
2
|
+
* Helper: nextRoundRobin
|
|
3
|
+
*
|
|
4
|
+
* I'm Queue Software Project
|
|
5
|
+
* Copyright (C) 2025 imqueue.com <support@imqueue.com>
|
|
6
|
+
*
|
|
7
|
+
* This program is free software: you can redistribute it and/or modify
|
|
8
|
+
* it under the terms of the GNU General Public License as published by
|
|
9
|
+
* the Free Software Foundation, either version 3 of the License, or
|
|
10
|
+
* (at your option) any later version.
|
|
11
|
+
*
|
|
12
|
+
* This program is distributed in the hope that it will be useful,
|
|
13
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
14
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
15
|
+
* GNU General Public License for more details.
|
|
16
|
+
*
|
|
17
|
+
* You should have received a copy of the GNU General Public License
|
|
18
|
+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
19
|
+
*
|
|
20
|
+
* If you want to use this code in a closed source (commercial) project, you can
|
|
21
|
+
* purchase a proprietary commercial license. Please contact us at
|
|
22
|
+
* <support@imqueue.com> to get commercial licensing options.
|
|
23
|
+
*/
|
|
24
|
+
/**
|
|
25
|
+
* Finds the next item, in round-robin order, that a predicate accepts.
|
|
26
|
+
*
|
|
27
|
+
* @typeParam T - item type
|
|
28
|
+
* @param items - the items to choose from
|
|
29
|
+
* @param cursor - where the scan starts; taken modulo the number of items,
|
|
30
|
+
* so a cursor that ran past the end of a shrunken list is still valid
|
|
31
|
+
* @param accept - whether an item may be chosen
|
|
32
|
+
* @returns the index of the first accepted item at or after the cursor,
|
|
33
|
+
* wrapping around once, or -1 when none is accepted
|
|
34
|
+
*/
|
|
35
|
+
export function nextRoundRobin(items, cursor, accept) {
|
|
36
|
+
const count = items.length;
|
|
37
|
+
const start = count ? cursor % count : 0;
|
|
38
|
+
for (let offset = 0; offset < count; offset++) {
|
|
39
|
+
const index = (start + offset) % count;
|
|
40
|
+
if (accept(items[index])) {
|
|
41
|
+
return index;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return -1;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=nextRoundRobin.js.map
|