diff --git a/README.md b/README.md index adc7b2e..8f78fec 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/server/Allserver.js b/src/server/Allserver.js index 62002d2..dadb1f2 100644 --- a/src/server/Allserver.js +++ b/src/server/Allserver.js @@ -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); }, }, diff --git a/src/server/HttpTransport.js b/src/server/HttpTransport.js index 4828f27..4540f90 100644 --- a/src/server/HttpTransport.js +++ b/src/server/HttpTransport.js @@ -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) { diff --git a/test/server/Allserver.test.js b/test/server/Allserver.test.js index 550eaed..9590456 100644 --- a/test/server/Allserver.test.js +++ b/test/server/Allserver.test.js @@ -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", () => { diff --git a/test/server/HttpTransport.test.js b/test/server/HttpTransport.test.js new file mode 100644 index 0000000..21578a9 --- /dev/null +++ b/test/server/HttpTransport.test.js @@ -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); + }); + }); +});