Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions __tests__/drain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import {
createClient,
createServer,
createServiceSchema,
Err,
Middleware,
Ok,
Procedure,
UNCAUGHT_ERROR_CODE,
} from '../router';
import { createMockTransportNetwork, readNextResult } from '../testUtil';
import { waitFor } from '../testUtil/fixtures/cleanup';
Expand Down Expand Up @@ -62,6 +64,19 @@ function setup(
resWritable.write(Ok(null));
},
}),
claim: Procedure.rpc({
requestInit: init,
responseData,
responseError: Type.Object({
code: Type.Literal('SERVER_DRAINING'),
message: Type.String(),
}),
async handler() {
effects.push('claim');

return Err({ code: 'SERVER_DRAINING', message: 'upstream' });
},
}),
}),
};
function startServer() {
Expand Down Expand Up @@ -279,3 +294,18 @@ test('a refused upload is returned without replay', async () => {
expect(await f.client.test.upload.upload({}).finalize()).toEqual(refusal);
expect(f.effects).toEqual(['hold']);
});

test('a handler that returns the reserved drain code is not resent', async () => {
// Risk: a handler that already ran is replayed on the next session.
const f = setup(1);
const result = f.client.test.claim.rpc({});
await waitFor(() => expect(f.effects).toEqual(['claim']));
await vi.advanceTimersByTimeAsync(10);
f.reconnect();
await vi.advanceTimersByTimeAsync(20);
expect(f.effects).toEqual(['claim']);
expect(await result).toMatchObject({
ok: false,
payload: { code: UNCAUGHT_ERROR_CODE, message: 'upstream' },
});
});
11 changes: 11 additions & 0 deletions router/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,17 @@ class RiverServer<
Result<Static<PayloadType>, Static<BaseErrorSchemaType>>
>({
writeCb: (response) => {
if (!response.ok && response.payload.code === SERVER_DRAINING_CODE) {
this.log?.warn(
`${serviceName}.${procedureName} handler returned the reserved ${SERVER_DRAINING_CODE} code`,
loggingMetadata,
);
response = Err({
code: UNCAUGHT_ERROR_CODE,
message: response.payload.message,
});
}

if (!response.ok) {
recordRiverError(span, response.payload);
}
Expand Down
Loading