@imqueue/core 3.2.4 → 3.3.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.
@@ -1,42 +1,79 @@
1
1
  import { ClusterManager } from './ClusterManager.js';
2
2
  import { type ILogger } from './IMessageQueue.js';
3
+ /**
4
+ * Configuration for {@link UDPClusterManager}.
5
+ *
6
+ * Pass any subset to the constructor; unspecified values come from
7
+ * {@link DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS}.
8
+ *
9
+ * @remarks
10
+ * All the broadcast-related values — `address`, `port`, `limitedAddress`,
11
+ * `aliveTimeoutCorrection` and `useAliveCheck` — together identify the shared
12
+ * broadcast worker, so two managers agreeing on all of them share one socket and
13
+ * one worker thread. `logger` and `handleSignals` are not part of that identity.
14
+ */
3
15
  export interface UDPClusterManagerOptions {
4
16
  /**
5
- * Message queue broadcast port
17
+ * UDP port the manager listens on for cluster announcements.
18
+ *
19
+ * @defaultValue 63000
6
20
  *
7
- * @default 63000
8
- * @type {number}
21
+ * @remarks
22
+ * The socket is opened with address and port reuse, so several processes on
23
+ * one host can listen simultaneously. The port is part of the shared-worker
24
+ * identity: managers using different ports each get their own socket and
25
+ * worker thread. There is no separate send port — this manager only listens.
9
26
  */
10
27
  port: number;
11
28
  /**
12
- * Message queue broadcast address
29
+ * Broadcast address the cluster announces on.
13
30
  *
14
- * @type {number}
31
+ * @defaultValue "255.255.255.255"
32
+ *
33
+ * @remarks
34
+ * The worker binds the local IPv4 interface whose address matches this value
35
+ * with its `.255` octets removed — so `10.0.1.255` selects an interface in
36
+ * `10.0.1.x` — and falls back to all interfaces when nothing matches, which is
37
+ * what happens for the default value.
15
38
  */
16
39
  address: string;
17
40
  /**
18
- * Message-queue-limited broadcast address
41
+ * Limited-broadcast address marker. No default: when unset, the worker tries
42
+ * to bind the local interface matching {@link UDPClusterManagerOptions.address}.
19
43
  *
20
- * @default "255.255.255.255"
21
- * @type {string}
44
+ * @remarks
45
+ * This is only ever compared against `address`; it is never itself used as a
46
+ * bind or destination address. Setting it to the same value as `address` makes
47
+ * the worker bind all interfaces (`0.0.0.0`) instead of selecting one.
22
48
  */
23
49
  limitedAddress?: string;
24
50
  /**
25
- * Message queue alive timeout correction. Used to correct waiting time to
26
- * check if the server is alive
51
+ * Grace period in milliseconds added to the liveness timeout each server
52
+ * advertises in its broadcast.
53
+ *
54
+ * @defaultValue 5000
27
55
  *
28
- * @default 5000
29
- * @type {number}
56
+ * @remarks
57
+ * A server is dropped from the cluster when no new announcement arrives within
58
+ * its advertised timeout plus this correction. Raise it on lossy networks to
59
+ * avoid dropping healthy servers. It has no effect when
60
+ * {@link UDPClusterManagerOptions.useAliveCheck} is disabled.
30
61
  */
31
62
  aliveTimeoutCorrection: number;
32
63
  /**
33
- * Message queue alive-server check flag. If set to false, the server will
34
- * not be checked for liveness on each broadcast message with a timeout.
35
- * Can be specified by the environment variable if the given option is not
36
- * bypassed: IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK
64
+ * Whether announced servers are expired when they stop broadcasting.
65
+ *
66
+ * @defaultValue true
37
67
  *
38
- * @default true
39
- * @type {boolean}
68
+ * @remarks
69
+ * When disabled, a server is removed only on an explicit `down` announcement —
70
+ * so a host that dies silently is never removed and keeps receiving
71
+ * round-robin sends.
72
+ *
73
+ * The default can be overridden with the
74
+ * `IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK` environment variable, which is read as
75
+ * a number when the module loads: use `1` to enable and `0` to disable.
76
+ * Any non-numeric value — including the word `true` — is read as disabled.
40
77
  */
41
78
  useAliveCheck: boolean;
42
79
  /**
@@ -46,24 +83,75 @@ export interface UDPClusterManagerOptions {
46
83
  * through the default signal behavior. Disable if the host application
47
84
  * manages its own shutdown sequence.
48
85
  *
49
- * @default true
50
- * @type {boolean}
86
+ * @defaultValue true
87
+ *
88
+ * @remarks
89
+ * The handlers are installed once per process, by the first manager created
90
+ * with this enabled, and on a signal they stop every UDP worker in the
91
+ * process — including those of managers created with this option disabled. So
92
+ * disabling it only means this instance does not install the handlers; it does
93
+ * not exempt its worker from a shutdown another instance triggers.
94
+ *
95
+ * Once installed, the handlers remain for the lifetime of the process, even
96
+ * after every manager has been destroyed.
51
97
  */
52
98
  handleSignals: boolean;
53
99
  /**
54
100
  * Logger used for worker supervision messages
55
101
  *
56
- * @default console
57
- * @type {ILogger}
102
+ * @defaultValue console
58
103
  */
59
104
  logger: ILogger;
60
105
  }
