@imqueue/rpc 3.11.0 → 3.12.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 CHANGED
@@ -10,6 +10,37 @@ raised `@imqueue/core` floor is how most fixes in the transport reach a service.
10
10
 
11
11
  This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
12
12
 
13
+ ## [Unreleased]
14
+
15
+ ### Fixed
16
+
17
+ - **A client honours `handleSignals: false`.** `IMQClient` installed its own
18
+ `SIGTERM`/`SIGINT`/`SIGHUP`/`SIGQUIT` handlers regardless of the option, and
19
+ those destroy the client — abandoning the calls still pending — and exit the
20
+ process after `IMQ_SHUTDOWN_TIMEOUT`. The option only reached the queue
21
+ layer, so a process that owns its shutdown, such as an HTTP gateway holding
22
+ clients, could not finish one: measured on a gateway under 50 req/s of
23
+ 300 ms calls, a `SIGTERM` cut 15 requests mid-flight with the gateway's own
24
+ close-then-destroy handler installed and `handleSignals: false` set. Now
25
+ `handleSignals: false` means neither the client nor its queue installs a
26
+ handler, and the same gateway stops with none cut. The default is unchanged.
27
+ - **A service honours it the same way.** `IMQService` with
28
+ `handleSignals: false` no longer installs its fixed-timer signal handlers
29
+ either. A drain is an opt-in of its own, so with draining on `SIGTERM` and
30
+ `SIGINT` still start it; `drain: false` hands those to the host too.
31
+
32
+ ### Changed
33
+
34
+ - A service or client that passed `handleSignals: false` and relied on this
35
+ package's handlers to exit the process on a signal no longer gets them. With
36
+ no handler of the host's own, Node's default applies to `SIGTERM`, `SIGINT`
37
+ and `SIGHUP` — the process ends at once, without `destroy()`; install a
38
+ handler that calls `destroy()` and exits, or leave the option at its
39
+ default.
40
+ - With draining enabled in the process, a client still installs its own
41
+ handlers for the drain to take over, as before — unless the host passed
42
+ `handleSignals: false` itself, in which case it installs none.
43
+
13
44
  ## [3.11.0] - 2026-11-12
14
45
 
15
46
  ### Added
package/README.md CHANGED
@@ -330,6 +330,74 @@ Delivery remains **at-least-once** either way. A drain narrows the window in
330
330
  which in-flight work is lost; `SIGKILL`, an OOM kill or a lost node still take
331
331
  it, so handlers must stay idempotent.
332
332
 
333
+ ### A host that owns shutdown
334
+
335
+ A client installs signal handlers too: on `SIGTERM`, `SIGINT`, `SIGHUP` or
336
+ `SIGQUIT` it destroys itself — abandoning the calls still waiting for an
337
+ answer — and exits the process after `IMQ_SHUTDOWN_TIMEOUT`. In a process whose
338
+ job is something else, such as an HTTP gateway holding clients, that exit cuts
339
+ the host's own shutdown short. Pass `handleSignals: false` and neither the
340
+ client nor its queue installs a handler; the host stops taking work, lets what
341
+ it accepted finish, and only then destroys the client:
342
+
343
+ ~~~typescript
344
+ import express from 'express';
345
+ import { userService } from './clients/UserService.js';
346
+
347
+ const client = new userService.UserClient({
348
+ handleSignals: false, // this process's shutdown is the gateway's
349
+ callTimeout: 5000,
350
+ });
351
+
352
+ await client.start();
353
+
354
+ let stopping = false;
355
+ const app = express();
356
+
357
+ app.use((_req, res, next) => {
358
+ // once stopping, every answer closes its connection, so keep-alive
359
+ // callers reconnect to another instance instead of holding this one open
360
+ if (stopping) {
361
+ res.set('Connection', 'close');
362
+ }
363
+
364
+ next();
365
+ });
366
+ app.get('/ready', (_req, res) => res.status(stopping ? 503 : 200).end());
367
+ app.get('/users/:id', async (req, res) => {
368
+ res.json(await client.get(req.params.id));
369
+ });
370
+
371
+ const server = app.listen(8080);
372
+
373
+ async function shutdown(): Promise<void> {
374
+ stopping = true;
375
+
376
+ // accept no new connection; resolves once the last open one has ended
377
+ const closed = new Promise<void>(resolve => server.close(() => resolve()));
378
+
379
+ server.closeIdleConnections();
380
+
381
+ // stay inside the orchestrator's grace period whatever the callers do
382
+ const deadline = setTimeout(() => server.closeAllConnections(), 10_000);
383
+
384
+ await closed; // every request already accepted has been answered
385
+ clearTimeout(deadline);
386
+ await client.destroy(); // so no call is left in flight
387
+ process.exit(0);
388
+ }
389
+
390
+ // `once`: a second signal finds no handler and ends the process at once
391
+ process.once('SIGTERM', shutdown);
392
+ process.once('SIGINT', shutdown);
393
+ ~~~
394
+
395
+ `handleSignals: false` has to be on every client and service in the process:
396
+ the handlers are process-wide, so any one that leaves the default in place
397
+ still exits the process on a signal. On a service the option stops the
398
+ fixed-timer handlers the same way, except that a drain, when enabled, still
399
+ handles `SIGTERM`/`SIGINT` — add `drain: false` to own those as well.
400
+
333
401
  ## Serving a service in process
