如何为 koajs 服务器做优雅的停止?

How to do graceful stop for koajs server?

expressjs优雅停止的例子很多,koajs如何实现?

我也想断开数据库连接

我有一个 mongoose 数据库连接和 2 个 oracle 数据库连接 (https://github.com/oracle/node-oracledb)

前段时间我创建了一个 npm 包 http-graceful-shutdown (https://github.com/sebhildebrandt/http-graceful-shutdown)。这与 httpexpresskoa 完美配合。由于您还想添加自己的清理功能,因此我修改了包,这样您现在就可以添加自己的清理功能,该功能将在关机时调用。所以基本上这个包处理所有 http 关闭事情加上调用你的清理函数(如果在选项中提供):

const koa = require('koa');
const gracefulShutdown = require('http-graceful-shutdown');
const app = new koa();

...
server = app.listen(...); // app can be an express OR koa app
...

// your personal cleanup function - this one takes one second to complete
function cleanup() {
  return new Promise((resolve) => {
    console.log('... in cleanup')
    setTimeout(function() {
        console.log('... cleanup finished');
        resolve();
    }, 1000)       
  });
}

// this enables the graceful shutdown with advanced options
gracefulShutdown(server,
    {
        signals: 'SIGINT SIGTERM',
        timeout: 30000,
        development: false,
        onShutdown: cleanup,
        finally: function() {
            console.log('Server gracefulls shutted down.....')
        }
    }
);

我已经以一种或另一种方式在不同的 support channels. Unfortunately, I couldn't recommend any of the existing libraries because they are lacking 上多次回答 "how to terminate a HTTP server" 的变体。从那以后,我整理了一个包,(我相信)它可以处理正常 HTTP 服务器终止所需的所有情况。

https://github.com/gajus/http-terminator

http-terminator 的主要好处是:

  • 没有 monkey-patch Node.js API
  • 它会立即销毁所有没有附加 HTTP 请求的套接字
  • 它允许对正在进行的 HTTP 请求的套接字进行正常超时
  • 它可以正确处理 HTTPS 连接
  • 它通过设置连接通知使用 keep-alive 的连接服务器正在关闭:关闭 header
  • 它不会终止 Node.js 进程

与 Koa 的用法:

import Koa from 'koa';
import {
  createHttpTerminator,
} from 'http-terminator';

const app = new Koa();

const server = app.listen();

const httpTerminator = createHttpTerminator({
  server,
});

await httpTerminator.terminate();

为确保 Oracle 数据库连接正常关闭,您可以使用连接池并调用 pool.close() with a drainTime of 0 or greater. This will let the app relatively cleanly interrupt any operation that is currently using a connection. It allows freeing the DB end of the connections without the DB waiting for whatever timeout period to expire before it cleans itself up. Even with two connections this is a solution I'd look at, since it doesn't matter that the pool is small. You may need to set the Oracle Net out-of-band break detection as well, see Connections and High Availability