61
106
  /**
62
- * Subset of the manager options handed over to the UDP worker thread. Only
63
- * structured-cloneable values may appear here (no logger).
107
+ * The options actually handed to the UDP worker thread: everything except the
108
+ * logger, which is not structured-cloneable, and the signal-handling flag, which
109
+ * the main thread owns.
110
+ *
111
+ * @remarks
112
+ * These are precisely the settings the worker's behaviour depends on, which is
113
+ * why the same fields form the shared-worker identity — managers agreeing on all
114
+ * of them can share one worker thread.
64
115
  */
65
116
  export type UDPWorkerOptions = Omit<UDPClusterManagerOptions, 'logger' | 'handleSignals'>;
117
+ /**
118
+ * Default options applied to every {@link UDPClusterManager} unless overridden:
119
+ * broadcast address `255.255.255.255` on port `63000`, a 5000 ms alive-timeout
120
+ * correction, liveness checking enabled, process signal handling enabled, and
121
+ * `console` as the logger.
122
+ *
123
+ * @remarks
124
+ * No `limitedAddress` is set by default. The liveness default is read from
125
+ * `IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK` once when the module is loaded, so that
126
+ * variable must be set before the module is first imported.
127
+ */
66
128
  export declare const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS: UDPClusterManagerOptions;
