diff --git a/README.md b/README.md index ca0a906..bb1e115 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,24 @@ memory and is lost, while approvals live in SQLite and survive. A gateway has to treat "approval still pending after a restart" as a run it must start again. +## Versions and the channel + +The dependencies are pinned exactly, not by range. This package is +consumed by a handful of services rather than by a library +ecosystem, and a surprise minor of grpc-js is the kind of thing that +breaks a portal at three in the morning. Raising one is a commit, and +the skew job runs against a released hub before it lands. + +The client sets two channel options by default: +`grpc.initial_reconnect_backoff_ms: 200` and +`grpc.max_reconnect_backoff_ms: 5000`. grpc-js otherwise waits a +second before its second reconnect attempt and backs off towards two +minutes, which is right for a far-away service and wrong for a hub on +the same network: after a restart or a deploy, a gateway would sit +out that backoff with requests in hand. Anything in +`channelOptions` wins over these, and `client.channelOptions` says +what the client ended up with. + ## Development ```bash diff --git a/package.json b/package.json index 6f70d3c..0873c20 100644 --- a/package.json +++ b/package.json @@ -32,11 +32,11 @@ "check": "tsc -p tsconfig.json --noEmit && tsc -p tsconfig.test.json --noEmit" }, "dependencies": { - "@grpc/grpc-js": "^1.14.0", - "@grpc/proto-loader": "^0.8.0" + "@grpc/grpc-js": "1.14.6", + "@grpc/proto-loader": "0.8.1" }, "devDependencies": { - "@types/node": "^22.10.0", - "typescript": "^5.9.0" + "@types/node": "22.20.5", + "typescript": "5.9.3" } } diff --git a/src/client.ts b/src/client.ts index 286659c..1d20218 100644 --- a/src/client.ts +++ b/src/client.ts @@ -39,7 +39,10 @@ export interface ChainClientOptions { project?: string; /** Deadline for a single call, in milliseconds. Default 30000. */ deadlineMs?: number; - /** Passed through to grpc-js, e.g. message size limits. */ + /** + * Passed through to grpc-js, e.g. message size limits. What is set + * here wins over the defaults below. + */ channelOptions?: grpc.ChannelOptions; } @@ -156,6 +159,14 @@ const DEFAULT_DEADLINE_MS = 30_000; /** A connection to one hub. */ export class ChainClient { + /** + * The channel options this client was built with: its own defaults + * with the caller's on top. Readable so a service can assert what + * it is running with, which is the only way to tell a default that + * applies from one that was silently replaced. + */ + readonly channelOptions: grpc.ChannelOptions; + private readonly hub: grpc.Client; private readonly admin: grpc.Client; private readonly token: string | undefined; @@ -170,9 +181,20 @@ export class ChainClient { options.tls === true ? undefined : options.tls.certChain, ) : grpc.credentials.createInsecure(); - const channelOptions = options.channelOptions ?? {}; + // A hub that comes back should be usable again at once. grpc-js + // waits a second before the first reconnect and then backs off + // towards two minutes, which is right for a far-away service and + // wrong for a hub on the same network: after a restart or a + // deploy, a gateway would sit out the backoff with requests in + // hand. The caller's own options still win. + const channelOptions: grpc.ChannelOptions = { + 'grpc.initial_reconnect_backoff_ms': 200, + 'grpc.max_reconnect_backoff_ms': 5_000, + ...(options.channelOptions ?? {}), + }; const Hub = hubServiceConstructor(); const HubAdmin = hubAdminServiceConstructor(); + this.channelOptions = channelOptions; this.hub = new Hub(options.address, credentials, channelOptions); this.admin = new HubAdmin(options.address, credentials, channelOptions); this.token = options.token; diff --git a/test/client_against_fake.test.ts b/test/client_against_fake.test.ts index bb01648..3271ce4 100644 --- a/test/client_against_fake.test.ts +++ b/test/client_against_fake.test.ts @@ -275,3 +275,43 @@ async function waitFor(produce: () => Promise, timeoutMs = 3_0 await new Promise((resolve) => setTimeout(resolve, 20)); } } + +describe('the channel after a hub comes back', () => { + it('is built to retry soon, not after a second', () => { + // grpc-js waits a second before its second reconnect attempt and + // backs off towards two minutes. That is right for a far-away + // service and wrong for a hub on the same network: after a + // restart or a deploy, a gateway would sit out the backoff with + // requests in hand. + const client = new ChainClient({ address: '127.0.0.1:1' }); + assert.equal(client.channelOptions['grpc.initial_reconnect_backoff_ms'], 200); + assert.equal(client.channelOptions['grpc.max_reconnect_backoff_ms'], 5_000); + client.close(); + }); + + it('lets a caller override what the client set', () => { + const client = new ChainClient({ + address: '127.0.0.1:1', + channelOptions: { 'grpc.initial_reconnect_backoff_ms': 1_234 }, + }); + assert.equal(client.channelOptions['grpc.initial_reconnect_backoff_ms'], 1_234); + // And the other default is still there. + assert.equal(client.channelOptions['grpc.max_reconnect_backoff_ms'], 5_000); + client.close(); + }); + + it('works again once the hub is back', async () => { + const hub = new FakeHub(); + const address = await hub.start(); + const client = new ChainClient({ address, deadlineMs: 5_000 }); + await client.health(); + + await hub.restart(); + hub.succeeds({ report: 'wieder da' }); + const result = await client.submit({ flowYaml: FLOW }); + assert.equal(asText(result.outputs.report), 'wieder da'); + + client.close(); + await hub.close(); + }); +});