Skip to content

Commit de79cad

Browse files
committed
feat: add ability to have sync close
* Add syncClose boolean option Issue: #27
1 parent 643071a commit de79cad

File tree

5 files changed

+14
-4
lines changed

5 files changed

+14
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,8 @@ where `State` is an enum that contains, `STARTING`, `READY`, `SHUTTING_DOWN` and
319319
All of the below options are optional.
320320

321321
| Name | Type | Default | Description |
322-
| ----------------- | :------------------------: | :-----: | ---------------------------------------------------------------: |
322+
|-------------------|:--------------------------:|:-------:|-----------------------------------------------------------------:|
323+
| syncClose | boolean | false | Run the closePromises in a series. |
323324
| closePromises | (() => Promise<unknown>)[] | [] | The functions to run when the API is stopping |
324325
| timeout | number | 1000 | The time in milliseconds to wait before shutting down the server |
325326
| healthCheck | boolean | true | Enable/Disable the default endpoints (liveness and readiness) |

src/config/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { IGracefulServerOptions } from "#interface/gracefulServerOptions";
22
import type { IOptions } from "#interface/options";
33

44
const options: IOptions = {
5+
syncClose: false,
56
closePromises: [],
67
timeout: 1000,
78
healthCheck: true,

src/core/improvedServer.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ const improvedServer = <TServer extends Server>(server: TServer, serverStatus: I
7474

7575
stopping = true;
7676

77-
const { timeout, closePromises } = config;
77+
const { timeout, closePromises, syncClose } = config;
7878

7979
let error: Error | undefined;
8080
if (args.body && args.body.message) {
@@ -87,14 +87,20 @@ const improvedServer = <TServer extends Server>(server: TServer, serverStatus: I
8787

8888
await sleep(timeout);
8989

90-
await Promise.all(closePromises.map((closePromise) => closePromise()));
90+
if (syncClose) {
91+
for (const closePromise of closePromises) {
92+
await closePromise();
93+
}
94+
} else {
95+
await Promise.allSettled(closePromises.map((closePromise) => closePromise()));
96+
}
9197

9298
server.removeAllListeners("request");
9399
server.on("request", (_: http.IncomingMessage, res: http.ServerResponse) => {
94100
if (!res.headersSent) res.setHeader("connection", "close");
95101
});
96102

97-
await Promise.all([socketsPool.closeAll(), secureSocketsPool.closeAll()]);
103+
await Promise.allSettled([socketsPool.closeAll(), secureSocketsPool.closeAll()]);
98104

99105
await new Promise((resolve, reject) => {
100106
server.close((err) => {

src/interface/gracefulServerOptions.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type IGracefulServerOptions = {
2+
syncClose?: boolean;
23
closePromises?: (() => Promise<unknown>)[];
34
timeout?: number;
45
healthCheck?: boolean;

src/interface/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type IOptions = {
2+
syncClose: boolean;
23
closePromises: (() => Promise<unknown>)[];
34
timeout: number;
45
healthCheck: boolean;

0 commit comments

Comments
 (0)