如何优化closures我的易捷版服务器?

在生产环境中运行我的Express应用程序时,我希望在其进程被终止时正常closures服务器(即发送SIGTERM或SIGINT)。

这里是我的代码的简化版本:

const express = require('express'); const app = express(); app.get('/', (req, res) => res.json({ ping: true })); const server = app.listen(3000, () => console.log('Running…')); setInterval(() => server.getConnections( (err, connections) => console.log(`${connections} connections currently open`) ), 1000); process.on('SIGTERM', shutDown); process.on('SIGINT', shutDown); function shutDown() { console.log('Received kill signal, shutting down gracefully'); server.close(() => { console.log('Closed out remaining connections'); process.exit(0); }); setTimeout(() => { console.error('Could not close connections in time, forcefully shutting down'); process.exit(1); }, 10000); } 

当我运行它并在浏览器中调用URL http:// localhost:3000 /时,setInterval函数中的日志语句将保持打印“1个连接当前打开”,直到我实际closures浏览器窗口。 即使closures标签也会使连接保持打开状态。

所以如果我按下Ctrl + C来杀死我的服务器,它将进入超时并在10秒后打印“无法closures连接”,同时继续打印“1连接打开”。

只有在closures浏览器窗口之前,我才能看到“closures剩余的连接”消息。

我在这里错过了什么? 什么是正常closuresExpress服务器的正确方法?

尝试NPM express-graceful-shutdown模块 ,优雅的关机将允许任何连接,包括你的数据库完成,不允许任何新的/新的build立。 由于您正在使用可能是您正在寻找的模块的快递,但是快速的NPMsearch将显示适合于Http服务器的所有模块列表。

如果有人感兴趣,我自己find了一个解决scheme(希望听到意见反馈)。

我添加了在服务器上打开连接的侦听器,将对这些连接的引用存储在数组中。 连接closures后,将从arrays中删除。

当服务器被end ,每个连接都通过调用其end方法来closures。 对于某些浏览器(例如Chrome),这是不够的,所以在超时之后,我会在每个连接上调用destroy

 const express = require('express'); const app = express(); app.get('/', (req, res) => res.json({ ping: true })); const server = app.listen(3000, () => console.log('Running…')); setInterval(() => server.getConnections( (err, connections) => console.log(`${connections} connections currently open`) ), 1000); process.on('SIGTERM', shutDown); process.on('SIGINT', shutDown); let connections = []; server.on('connection', connection => { connections.push(connection); connection.on('close', () => connections = connections.filter(curr => curr !== connection)); }); function shutDown() { console.log('Received kill signal, shutting down gracefully'); server.close(() => { console.log('Closed out remaining connections'); process.exit(0); }); setTimeout(() => { console.error('Could not close connections in time, forcefully shutting down'); process.exit(1); }, 10000); connections.forEach(curr => curr.end()); setTimeout(() => connections.forEach(curr => curr.destroy()), 5000); } 

您遇到的问题是,所有现代浏览器重复使用单个连接的多个请求。 这被称为保持连接

处理这种情况的正确方法是监视所有新的连接和请求,并跟踪每个连接的状态(现在是空闲还是活动状态)。 然后,可以强制closures所有空闲连接,并确保在当前请求正在处理之后closures活动连接。

我已经实现了@ moebius / http-graceful-shutdown模块,专门用于整体closuresExpress应用程序和节点服务器。 不幸的是也不明确,也没有节点本身没有内置的这个function。

以下是如何与任何Express应用程序一起使用的方法:

 const express = require('express'); const GracefulShutdownManager = require('@moebius/http-graceful-shutdown').GracefulShutdownManager; const app = express(); const server = app.listen(8080); const shutdownManager = new GracefulShutdownManager(server); process.on('SIGTERM', () => { shutdownManager.terminate(() => { console.log('Server is gracefully terminated'); }); }); 

随意检查模块 ,GitHub页面有更多的细节。