Skip to content

A service can be served in process, and its clients there call it directly - #40

Merged
SerhiyGreench merged 1 commit into
masterfrom
in-process-transport
Nov 12, 2026
Merged

SerhiyGreench merged 1 commit into
masterfrom
in-process-transport

Conversation

@SerhiyGreench

@SerhiyGreench SerhiyGreench commented Nov 12, 2026 •

Copy link
Copy Markdown
Member

A service started with local: true registers itself, once started, with every client in its own process, and those clients call it directly: the same checks, hooks (beforeCall, wrapCall, afterCall on both sides) and method as a queued request, with no trip through the queue.

What stays the same

  • The queue is still served, so a client in another process, a delayed call (the queue holds it until it is due) and publish() are unaffected, and the same service runs standalone wherever local is off.
  • The request and the answer are each copied through JSON, as the queue copies them: a Date arrives as its ISO string, a class instance as a plain object, undefined properties are dropped and undefined array items become null, and neither side holds an object of the other's.
  • Errors are the same plain { code, message, ... } objects (instanceof Error is false), callTimeout applies, and callTiming() still splits the call.

What differs, on purpose

  • An answer that cannot be written as JSON (a BigInt, a cycle) rejects at once with IMQ_RPC_CALL_ERROR; through the queue the service failed to send it and the call timed out.
  • A service served in process belongs to its host: no signal handlers, no drain, no metrics server, and it cannot be combined with multiProcess (a TypeError at construction).
  • A client with local: false sends every call through the queue regardless.

How it is found. The registry is anchored on the process (Symbol.for, as the default option singletons are since 3.5.4), not the module, so a host that loads a service from its own directory, with that service's own copy of this package, still reaches it. localService(), localServiceNames() and wireCopy() are exported for a host.

Tests. test/IMQLocal.spec.ts: no queue send on either side, the JSON semantics, no shared objects, error shapes, unknown method, metadata reaching the method, timing, a BigInt answer, delayed calls and local: false going through the queue, the timeout, multiProcess refused, unregistering on destroy(), and no signal handlers installed. npm test: 292 pass.

Measured (a gateway serving two services in process, the same browser suites against the same images): the round trip to an in-process service fell to under a millisecond at the median and about 2 ms at p95, against 2-20 ms and 30-160 ms through Redis. But the service's own work then shares the caller's event loop, so in that deployment pages got no faster. That is a property of the deployment, not of this transport.

…ectly

`local: true` on a service registers it, once started, with every client in
its own process. Those clients call it through the same checks, hooks and
method as a queued request, with no trip through the queue: the request and
the answer are each copied through JSON, as the queue copies them, so dates,
class instances and `undefined` arrive exactly as they would have, and
neither side holds an object of the other's.

The queue is still served, so a client in another process, a delayed call
(which the queue holds until it is due) and `publish()` are unaffected, and
the same service runs standalone wherever `local` is off. A client's
`local: false` keeps it on the queue.

The registry is anchored on the process, not the module, so a host that
loads a service from its own directory, with that service's own copy of this
package, still reaches it. A service served in process belongs to its host:
it installs no signal handlers, does not drain, starts no metrics server and
cannot fork workers.

An answer that cannot be written as JSON rejects at once with
IMQ_RPC_CALL_ERROR, where through the queue the call timed out.
@SerhiyGreench
SerhiyGreench merged commit 0b3c236 into master Nov 12, 2026
11 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant