@imqueue/rpc 3.9.4 → 3.10.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,10 +10,26 @@ 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]
13
+ ## [3.10.0] - 2026-11-12
14
14
 
15
15
  ### Added
16
16
 
17
+ - **Where a call spent its time.** A service now stamps every response with
18
+ `timing` — when it took the request off the queue, entered the method and
19
+ left it, by its own clock — and a client stamps every request with `sentAt`
20
+ just before sending it. `callTiming(request, response)` turns the pair into
21
+ `total`, `handled`, `waited` (in the service, before the method ran) and
22
+ `transit` (both trips through the queue and the caller's own wait to read
23
+ the answer). Each figure is a difference of two readings of one clock, so
24
+ hosts whose clocks disagree move none of them. Call it from an `afterCall`
25
+ hook. Both fields are optional on the wire: an older client ignores
26
+ `timing`, an older service ignores `sentAt`, and `callTiming` answers `null`
27
+ where either half is missing.
28
+
29
+ Measured on a production-like fleet, a call's round trip minus its handler
30
+ was 4 ms typical, 33 ms at p95 and 152 ms at p99, and nothing said which part
31
+ of the trip it was.
32
+
17
33
  - **TLS on the cache connection.** `RedisCache.init()` now honours `tls`, which
18
34
  it inherited from the queue option shape and silently ignored. `true`
19
35
  connects with Node's defaults, an object is handed to `tls.connect()` as
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@imqueue/rpc",
3
- "version": "3.9.4",
3
+ "version": "3.10.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",
@@ -52,7 +52,7 @@
52
52
  "author": "imqueue.com <support@imqueue.com> (https://imqueue.com)",
53
53
  "license": "GPL-3.0-only",
54
54
  "dependencies": {
55
- "@imqueue/core": "^3.5.3",
55
+ "@imqueue/core": "^3.5.4",
56
56
  "acorn": "^8.17.0",
57
57
  "typescript": "^7.0.2"
58
58
  },
