Skip to content

Commit

Permalink
Fix writing host and port on connection timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkdev98 authored and porsager committed Feb 1, 2023
1 parent 7e4f76d commit 1525f32
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
3 changes: 3 additions & 0 deletions cjs/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return socket.connect(options.path)

socket.connect(port[hostIndex], host[hostIndex])
socket.host = host[hostIndex]
socket.port = port[hostIndex]

hostIndex = (hostIndex + 1) % port.length
}

Expand Down
16 changes: 16 additions & 0 deletions cjs/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,22 @@ t('connect_timeout throws proper error', async() => [
})`select 1`.catch(e => e.code)
])

t('connect_timeout error message includes host:port', { timeout: 20 }, async() => {
const connect_timeout = 0.2
const server = net.createServer()
server.listen()
const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })
const port = server.address().port
let err
await sql`select 1`.catch((e) => {
if (e.code !== 'CONNECT_TIMEOUT')
throw e
err = e.message
})
server.close()
return [["write CONNECT_TIMEOUT 127.0.0.1:", port].join(""), err]
})

t('requests works after single connect_timeout', async() => {
let first = true

Expand Down
3 changes: 3 additions & 0 deletions deno/src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return socket.connect(options.path)

socket.connect(port[hostIndex], host[hostIndex])
socket.host = host[hostIndex]
socket.port = port[hostIndex]

hostIndex = (hostIndex + 1) % port.length
}

Expand Down
16 changes: 16 additions & 0 deletions deno/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,22 @@ t('connect_timeout throws proper error', async() => [
})`select 1`.catch(e => e.code)
])

t('connect_timeout error message includes host:port', { timeout: 20 }, async() => {
const connect_timeout = 0.2
const server = net.createServer()
server.listen()
const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })
const port = server.address().port
let err
await sql`select 1`.catch((e) => {
if (e.code !== 'CONNECT_TIMEOUT')
throw e
err = e.message
})
server.close()
return [["write CONNECT_TIMEOUT 127.0.0.1:", port].join(""), err]
})

t('requests works after single connect_timeout', async() => {
let first = true

Expand Down
3 changes: 3 additions & 0 deletions src/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,9 @@ function Connection(options, queues = {}, { onopen = noop, onend = noop, onclose
return socket.connect(options.path)

socket.connect(port[hostIndex], host[hostIndex])
socket.host = host[hostIndex]
socket.port = port[hostIndex]

hostIndex = (hostIndex + 1) % port.length
}

Expand Down
16 changes: 16 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,22 @@ t('connect_timeout throws proper error', async() => [
})`select 1`.catch(e => e.code)
])

t('connect_timeout error message includes host:port', { timeout: 20 }, async() => {
const connect_timeout = 0.2
const server = net.createServer()
server.listen()
const sql = postgres({ port: server.address().port, host: '127.0.0.1', connect_timeout })
const port = server.address().port
let err
await sql`select 1`.catch((e) => {
if (e.code !== 'CONNECT_TIMEOUT')
throw e
err = e.message
})
server.close()
return [["write CONNECT_TIMEOUT 127.0.0.1:", port].join(""), err]
})

t('requests works after single connect_timeout', async() => {
let first = true

Expand Down

0 comments on commit 1525f32

Please sign in to comment.