如何使用节点发布正确处理意外断开连接/自动重新连接



从5.1更新到6.2.4后,我有时会(几乎每天一次(看到以下错误,导致我们的生产服务器崩溃:

Error: Connection terminated unexpectedly
2017-06-15T10:03:33.683+02:00 at Object.onceWrapper (events.js:293:19)
2017-06-15T10:03:33.683+02:00Connection terminated unexpectedly
2017-06-15T10:03:33.684+02:00 at Socket.<anonymous> (/home/bas/app_21386476-a451-424c-ad67-870442bbdbe7/node_modules/pg/lib/connection.js:138:10)
2017-06-15T10:03:33.684+02:00 at emitNone (events.js:86:13)
2017-06-15T10:03:33.684+02:00 at emitNone (events.js:91:20)
2017-06-15T10:03:33.684+02:00 at Connection.emit (events.js:188:7)
2017-06-15T10:03:33.684+02:00 at Socket.emit (events.js:188:7)
2017-06-15T10:03:33.685+02:00 at process._tickCallback (internal/process/next_tick.js:104:9)
2017-06-15T10:03:33.685+02:00undefined
2017-06-15T10:03:33.685+02:00 at _combinedTickCallback (internal/process/next_tick.js:80:11)
2017-06-15T10:03:33.685+02:00 at endReadableNT (_stream_readable.js:975:12)

我见过 https://github.com/brianc/node-postgres/pull/1316

AFAI,在此补丁之前未发出错误,截至目前,我们在池上发出错误。

我在这里有点困惑,当这个错误发生时我们应该怎么做?这是否意味着我们必须重新连接整个池?但是怎么做呢?或者这是否意味着发出错误的客户端最终已死亡,因此我们宁愿删除客户端发起的操作,并告诉用户他的查询已被丢弃。但是再一次,由于client.query回调似乎没有被此错误调用(可能与 https://github.com/brianc/node-postgres/issues/1322 有关(?

发生此错误时我们应该怎么做?这是否意味着我们必须重新连接整个池?

这意味着只需在Pool对象上具有.on('error', cb)处理程序就足以自动重新创建相应的Client对象,而无需您提供任何其他内容。

如果您想要 100% 可靠的驱动程序版本,我建议您暂时坚持使用 5.1 版。较新的版本确实在这里和那里提供了一些小的更新,但连接处理在其新的连接池中有点横向。

尽管最新的修复程序确实带来了一些希望,即已知的连接问题已经消失,但尚未通过时间和适当的测试来证明。

另请参阅我之前写过的其他一些想法。那里的一些观点仍然有效。

我最终做了如下事情:

let client;
const connectDb = () => {
    const startedAt = new Date().getTime();
    client = new Client(dbConfig);
    client.on('error', (err) => {
        console.log('An error occurred with client => ', err);
        console.log('startedAt', startedAt);
        console.log('crashedAt', new Date().getTime());
        // Reconnect
        connectDb();
    });
    client.connect((err) => {
        if (err) {
            console.log(chalk.redBright(`[main] Connection error => ${err}`));
        } else {
            console.log(chalk.blueBright(`[main] Connected to db!`));
        }
    });
};
connectDb();

作为临时解决方案,这完成了工作,但我注意到startedAtcrashedAt总是相隔 30 分钟。我敢肯定,某处有一个连接超时的配置变量。我还没有研究过,但以上应该是你问题的足够答案。

最新更新