在Express 3中closures数据库连接
在Express 3中,当进程存在时,如何处理closures数据库连接?
.on('close', ...
事件不会被发射,除非您明确使用HTTP服务器的.close()
调用。
到目前为止,这是最接近我来,但它使用process.on
而不是server.on
:
process.on('SIGTERM', function () { // Close connections. process.exit(0); });
基于以下信息:
- https://github.com/visionmedia/express/issues/1366
- http://blog.argteam.com/coding/hardening-node-js-for-production-part-3-zero-downtime-deployments-with-nginx/
–
var express = require('express'); var app = express(); var server = app.listen(1337); var shutting_down = false; app.use(function (req, resp, next) { if(!shutting_down) return next(); resp.setHeader('Connection', "close"); resp.send(503, "Server is in the process of restarting"); // Change the response to something your client is expecting: // html, text, json, etc. }); function cleanup () { shutting_down = true; server.close( function () { console.log( "Closed out remaining connections."); // Close db connections, other chores, etc. process.exit(); }); setTimeout( function () { console.error("Could not close connections in time, forcing shut down"); process.exit(1); }, 30*1000); } process.on('SIGINT', cleanup); process.on('SIGTERM', cleanup);
Connection: close
头用来告诉任何keep-alive
连接在下一次发送HTTP请求时closures。 更多信息在这里: http : //www.jmarshall.com/easy/http/#http1.1s4
我不知道是否有其他方式closureskeep-alive
连接。 节点进程将挂起,除非keep-alive
连接closures。 默认的空闲超时时间是2分钟。 更多信息。 在node.js keep-alive
超时(包括在更改超时): 如何设置HTTP Keep-Alive超时在nodejs服务器