pg-promise中的查询超时 [英] Query timeout in pg-promise

查看:103
本文介绍了pg-promise中的查询超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向pg-promise查询添加超时,因此如果数据库尚未响应,则它们将在一段时间后失败。
是否有任何建议的方法可以做到这一点,或者我应该制作自定义包装来处理计时器并在太晚时拒绝promise?

解决方案

来自 pg-promise ...




pg-promise 不支持查询取消,因为它可以解决数据库设计错误或查询执行不正确的问题。



PostgreSQL支持事件在执行耗时的查询时应使用该函数,因此无需等待,可以将事件侦听器设置为在特定数据/视图可用时触发。参见 LISTEN / NOTIFY 示例。



您可以扩展 pg-promise 使用您自己的自定义查询方法,该方法将因拒绝而超时(请参见下面的示例),但这还是设计问题之外的另一种解决方法。



使用蓝鸟的示例

  const Promise = require('bluebird'); 

Promise.config({
cancel:true
});


const initOptions = {
promiseLib:承诺,
extend(obj){
obj.queryTimeout =(查询,值,延迟)=> ; {
return obj.any(query,values).timeout(delay);
}
}
};

const pgp = require(’pg-promise’)(initOptions);
const db = pgp(/ *连接详细信息* /);

然后您可以使用 db.queryTimeout(query,values,delay)在每个级别上。



或者,如果您使用的是蓝鸟 ,您可以将 .timeout(delay)链接到任何现有方法:

  db.any(查询,值)
.timeout(500)
.then(数据=> {})
.catch(错误=> {})

另请参见:





更新



从版本 8.5.3 pg-promise 开始通过属性 query_time支持查询超时



您可以覆盖默认值:

  pgp.pg.defaults.query_timeout = 3000; // 3秒后每个查询超时

或在连接对象中指定该查询:

  const db = pgp({
/ *所有连接详细信息* /

query_timeout:3000
});


I want to add timeout to pg-promise queries so they will fail after some amount of time if database have not yet responded. Is there any recommended way to do that or should I make custom wrapper that will handle timer and reject promise if it's too late?

解决方案

From the author of pg-promise...


pg-promise doesn't support query cancellation, because it is a hack to work-around incorrect database design or bad query execution.

PostgreSQL supports events that should be used when executing time-consuming queries, so instead of waiting, one can set an event listener to be triggered when specific data/view becomes available. See LISTEN/NOTIFY example.

You can extend pg-promise with your own custom query method that will time out with a reject (see example below), but that's again another work-around on top of a design problem.

Example using Bluebird:

const Promise = require('bluebird');

Promise.config({
    cancellation: true
});


const initOptions = {
    promiseLib: Promise,
    extend(obj) {
        obj.queryTimeout = (query, values, delay) => {
            return obj.any(query, values).timeout(delay);
        }
    }
};

const pgp = require('pg-promise')(initOptions);
const db = pgp(/* connection details */);

Then you can use db.queryTimeout(query, values, delay) on every level.

Alternatively, if you are using Bluebird, you can chain .timeout(delay) to any of the existing methods:

db.any(query, values)
    .timeout(500)
    .then(data => {})
    .catch(error => {})

See also:

UPDATE

From version 8.5.3, pg-promise started supporting query timeouts, via property query_timeout within the connection object.

You can either override the defaults:

pgp.pg.defaults.query_timeout = 3000; // timeout every query after 3 seconds

Or specify it within the connection object:

const db = pgp({
    /* all connection details */

    query_timeout: 3000
});

这篇关于pg-promise中的查询超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