我在网上看到了大量有关正常关闭的文档(为了避免API请求失败,几乎每个生产应用都应处理该问题
我有这个用于服务器设置:
var pendingRequests = {};
function initServer() {
var app = express();
var server = http.createServer(app);
server.listen(global.port, function() {
logger.info('Listening on port ' + global.port);
process.on('SIGINT', function() {
server.close(function() {
logger.info('Server shut down on port ' + global.port);
var drainCheck = setInterval(function() {
if (Object.keys(pendingRequests).length === 0) {
clearInterval(drainCheck);
process.exit(0);
}
}, 100);
});
});
});
}
并且我有这个用于注册待处理的请求:
function initRoute(app, method, path, fn) {
app[method](path, async function(req, resp) {
var requestId = uuid.v4();
var start = now();
pendingRequests[requestId] = true;
try {
var res = await fn(req, resp);
resp.send(utilitiesModule.snakeToCamel(res));
var end = now();
logger.info({
method: method,
path: path,
time: end - start
});
} catch (err) {
var end = now();
logger.error({
error: err.stack,
method: method,
path: path,
time: end - start
});
resp.status(500).send({
'error': err.message,
'stack': err.stack
});
}
delete pendingRequests[requestId];
});
}
为什么没有关于此的更多文档?这让我感到自己做错了什么。在某个地方是否有更常用的+经过测试的+经过深思熟虑的中间件或库?在其他应用中如何处理?