334
402
 
335
403
  A service may also run inside the process of its caller. Start it with
@@ -374,7 +442,10 @@ What differs:
374
442
  it and the call timed out.
375
443
  - A service served in process belongs to its host: it installs no signal
376
444
  handlers, does not drain, starts no metrics server and cannot fork workers
377
- (`multiProcess`). Destroy it from the host's own shutdown.
445
+ (`multiProcess`). Destroy it from the host's own shutdown — and give the
446
+ host's clients `handleSignals: false` too (see
447
+ [A host that owns shutdown](#a-host-that-owns-shutdown)), or theirs still
448
+ exit the process first.
378
449
  - A client with `local: false` sends every call through the queue regardless.
379
450
 
380
451
  The service and the client need not share one copy of this package — a host
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "3.11.0",
3
+ "version": "3.12.0",
4
4
  "description": "Type-safe RPC framework for Node.js & TypeScript microservices over a Redis message queue — self-describing services generate their own strongly-typed clients (@imqueue framework)",
5
5
  "keywords": [
6
6
  "redis",
@@ -127,8 +127,22 @@ export declare abstract class IMQClient extends EventEmitter {
127
127
  * file under `$TMPDIR/.imq-rpc`, opens its message queue (two in `singleQueue`
128
128
  * mode), raises the process max-listener limit once per process, and installs
129
129
  * `SIGTERM`/`SIGINT`/`SIGHUP`/`SIGQUIT` handlers that destroy the client and
130
- * then exit the process after `IMQ_SHUTDOWN_TIMEOUT`. Any library embedding
131
- * a client inherits that process-terminating behaviour.
130
+ * then exit the process after `IMQ_SHUTDOWN_TIMEOUT`, without waiting for
131
+ * calls still in flight. Any library embedding a client inherits that
132
+ * process-terminating behaviour.
133
+ *
134
+ * Pass `handleSignals: false` when the host owns the process's shutdown —
135
+ * an HTTP gateway holding clients, say. Then neither the client nor its
136
+ * queue installs a signal handler, and the host's own handler decides when
137
+ * to call {@link IMQClient.destroy}: typically after it has stopped taking
138
+ * new work and the calls already made have settled. The option is
139
+ * process-wide in effect, not per client — any client or service in the
140
+ * process that leaves it on still exits the process on a signal.
141
+ *
142
+ * With draining enabled in this process — a service's `drain` option or
143
+ * `IMQ_DRAIN_ENABLE=1` — the client's queue is kept from installing
144
+ * handlers, but the client still installs its own unless the host passed
145
+ * `handleSignals: false` itself; the drain takes those over.
132
146
  *
133
147
  * Only {@link IMQClient.destroy} releases the pid file and removes those
134
148
  * handlers.
package/src/IMQClient.js CHANGED
@@ -187,8 +187,22 @@ let IMQClient = (() => {
187
187
  * file under `$TMPDIR/.imq-rpc`, opens its message queue (two in `singleQueue`
188
188
  * mode), raises the process max-listener limit once per process, and installs
189
189
  * `SIGTERM`/`SIGINT`/`SIGHUP`/`SIGQUIT` handlers that destroy the client and
190
- * then exit the process after `IMQ_SHUTDOWN_TIMEOUT`. Any library embedding
191
- * a client inherits that process-terminating behaviour.
190
+ * then exit the process after `IMQ_SHUTDOWN_TIMEOUT`, without waiting for
191
+ * calls still in flight. Any library embedding a client inherits that
192
+ * process-terminating behaviour.
193
+ *
194
+ * Pass `handleSignals: false` when the host owns the process's shutdown —
195
+ * an HTTP gateway holding clients, say. Then neither the client nor its
196
+ * queue installs a signal handler, and the host's own handler decides when
197
+ * to call {@link IMQClient.destroy}: typically after it has stopped taking
198
+ * new work and the calls already made have settled. The option is
199
+ * process-wide in effect, not per client — any client or service in the
200
+ * process that leaves it on still exits the process on a signal.
201
+ *
202
+ * With draining enabled in this process — a service's `drain` option or
203
+ * `IMQ_DRAIN_ENABLE=1` — the client's queue is kept from installing
204
+ * handlers, but the client still installs its own unless the host passed
205
+ * `handleSignals: false` itself; the drain takes those over.
192
206
  *
193
207
  * Only {@link IMQClient.destroy} releases the pid file and removes those
194
208
  * handlers.
@@ -202,6 +216,10 @@ let IMQClient = (() => {
202
216
  'be instantiated directly!');
203
217
  }
204
218
  this.options = { ...DEFAULT_IMQ_CLIENT_OPTIONS, ...options };
219
+ // read before the drain below rewrites the option: only the host's
220
+ // own `false` means it owns shutdown. A drain forcing it off for the
221
+ // queue still wants these handlers here, to take them over.
222
+ const ownSignalHandlers = this.options.handleSignals !== false;
205
223
  if (isProcessDrainEnabled()) {
206
224
  // a client's queue would otherwise install the queue layer's own
207
225
  // signal handlers, which exit the process without waiting — enough
@@ -225,7 +243,7 @@ let IMQClient = (() => {
225
243
  IMQClient.maxListenersBumped = true;
226
244
  process.setMaxListeners(10000);
227
245
  }
228
- SIGNALS.forEach((signal) => {
246
+ (ownSignalHandlers ? SIGNALS : []).forEach((signal) => {
229
247
  const handler = () => {
230
248
  this.destroy().catch(this.logger.error);
231
249
  setTimeout(() => process.exit(0), IMQ_SHUTDOWN_TIMEOUT);
@@ -231,6 +231,27 @@ export interface IMQServiceOptions extends IMQOptions {
231
231
  * abandoned, logged, and the process exits `0` regardless.
232
232
  */
233
233
  drainTimeout?: number;
234
+ /**
235
+ * Let this package handle `SIGTERM`, `SIGINT`, `SIGHUP` and `SIGQUIT` for
236
+ * the process. Set it to `false` when the host owns shutdown.
237
+ *
238
+ * @defaultValue true
239
+ *
240
+ * @remarks
241
+ * Left on, the service installs handlers that tear it down and exit the
242
+ * process after `IMQ_SHUTDOWN_TIMEOUT`, and its queue installs its own
243
+ * (see the core `IMQOptions.handleSignals`).
244
+ *
245
+ * `false` stops both: the service installs no such handler and its queue
246
+ * none either, so the host's own handler decides when to call
247
+ * `destroy()`. With {@link IMQServiceOptions.drain} on, `SIGTERM` and
248
+ * `SIGINT` still start the drain — that is an opt-in of its own — so a
249
+ * host that owns those signals too leaves the drain off.
250
+ *
251
+ * Handlers are process-wide: any other service or client in the process
252
+ * that leaves this on still exits the process on a signal.
253
+ */
254
+ handleSignals?: boolean;
234
255
  /**
235
256
  * Serve this service in process as well: a client in the same process
236
257
  * calls it directly, with no trip through the queue.
@@ -343,6 +364,32 @@ export interface IMQClientOptions extends IMQOptions {
343
364
  * Use it to bound Redis connections when a process instantiates many clients.
344
365
  */
345
366
  singleQueue?: boolean;
367
+ /**
368
+ * Let this package handle `SIGTERM`, `SIGINT`, `SIGHUP` and `SIGQUIT` for
369
+ * the process. Set it to `false` when the host owns shutdown — an HTTP
370
+ * gateway holding clients, for one.
371
+ *
372
+ * @defaultValue true
373
+ *
374
+ * @remarks
375
+ * Left on, the client installs handlers that destroy it — abandoning the
376
+ * calls still waiting for an answer — and exit the process after
377
+ * `IMQ_SHUTDOWN_TIMEOUT`, and its queue installs its own (see the core
378
+ * `IMQOptions.handleSignals`). A host's own shutdown handler cannot finish
379
+ * its work before that exit.
380
+ *
381
+ * `false` stops both, so the host decides: stop taking new work, let the
382
+ * calls already made settle, then call `destroy()`.
383
+ *
384
+ * With draining enabled in the process (`IMQ_DRAIN_ENABLE=1`, or a service
385
+ * constructed with `drain`), the queue's handlers are suppressed anyway and
386
+ * the client's own are left for the drain to take over — unless this is
387
+ * `false`, in which case the client installs none.
388
+ *
389
+ * Handlers are process-wide: any other client or service in the process
390
+ * that leaves this on still exits the process on a signal.
391
+ */
392
+ handleSignals?: boolean;
346
393
  /**
347
394
  * Call a service this process serves itself directly, rather than through
348
395
  * the queue.
@@ -173,6 +173,14 @@ export declare abstract class IMQService {
173
173
  * queue layer does not exit mid-drain. Handlers registered by anything
174
174
  * other than this package are never touched.
175
175
  *
176
+ * Pass `handleSignals: false` when the host owns the process's shutdown:
177
+ * then neither the service nor its queue installs a handler for the
178
+ * fixed-timer shutdown above, and the host calls
179
+ * {@link IMQService.destroy} when it is ready. A drain is the exception —
180
+ * an explicit opt-in to this package handling shutdown gracefully — so with
181
+ * draining on, `SIGTERM` and `SIGINT` still start it; pass `drain: false`
182
+ * as well to own those too.
183
+ *
176
184
  * The merged options are passed straight to the queue factory, so `vendor` and
177
185
  * `cluster`/`clusterManagers` select the transport implementation.
178
186
  */
package/src/IMQService.js CHANGED
@@ -258,6 +258,14 @@ let IMQService = (() => {
258
258
  * queue layer does not exit mid-drain. Handlers registered by anything
259
259
  * other than this package are never touched.
260
260
  *
261
+ * Pass `handleSignals: false` when the host owns the process's shutdown:
262
+ * then neither the service nor its queue installs a handler for the
263
+ * fixed-timer shutdown above, and the host calls
264
+ * {@link IMQService.destroy} when it is ready. A drain is the exception —
265
+ * an explicit opt-in to this package handling shutdown gracefully — so with
266
+ * draining on, `SIGTERM` and `SIGINT` still start it; pass `drain: false`
267
+ * as well to own those too.
268
+ *
261
269
  * The merged options are passed straight to the queue factory, so `vendor` and
262
270
  * `cluster`/`clusterManagers` select the transport implementation.
263
271
  */
@@ -276,6 +284,9 @@ let IMQService = (() => {
276
284
  },
277
285
  };
278
286
  this.logger = this.options.logger || /* istanbul ignore next */ console;
287
+ // read before `local` and the drain below rewrite the option: only the
288
+ // host's own `false` means it owns shutdown
289
+ const ownSignalHandlers = this.options.handleSignals !== false;
279
290
  if (this.options.local) {
280
291
  if (this.options.multiProcess) {
281
292
  throw new TypeError(`${this.name}: a service served in process cannot ` +
@@ -321,6 +332,9 @@ let IMQService = (() => {
321
332
  process.on(signal, handler);
322
333
  return;
323
334
  }
335
+ if (!ownSignalHandlers) {
336
+ return;
337
+ }
324
338
  const handler = () => {
325
339
  this.destroy().catch(this.logger.error);
326
340
  if (this.metricsServer) {