-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathindex.js
311 lines (263 loc) · 8.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict';
// =============================================================================
// _
// |_ _|_ _|_ ._ __ _ ._ _. _ _ _|_ | __ _ |_ _|_ _| _ ._
// | | |_ |_ |_) (_| | (_| (_ (/_ | |_| | _> | | |_| |_ (_| (_) \/\/ | |
// | _|
// -----------------------------------------------------------------------------
// gracefully shuts downs http server
// can be used with http, express, koa, ...
// (c) 2023 Sebastian Hildebrandt
// License: MIT
// =============================================================================
const debug = require('debug')('http-graceful-shutdown');
const http = require('http');
/**
* Gracefully shuts down `server` when the process receives
* the passed signals
*
* @param {http.Server} server
* @param {object} opts
* signals: string (each signal separated by SPACE)
* timeout: timeout value for forceful shutdown in ms
* forceExit: force process.exit() - otherwise just let event loop clear
* development: boolean value (if true, no graceful shutdown to speed up development
* preShutdown: optional function. Needs to return a promise. - HTTP sockets are still available and untouched
* onShutdown: optional function. Needs to return a promise.
* finally: optional function, handled at the end of the shutdown.
*/
function GracefulShutdown(server, opts) {
// option handling
// ----------------------------------
opts = opts || {};
// merge opts with default options
let options = Object.assign({
signals: 'SIGINT SIGTERM',
timeout: 30000,
development: false,
forceExit: true,
onShutdown: (signal) => Promise.resolve(signal),
preShutdown: (signal) => Promise.resolve(signal),
}, opts);
let isShuttingDown = false;
let connections = {};
let connectionCounter = 0;
let secureConnections = {};
let secureConnectionCounter = 0;
let failed = false;
let finalRun = false;
function onceFactory() {
let called = false;
return (emitter, events, callback) => {
function call() {
if (!called) {
called = true;
return callback.apply(this, arguments);
}
}
events.forEach(e => emitter.on(e, call));
};
}
const signals = options.signals
.split(' ')
.map(s => s.trim())
.filter(s => !!s.length);
const once = onceFactory();
once(process, signals, (signal) => {
debug('received shut down signal', signal);
shutdown(signal)
.then(() => {
if (options.forceExit) {
process.exit(failed ? 1 : 0);
}
})
.catch((err) => {
debug('server shut down error occurred', err);
process.exit(1);
});
});
// helper function
// ----------------------------------
function isFunction(functionToCheck) {
let getType = Object.prototype.toString.call(functionToCheck);
return /^\[object\s([a-zA-Z]+)?Function\]$/.test(getType);
}
function destroy(socket, force = false) {
if ((socket._isIdle && isShuttingDown) || force) {
socket.destroy();
if (socket.server instanceof http.Server) {
delete connections[socket._connectionId];
} else {
delete secureConnections[socket._connectionId];
}
}
}
function destroyAllConnections(force = false) {
// destroy empty and idle connections / all connections (if force = true)
debug('Destroy Connections : ' + (force ? 'forced close' : 'close'));
let counter = 0;
let secureCounter = 0;
Object.keys(connections).forEach(function (key) {
const socket = connections[key];
const serverResponse = socket._httpMessage;
// send connection close header to open connections
if (serverResponse && !force) {
if (!serverResponse.headersSent) {
serverResponse.setHeader('connection', 'close');
}
} else {
counter++;
destroy(socket);
}
});
debug('Connections destroyed : ' + counter);
debug('Connection Counter : ' + connectionCounter);
Object.keys(secureConnections).forEach(function (key) {
const socket = secureConnections[key];
const serverResponse = socket._httpMessage;
// send connection close header to open connections
if (serverResponse && !force) {
if (!serverResponse.headersSent) {
serverResponse.setHeader('connection', 'close');
}
} else {
secureCounter++;
destroy(socket);
}
});
debug('Secure Connections destroyed : ' + secureCounter);
debug('Secure Connection Counter : ' + secureConnectionCounter);
}
// set up server/process events
// ----------------------------------
server.on('request', function (req, res) {
req.socket._isIdle = false;
if (isShuttingDown && !res.headersSent) {
res.setHeader('connection', 'close');
}
res.on('finish', function () {
req.socket._isIdle = true;
destroy(req.socket);
});
});
server.on('connection', function (socket) {
if (isShuttingDown) {
socket.destroy();
} else {
let id = connectionCounter++;
socket._isIdle = true;
socket._connectionId = id;
connections[id] = socket;
socket.once('close', () => {
delete connections[socket._connectionId];
});
}
});
server.on('secureConnection', (socket) => {
if (isShuttingDown) {
socket.destroy();
} else {
let id = secureConnectionCounter++;
socket._isIdle = true;
socket._connectionId = id;
secureConnections[id] = socket;
socket.once('close', () => {
delete secureConnections[socket._connectionId];
});
}
});
process.on('close', function () {
debug('closed');
});
// shutdown event (per signal)
// ----------------------------------
function shutdown(sig) {
function cleanupHttp() {
destroyAllConnections();
debug('Close http server');
return new Promise((resolve, reject) => {
server.close((err) => {
if (err) {
return reject(err);
}
return resolve(true);
});
});
}
debug('shutdown signal - ' + sig);
// Don't bother with graceful shutdown on development to speed up round trip
if (options.development) {
debug('DEV-Mode - immediate forceful shutdown');
return process.exit(0);
}
function finalHandler() {
if (!finalRun) {
finalRun = true;
if (options.finally && isFunction(options.finally)) {
debug('executing finally()');
options.finally();
}
}
return Promise.resolve();
}
// returns true if should force shut down. returns false for shut down without force
function waitForReadyToShutDown(totalNumInterval) {
debug(`waitForReadyToShutDown... ${totalNumInterval}`);
if (totalNumInterval === 0) { // timeout reached
debug(
`Could not close connections in time (${options.timeout}ms), will forcefully shut down`
);
return Promise.resolve(true);
}
// test all connections closed already?
const allConnectionsClosed = Object.keys(connections).length === 0 && Object.keys(secureConnections).length === 0;
if (allConnectionsClosed) {
debug('All connections closed. Continue to shutting down');
return Promise.resolve(false);
}
debug('Schedule the next waitForReadyToShutdown');
return new Promise((resolve) => {
setTimeout(() => {
resolve(waitForReadyToShutDown(totalNumInterval - 1));
}, 250);
});
}
if (isShuttingDown) {
return Promise.resolve();
}
debug('shutting down');
return options
.preShutdown(sig)
.then(() => {
isShuttingDown = true;
cleanupHttp();
})
.then(() => {
const pollIterations = options.timeout
? Math.round(options.timeout / 250)
: 0;
return waitForReadyToShutDown(pollIterations);
})
.then((force) => {
debug('Do onShutdown now');
// if after waiting for connections to drain within timeout period
// or if timeout has reached, we forcefully disconnect all sockets
if (force) {
destroyAllConnections(force);
}
return options.onShutdown(sig);
})
.then(finalHandler)
.catch((err) => {
const errString = typeof err === 'string' ? err : JSON.stringify(err);
debug(errString);
failed = true;
throw errString;
});
}
function shutdownManual() {
return shutdown('manual');
}
return shutdownManual;
}
module.exports = GracefulShutdown;