porsager / porsager/postgres

Issue with auto failover with CONNECT_TIMEOUT host

Open
#988 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

Related to but different than #815

const pgsql = postgres({
  host: "host1,host2,host3",
  port: 5432,
  user: "username",
  password: "password",
  database: "app",
  target_session_attrs: primary,
  max: 20,
  connect_timeout: 3
});

when host1 is offline, resulting in a CONNECT_TIMEOUT on the socket then the postgres client errors with the following and does not attempt to connect to host2 or host3:

Error: write CONNECT_TIMEOUT host1:5432
Stack: Error: write CONNECT_TIMEOUT host1:5432
    at connectTimedOut (node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js:257:20)
    at Timeout.done [as _onTimeout] (node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js:1033:8)
    at listOnTimeout (node:internal/timers:596:11)
    at process.processTimers (node:internal/timers:529:7)

when host1 is online, but not listening on port then the postgres library connects to host2 or host3, as appropriate based on primary session attrs.

when we change our config to only connect to host1 when it is not listening on port:

const pgsql = postgres({
 host: "host1",
 port: 5432,
 user: "username",
 password: "password",
 database: "app",
 max: 20,
});

we get the following error when trying to connect to the db via this postgres client library

Error:
Stack: AggregateError [ECONNREFUSED]:
    at internalConnectMultiple (node:net:1121:18)
    at internalConnectMultiple (node:net:1189:5)
    at afterConnectMultiple (node:net:1688:7)

It's as if ECONNREFUSED is handled in the desired way but CONNECT_TIMEOUT is not.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with node_modules/.pnpm/postgres@3.4.4/node_modules/postgres/cjs/src/connection.js, especially connectTimedOut and the timeout callback around line 1033. Reproduce the configuration with host1 offline and host2/host3 available, then verify that a CONNECT_TIMEOUT triggers the same failover behavior as ECONNREFUSED while respecting target_session_attrs: primary.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.