129
+ /**
130
+ * Cluster manager that discovers redis cluster members from UDP broadcast
131
+ * announcements. Supply instances through {@link IMQOptions.clusterManagers}.
132
+ *
133
+ * @remarks
134
+ * It runs a supervised worker thread holding the broadcast socket and applies
135
+ * each announcement to every registered cluster, skipping servers that are
136
+ * already known. Managers created with the same broadcast settings share one
137
+ * socket and worker thread, reference-counted so the worker lives until the last
138
+ * of them is destroyed; the `logger` and `handleSignals` options are not part of
139
+ * that identity. If the worker dies unexpectedly it is logged and re-spawned
140
+ * after one second with all live managers re-attached, so membership tracking
141
+ * resumes on its own.
142
+ *
143
+ * Announcements are not filtered by queue name: every cluster registered with
144
+ * a manager listening on a given address and port receives every server announced
145
+ * there. Isolate unrelated services by giving them distinct broadcast addresses
146
+ * or ports.
147
+ *
148
+ * Wire format — each announcement is one UDP datagram containing five
149
+ * tab-separated fields: queue name, unique server id, `up` or `down`,
150
+ * `host:port`, and the liveness timeout in seconds. Datagrams with a missing
151
+ * field, a port outside 1-65535, or a negative timeout are ignored. An `up`
152
+ * announcement adds the server and refreshes its liveness deadline; a `down`
153
+ * announcement removes it immediately.
154
+ */
67
155
  export declare class UDPClusterManager extends ClusterManager {
68
156
  private static workers;
69
157
  /** Number of manager instances sharing each worker (by worker key) */
@@ -84,6 +172,25 @@ export declare class UDPClusterManager extends ClusterManager {
84
172
  private workerKey;
85
173
  private worker;
86
174
  private destroyed;
175
+ /**
176
+ * Creates the manager and starts listening for broadcasts immediately,
177
+ * joining an existing worker when another manager already uses the same
178
+ * broadcast settings.
179
+ *
180
+ * @param options - partial options merged over
181
+ * {@link DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS}
182
+ *
183
+ * @remarks
184
+ * The merge is a plain shallow spread, so passing an explicit `undefined`
185
+ * overrides a default rather than falling back to it — pass only the
186
+ * properties you mean to change.
187
+ *
188
+ * Broadcast listening begins before any cluster is registered, so register
189
+ * yours with {@link ClusterManager.init} right away: announcements that arrive
190
+ * in the meantime are discarded. When
191
+ * {@link UDPClusterManagerOptions.handleSignals} is enabled, this also
192
+ * installs process-wide signal handlers.
193
+ */
87
194
  constructor(options?: Partial<UDPClusterManagerOptions>);
88
195
  private get logger();
89
196
  /**
@@ -91,8 +198,7 @@ export declare class UDPClusterManager extends ClusterManager {
91
198
  * depends on, so managers configured differently never silently share
92
199
  * a worker built from another manager's options.
93
200
  *
94
- * @param {UDPClusterManagerOptions} options
95
- * @returns {string}
201
+ * @param options -
96
202
  */
97
203
  private static workerKeyFor;
98
204
  /**
@@ -106,8 +212,7 @@ export declare class UDPClusterManager extends ClusterManager {
106
212
  * Stops all workers and re-raises the given signal, so the process
107
213
  * terminates through the default signal behavior.
108
214
  *
109
- * @param {NodeJS.Signals} signal
110
- * @returns {Promise<void>}
215
+ * @param signal -
111
216
  */
112
217
  private static freeAndRaise;
113
218
  private static free;
@@ -121,8 +226,6 @@ export declare class UDPClusterManager extends ClusterManager {
121
226
  * Spawns and supervises a UDP worker for this manager's options. On an
122
227
  * unexpected worker death the worker is dropped from the registry, and
123
228
  * a re-spawn is scheduled while live manager instances remain.
124
- *
125
- * @returns {Worker}
126
229
  */
127
230
  private spawnWorker;
128
231
  /**
@@ -130,7 +233,7 @@ export declare class UDPClusterManager extends ClusterManager {
130
233
  * live manager instances to it, so cluster membership does not silently
131
234
  * freeze after a worker crash.
132
235
  *
133
- * @param {string} workerKey
236
+ * @param workerKey -
134
237
  */
135
238
  private static respawn;
136
239
  /**
@@ -143,10 +246,23 @@ export declare class UDPClusterManager extends ClusterManager {
143
246
  * cluster. Cluster callback errors are contained per cluster, so the
144
247
  * worker message listener can never raise an unhandled rejection.
145
248
  *
146
- * @param {WorkerMessage} message
147
- * @returns {Promise<void>}
249
+ * @param message -
148
250
  */
149
251
  private handleWorkerMessage;
252
+ /**
253
+ * Stops this manager and releases its share of the broadcast worker.
254
+ *
255
+ * @remarks
256
+ * Safe to call more than once. The shared worker is only shut down when this
257
+ * is the last manager using it — otherwise the call just releases this
258
+ * manager's reference and the worker keeps running for the others. Shutdown
259
+ * asks the worker to close its socket and waits up to five seconds for
260
+ * confirmation before terminating the thread.
261
+ *
262
+ * Process signal handlers installed by this manager are not removed, and
263
+ * registered clusters are not cleared — use {@link ClusterManager.remove} for
264
+ * that, which itself calls this method once the last cluster is gone.
265
+ */
150
266
  destroy(): Promise<void>;
151
267
  private static destroyWorker;
152
268
  }
@@ -28,6 +28,17 @@ import {} from './IMessageQueue.js';
28
28
  const IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK = !!+(process.env.IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK || 1);
29
29
  /** Delay (ms) before an unexpectedly dead worker is re-spawned */
30
30
  const WORKER_RESPAWN_DELAY = 1000;
31
+ /**
32
+ * Default options applied to every {@link UDPClusterManager} unless overridden:
33
+ * broadcast address `255.255.255.255` on port `63000`, a 5000 ms alive-timeout
34
+ * correction, liveness checking enabled, process signal handling enabled, and
35
+ * `console` as the logger.
36
+ *
37
+ * @remarks
38
+ * No `limitedAddress` is set by default. The liveness default is read from
39
+ * `IMQ_UDP_CLUSTER_MANAGER_ALIVE_CHECK` once when the module is loaded, so that
40
+ * variable must be set before the module is first imported.
41
+ */
31
42
  export const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
32
43
  port: 63000,
33
44
  address: '255.255.255.255',
@@ -36,6 +47,32 @@ export const DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS = {
36
47
  handleSignals: true,
37
48
  logger: console,
38
49
  };
50
+ /**
51
+ * Cluster manager that discovers redis cluster members from UDP broadcast
52
+ * announcements. Supply instances through {@link IMQOptions.clusterManagers}.
53
+ *
54
+ * @remarks
55
+ * It runs a supervised worker thread holding the broadcast socket and applies
56
+ * each announcement to every registered cluster, skipping servers that are
57
+ * already known. Managers created with the same broadcast settings share one
58
+ * socket and worker thread, reference-counted so the worker lives until the last
59
+ * of them is destroyed; the `logger` and `handleSignals` options are not part of
60
+ * that identity. If the worker dies unexpectedly it is logged and re-spawned
61
+ * after one second with all live managers re-attached, so membership tracking
62
+ * resumes on its own.
63
+ *
64
+ * Announcements are not filtered by queue name: every cluster registered with
65
+ * a manager listening on a given address and port receives every server announced
66
+ * there. Isolate unrelated services by giving them distinct broadcast addresses
67
+ * or ports.
68
+ *
69
+ * Wire format — each announcement is one UDP datagram containing five
70
+ * tab-separated fields: queue name, unique server id, `up` or `down`,
71
+ * `host:port`, and the liveness timeout in seconds. Datagrams with a missing
72
+ * field, a port outside 1-65535, or a negative timeout are ignored. An `up`
73
+ * announcement adds the server and refreshes its liveness deadline; a `down`
74
+ * announcement removes it immediately.
75
+ */
39
76
  export class UDPClusterManager extends ClusterManager {
40
77
  static workers = {};
41
78
  /** Number of manager instances sharing each worker (by worker key) */
@@ -56,6 +93,25 @@ export class UDPClusterManager extends ClusterManager {
56
93
  workerKey;
57
94
  worker;
58
95
  destroyed = false;
96
+ /**
97
+ * Creates the manager and starts listening for broadcasts immediately,
98
+ * joining an existing worker when another manager already uses the same
99
+ * broadcast settings.
100
+ *
101
+ * @param options - partial options merged over
102
+ * {@link DEFAULT_UDP_CLUSTER_MANAGER_OPTIONS}
103
+ *
104
+ * @remarks
105
+ * The merge is a plain shallow spread, so passing an explicit `undefined`
106
+ * overrides a default rather than falling back to it — pass only the
107
+ * properties you mean to change.
108
+ *
109
+ * Broadcast listening begins before any cluster is registered, so register
110
+ * yours with {@link ClusterManager.init} right away: announcements that arrive
111
+ * in the meantime are discarded. When
112
+ * {@link UDPClusterManagerOptions.handleSignals} is enabled, this also
113
+ * installs process-wide signal handlers.
114
+ */
59
115
  constructor(options) {
60
116
  super();
61
117
  this.options = {
@@ -75,8 +131,7 @@ export class UDPClusterManager extends ClusterManager {
75
131
  * depends on, so managers configured differently never silently share
76
132
  * a worker built from another manager's options.
77
133
  *
78
- * @param {UDPClusterManagerOptions} options
79
- * @returns {string}
134
+ * @param options -
80
135
  */
81
136
  static workerKeyFor(options) {
82
137
  return [
@@ -109,8 +164,7 @@ export class UDPClusterManager extends ClusterManager {
109
164
  * Stops all workers and re-raises the given signal, so the process
110
165
  * terminates through the default signal behavior.
111
166
  *
112
- * @param {NodeJS.Signals} signal
113
- * @returns {Promise<void>}
167
+ * @param signal -
114
168
  */
115
169
  static async freeAndRaise(signal) {
116
170
  await UDPClusterManager.free();
@@ -141,8 +195,6 @@ export class UDPClusterManager extends ClusterManager {
141
195
  * Spawns and supervises a UDP worker for this manager's options. On an
142
196
  * unexpected worker death the worker is dropped from the registry, and
143
197
  * a re-spawn is scheduled while live manager instances remain.
144
- *
145
- * @returns {Worker}
146
198
  */
147
199
  spawnWorker() {
148
200
  const workerData = {
@@ -185,7 +237,7 @@ export class UDPClusterManager extends ClusterManager {
185
237
  * live manager instances to it, so cluster membership does not silently
186
238
  * freeze after a worker crash.
187
239
  *
188
- * @param {string} workerKey
240
+ * @param workerKey -
189
241
  */
190
242
  static respawn(workerKey) {
191
243
  const instances = UDPClusterManager.instances[workerKey];
@@ -219,8 +271,7 @@ export class UDPClusterManager extends ClusterManager {
219
271
  * cluster. Cluster callback errors are contained per cluster, so the
220
272
  * worker message listener can never raise an unhandled rejection.
221
273
  *
222
- * @param {WorkerMessage} message
223
- * @returns {Promise<void>}
274
+ * @param message -
224
275
  */
225
276
  async handleWorkerMessage(message) {
226
277
  if (message.type === 'error') {
@@ -241,6 +292,20 @@ export class UDPClusterManager extends ClusterManager {
241
292
  handler?.(server);
242
293
  });
243
294
  }
295
+ /**
296
+ * Stops this manager and releases its share of the broadcast worker.
297
+ *
298
+ * @remarks
299
+ * Safe to call more than once. The shared worker is only shut down when this
300
+ * is the last manager using it — otherwise the call just releases this
301
+ * manager's reference and the worker keeps running for the others. Shutdown
302
+ * asks the worker to close its socket and waits up to five seconds for
303
+ * confirmation before terminating the thread.
304
+ *
305
+ * Process signal handlers installed by this manager are not removed, and
306
+ * registered clusters are not cleared — use {@link ClusterManager.remove} for
307
+ * that, which itself calls this method once the last cluster is gone.
308
+ */
244
309
  async destroy() {
245
310
  if (this.destroyed) {
246
311
  return;
package/src/index.d.ts CHANGED
@@ -24,5 +24,6 @@ export * from './redis.js';
24
24
  export * from './IMQMode.js';
25
25
  export * from './IMessageQueue.js';
26
26
  export * from './RedisQueue.js';
27
+ export * from './ClusterManager.js';
27
28
  export * from './ClusteredRedisQueue.js';
28
29
  export * from './UDPClusterManager.js';
package/src/index.js CHANGED
@@ -24,6 +24,7 @@ export * from './redis.js';
24
24
  export * from './IMQMode.js';
25
25
  export * from './IMessageQueue.js';
26
26
  export * from './RedisQueue.js';
27
+ export * from './ClusterManager.js';
27
28
  export * from './ClusteredRedisQueue.js';
28
29
  export * from './UDPClusterManager.js';
29
30
  //# sourceMappingURL=index.js.map