@@ -0,0 +1,59 @@
1
+ /*!
2
+ * IMQ-RPC: where a remote call spent its time
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
+ import type { IMQRPCRequest } from './IMQRPCRequest.js';
25
+ import type { IMQRPCResponse } from './IMQRPCResponse.js';
26
+ /**
27
+ * One call's round trip, split into where it went, in milliseconds.
28
+ */
29
+ export interface IMQCallTiming {
30
+ /** From handing the call to the queue until its answer arrived. */
31
+ total: number;
32
+ /** Inside the service's method. */
33
+ handled: number;
34
+ /** In the service between leaving the queue and entering the method. */
35
+ waited: number;
36
+ /**
37
+ * Everything else: both trips through the queue, and the caller's own wait
38
+ * to read the answer.
39
+ */
40
+ transit: number;
41
+ }
42
+ /**
43
+ * Splits a call's round trip from the request a client sent and the response
44
+ * it was answered with.
45
+ *
46
+ * @param request - the request as the client sent it, carrying `sentAt`
47
+ * @param response - the response as it arrived, carrying `timing`
48
+ * @param answeredAt - when the answer arrived, by the caller's clock
49
+ * @returns the split, or `null` where either side did not record its half
50
+ *
51
+ * @remarks
52
+ * Each figure is a difference of two readings of one clock — `total` the
53
+ * caller's, `handled` and `waited` the service's — so clocks that disagree
54
+ * between hosts move no figure. `transit` is what is left, and is where a
55
+ * busy caller, a busy broker and the size of a message show.
56
+ *
57
+ * Call it from an `afterCall` hook, which runs as the answer arrives.
58
+ */
59
+ export declare function callTiming(request: Partial<IMQRPCRequest> | undefined, response: Partial<IMQRPCResponse> | undefined, answeredAt?: number): IMQCallTiming | null;
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Splits a call's round trip from the request a client sent and the response
3
+ * it was answered with.
4
+ *
5
+ * @param request - the request as the client sent it, carrying `sentAt`
6
+ * @param response - the response as it arrived, carrying `timing`
7
+ * @param answeredAt - when the answer arrived, by the caller's clock
8
+ * @returns the split, or `null` where either side did not record its half
9
+ *
10
+ * @remarks
11
+ * Each figure is a difference of two readings of one clock — `total` the
12
+ * caller's, `handled` and `waited` the service's — so clocks that disagree
13
+ * between hosts move no figure. `transit` is what is left, and is where a
14
+ * busy caller, a busy broker and the size of a message show.
15
+ *
16
+ * Call it from an `afterCall` hook, which runs as the answer arrives.
17
+ */
18
+ export function callTiming(request, response, answeredAt = Date.now()) {
19
+ const sentAt = request?.sentAt;
20
+ const timing = response?.timing;
21
+ if (typeof sentAt !== 'number' || !timing) {
22
+ return null;
23
+ }
24
+ const total = Math.max(0, answeredAt - sentAt);
25
+ const handled = Math.max(0, timing.finished - timing.started);
26
+ const waited = Math.max(0, timing.started - timing.received);
27
+ return {
28
+ total,
29
+ handled,
30
+ waited,
31
+ transit: Math.max(0, total - handled - waited),
32
+ };
33
+ }
34
+ //# sourceMappingURL=IMQCallTiming.js.map
package/src/IMQClient.js CHANGED
@@ -349,6 +349,7 @@ let IMQClient = (() => {
349
349
  };
350
350
  void (async () => {
351
351
  try {
352
+ request.sentAt = Date.now();
352
353
  const id = await this.imq.send(to, request, delay, doReject);
353
354
  this.resolvers[id] = [
354
355
  imqCallResolver(doResolve, request, this),
@@ -69,4 +69,14 @@ export interface IMQRPCRequest extends JsonObject {
69
69
  * caller's bag, so it is a plain object, not an {@link IMQMetadata} instance.
70
70
  */
71
71
  metadata?: IMQMetadata;
72
+ /**
73
+ * When the client handed the call to the queue, in milliseconds since the
74
+ * epoch by the caller's clock.
75
+ *
76
+ * @remarks
77
+ * Set by the client immediately before sending, after `beforeCall` ran.
78
+ * Read back on the client only, so the service's clock never meets it;
79
+ * see {@link callTiming}.
80
+ */
81
+ sentAt?: number;
72
82
  }
@@ -66,4 +66,26 @@ export interface IMQRPCResponse extends JsonObject {
66
66
  * this round-trips every argument value back to the caller.
67
67
  */
68
68
  request: IMQRPCRequest;
69
+ /**
70
+ * When the service took the request off the queue, started the method and
71
+ * finished it, in milliseconds since the epoch by the service's clock.
72
+ *
73
+ * @remarks
74
+ * Absent on a response from a service older than this field. Only the
75
+ * differences between its own values mean anything to a caller, because
76
+ * the two clocks are not the same clock; {@link callTiming} combines them
77
+ * with the caller's own round trip without comparing one with the other.
78
+ */
79
+ timing?: IMQRPCTiming;
80
+ }
81
+ /**
82
+ * Where a service spent a call, by its own clock.
83
+ */
84
+ export interface IMQRPCTiming extends JsonObject {
85
+ /** When the message was taken off the queue. */
86
+ received: number;
87
+ /** When the method was entered, after the service's `beforeCall` ran. */
88
+ started: number;
89
+ /** When the method returned or threw. */
90
+ finished: number;
69
91
  }
@@ -226,6 +226,7 @@ export declare abstract class IMQService {
226
226
  *
227
227
  * @param request - request message
228
228
  * @param id - message unique identifier
229
+ * @param received - when the message was taken off the queue
229
230
  */
230
231
  private handleRequest;
231
232
  private processRequest;
package/src/IMQService.js CHANGED
@@ -316,7 +316,7 @@ let IMQService = (() => {
316
316
  // the response (e.g. the broker went away mid-reply) must be logged,
317
317
  // not surface as an unhandled rejection and crash the process
318
318
  this.imq.on('message', ((request, id) => {
319
- const handled = this.handleRequest(request, id).catch(err => this.logger.error(`${this.name}: error handling request:`, err));
319
+ const handled = this.handleRequest(request, id, Date.now()).catch(err => this.logger.error(`${this.name}: error handling request:`, err));
320
320
  // fast path with draining off: no set, no tracking, no
321
321
  // allocation beyond what this dispatch already did
322
322
  return this.inFlight ? this.track(handled) : handled;
@@ -430,11 +430,12 @@ let IMQService = (() => {
430
430
  *
431
431
  * @param request - request message
432
432
  * @param id - message unique identifier
433
+ * @param received - when the message was taken off the queue
433
434
  */
434
- async handleRequest(request, id) {
435
- return runWithRequest(request, () => this.processRequest(request, id));
435
+ async handleRequest(request, id, received = Date.now()) {
436
+ return runWithRequest(request, () => this.processRequest(request, id, received));
436
437
  }
437
- async processRequest(request, id) {
438
+ async processRequest(request, id, received) {
438
439
  const logger = this.options.logger || console;
439
440
  const method = request.method;
440
441
  const description = await this.describe();
@@ -476,8 +477,11 @@ let IMQService = (() => {
476
477
  }
477
478
  if (response.error) {
478
479
  this.logger.warn(response.error);
480
+ const now = Date.now();
481
+ response.timing = { received, started: now, finished: now };
479
482
  return await send(request, response, this);
480
483
  }
484
+ const started = Date.now();
481
485
  try {
482
486
  // Run the method through the optional `wrapCall` around-hook so a
483
487
  // hook can execute it within its own scope (e.g. an OpenTelemetry
@@ -496,6 +500,7 @@ let IMQService = (() => {
496
500
  catch (err) {
497
501
  response.error = IMQError(err.code || 'IMQ_RPC_CALL_ERROR', err.message, err.stack, method, args, err);
498
502
  }
503
+ response.timing = { received, started, finished: Date.now() };
499
504
  return await send(request, response, this);
500
505
  }
501
506
  /**
package/src/index.d.ts CHANGED
@@ -28,6 +28,7 @@ export * from './IMQRPCDescription.js';
28
28
  export * from './IMQRPCError.js';
29
29
  export * from './IMQRPCRequest.js';
30
30
  export * from './IMQRPCResponse.js';
31
+ export * from './IMQCallTiming.js';
31
32
  export * from './IMQLock.js';
32
33
  export * from './IMQService.js';
33
34
  export * from './IMQClient.js';
package/src/index.js CHANGED
@@ -28,6 +28,7 @@ export * from './IMQRPCDescription.js';
28
28
  export * from './IMQRPCError.js';
29
29
  export * from './IMQRPCRequest.js';
30
30
  export * from './IMQRPCResponse.js';
31
+ export * from './IMQCallTiming.js';
31
32
  export * from './IMQLock.js';
32
33
  export * from './IMQService.js';
33
34
  export * from './IMQClient.js';