Skip to content

Commit f14c8ab

Browse files
committed
Close all alive connections on SIGINT/SIGTERM
1 parent 22c790f commit f14c8ab

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/cli/parse-server.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ const help = function(){
3131
function startServer(options, callback) {
3232
const app = express();
3333
const api = new ParseServer(options);
34+
const sockets = {}
35+
3436
app.use(options.mountPath, api);
3537

3638
var server = app.listen(options.port, callback);
39+
server.on('connection', initializeConnections);
40+
3741
if (options.startLiveQueryServer || options.liveQueryServerOptions) {
3842
let liveQueryServer = server;
3943
if (options.liveQueryPort) {
@@ -43,8 +47,27 @@ function startServer(options, callback) {
4347
}
4448
ParseServer.createLiveQueryServer(liveQueryServer, options.liveQueryServerOptions);
4549
}
50+
51+
function initializeConnections(socket) {
52+
const socketId = socket.remoteAddress + ':' + socket.remotePort
53+
sockets[socketId] = socket;
54+
55+
socket.on('close', () => {
56+
delete sockets[socketId];
57+
});
58+
}
59+
60+
function destroyAliveConnections() {
61+
for (const socketId in sockets) {
62+
try {
63+
sockets[socketId].destroy()
64+
} catch (e) { }
65+
}
66+
}
67+
4668
var handleShutdown = function() {
4769
console.log('Termination signal received. Shutting down.');
70+
destroyAliveConnections();
4871
server.close(function () {
4972
process.exit(0);
5073
});

0 commit comments

Comments
 (0)