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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,25 @@ Allserver({
}).start();
```

#### Graceful stop

By default, `stop()` closes the connections of the requests that are still in flight. To let them complete first, give a `timeoutMs`:

```js
const server = Allserver({
procedures,
transport: HttpTransport({ port: process.env.PORT }),
});
await server.start();

process.on("SIGTERM", async () => {
await server.stop({ timeoutMs: 20_000 });
process.exit(0);
});
```

`stop()` resolves when all connections have ended, which can be up to `keepAliveTimeout` (5s by default) after the last request. After `timeoutMs` it closes the connections that are still open, so keep it below the time your platform gives the process to stop.

#### Replying non-standard HTTP response from a procedure

You'd need to deal with node.js `res` yourself.
Expand Down
7 changes: 5 additions & 2 deletions src/server/Allserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,11 @@ module.exports = require("stampit")({
start() {
return this.transport.startServer({ allserver: this });
},
stop() {
return this.transport.stopServer();
/**
* @param [options] {Object} Passed to the transport. HttpTransport accepts `{ timeoutMs }`.
*/
stop(options) {
return this.transport.stopServer(options);
},
},

Expand Down
15 changes: 9 additions & 6 deletions src/server/HttpTransport.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,15 @@ module.exports = require("./Transport").compose({
);
return new Promise((r) => this.server.listen(this.port, r));
},
stopServer() {
return new Promise((r) => {
if (this.server.closeIdleConnections) this.server.closeIdleConnections();
this.server.close(r);
if (this.server.closeAllConnections) this.server.closeAllConnections();
});
/**
* @param [options.timeoutMs=0] {number} How long the requests in flight can take to complete.
* After this time, or with 0, all connections are closed, also the ones with a request in flight.
*/
stopServer({ timeoutMs = 0 } = {}) {
this.server.closeIdleConnections?.();
const timer = setTimeout(() => this.server.closeAllConnections?.(), timeoutMs);
// Stops accepting new connections. Resolves when all connections have ended.
return new Promise((r) => this.server.close(r)).finally(() => clearTimeout(timer));
},

getProcedureName(ctx) {
Expand Down
14 changes: 14 additions & 0 deletions test/server/Allserver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,20 @@ describe("Allserver", () => {
await promise;
assert(stopped);
});

it("should pass the options to transport stopServer()", async () => {
let stopOptions;
const MockedTransport = VoidTransport.methods({
async stopServer(options) {
stopOptions = options;
},
});

const server = Allserver({ transport: MockedTransport() });
await server.stop({ timeoutMs: 100 });

assert.deepEqual(stopOptions, { timeoutMs: 100 });
});
});

describe("introspection", () => {
Expand Down
55 changes: 55 additions & 0 deletions test/server/HttpTransport.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const assert = require("node:assert/strict");
const { describe, it } = require("node:test");
const { setTimeout } = require("node:timers/promises");

const { Allserver, HttpTransport } = require("../..");

const procedures = {
async slow({ ms }) {
await setTimeout(Number(ms));
return "done";
},
};

async function startServer() {
const transport = HttpTransport({ port: 0 });
const server = Allserver({ procedures, transport, logger: { error() {} } });
await server.start();
return { server, url: `http://localhost:${transport.server.address().port}` };
}

describe("HttpTransport", () => {
describe("#stopServer", () => {
it("should complete the requests in flight before it closes the connections", async () => {
const { server, url } = await startServer();
const response = fetch(`${url}/slow?ms=200`).then((res) => res.json());
await setTimeout(50);

await server.stop({ timeoutMs: 1000 });

assert.equal((await response).slow, "done");
});

it("should close the connections of the requests in flight without a timeout", async () => {
const { server, url } = await startServer();
const response = fetch(`${url}/slow?ms=200`);
await setTimeout(50);

await server.stop();

await assert.rejects(response);
});

it("should close the connections of the requests in flight after the timeout", async () => {
const { server, url } = await startServer();
const response = fetch(`${url}/slow?ms=2000`);
await setTimeout(50);

const start = Date.now();
await server.stop({ timeoutMs: 200 });

assert(Date.now() - start < 1000);
await assert.rejects(response);
});
});
});
Loading