Skip to content

how to stop connections from closing when idle #699

@rhyek

Description

@rhyek

Expected behavior

For my setup, I want database connections to remain idle forever

Actual behavior

In a small local setup, hitting my database via a restful api will take around 2s after some amount of idle time. Using pg-monitor, I can see useCount === 0 every time this happens. After this, every subsequent request will respond in 10ms or less and useCount will increment.

I have tried the following to no effect:

import pgpLib from 'pg-promise';
import monitor from 'pg-monitor';
import promise from 'bluebird';

const initOptions = {
  promiseLib: promise,
};

monitor.attach(initOptions);
monitor.setTheme('matrix');
monitor.setLog((...args) => {
  console.log(...args);
});

const pgp = pgpLib(initOptions);
pgp.pg.defaults.max = 20;
// pgp.pg.defaults.idleTimeoutMillis = 0;
pgp.pg.defaults.idleTimeoutMillis = 10000000;

export const pgDb = pgp(process.env.DB_URL);

For my setup, I want database connections to remain idle forever until they're needed to avoid this new connection overhead. Could someone please provide code example of how to achieve this? Thanks.

Environment

  • Version of pg-promise: 10.4.4
  • OS type (Linux/Windows/Mac): Linux
  • Version of Node.js: 12.14.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions