在Nodejs中重build连接,pg-promise

在使用pg-promise构buildmaster / replica postgres连接的情况下,有没有办法在出现副本中断的情况下重build这些连接?

而不是做process.exitCode = 1; 在与initOptions传递的错误函数,并重build服务启动时只有工作连接…有没有更好的方式来删除失败的连接(如果它的副本和process.exitCode更好,如果它的主要)?

 const initOptions = { // global event notification; error: (error, e) => { if (e.cn) { //log } process.exitCode =1; } }; //singleton const pgp = require('pg-promise')(initOptions); // then for each database in config, we connect and start the service 

模块pg-promisebuild立在顶层node-postgres之上,它使用连接池,能够自动恢复断开的连接。

为此,没有必要在你身边。 一旦连接再次可用,您的查询将再次开始成功。

根据你所寻求的逻辑,你可以在主连接丢失的时候专门执行process.exit() ,而忽略(或只logging)副本连接的丢失。

假设这两者是通过单独的数据库对象使用的,您可以借助在对象构build期间可以传入的dc参数( 数据库上下文)来区分它们,这可以是任何事情。

例:

 const dbPrimary = pgp(primaryConnection, 'primary'); const dbReplica = pgp(replicaConnection, 'replica'); 

然后在全局error handling程序中:

 const initOptions = { error: (err, e) => { if(e.cn) { // connectivity issue: console.log(e.dc, 'connection error:', err); if(e.dc === 'primary') { process.exit(1); } } } };