Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,14 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
socket.removeAllListeners()
socket = null

if (initial)
return reconnect()
if (initial) {
if (options.host[++retries])
return reconnect()

errored(Errors.connection('CONNECTION_CLOSED', options, socket))
}

retries = 0
!hadError && (query || sent.length) && error(Errors.connection('CONNECTION_CLOSED', options, socket))
closedTime = performance.now()
hadError && options.shared.retries++
Expand Down
19 changes: 19 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,25 @@ t('Connection errors are caught using begin()', {
]
})

t('Connection errors are caught when all hosts fail', {
timeout: 2
}, async() => {
let error
try {
const sql = postgres({ host: ['localhost', 'localhost'], port: [1, 1] })

await sql`select 1`
} catch (err) {
error = err
}

return [
true,
error.code === 'ECONNREFUSED' ||
error.message === 'Connection refused (os error 61)'
]
})

t('dynamic table name', async() => {
await sql`create table test(a int)`
return [
Expand Down