Send connection: close header on shutdown

This commit is contained in:
Shweelan Samson authored and Zaid Abdulla committed 2018-12-27 01:21:18 -08:00
1 parent 00dd345784
commit 4dec8921db
1 file changed
+9 -11
+9 -11
View File
@@ -169,26 +169,24 @@ koa.use(
flush: require("zlib").Z_SYNC_FLUSH
})
);
koa.use(router.routes());
koa.use(koaStatic("./public"));
const server = http
.createServer(koa.callback())
.listen(config.server.port, config.server.interface);
koa.use(async (ctx, next) => {
await next();
if (!server.listening) ctx.set("Connection", "close");
});
koa.use(router.routes());
koa.use(koaStatic("./public"));
function closeServer(timeout) {
return new Promise(resolve => {
setTimeout(() => {
if (!resolve) return;
// Ignore HTTP requests from alive connections
server.removeListener("request", koa.callback());
server.setTimeout(1);
setTimeout(resolve, 1000);
}, timeout).unref();
// prevent new sockets to connect and close server eventually.
server.close(resolve);
setTimeout(resolve, timeout).unref();
});
}