From 1525f32dbc3e2a0078ca85c5bc73017f8064f64d Mon Sep 17 00:00:00 2001 From: Dirk de Visser Date: Sat, 10 Dec 2022 16:51:30 +0100 Subject: [PATCH] Fix writing host and port on connection timeouts --- cjs/src/connection.js | 3 +++ cjs/tests/index.js | 16 ++++++++++++++++ deno/src/connection.js | 3 +++ deno/tests/index.js | 16 ++++++++++++++++ src/connection.js | 3 +++ tests/index.js | 16 ++++++++++++++++ 6 files changed, 57 insertions(+) diff --git a/cjs/src/connection.js b/cjs/src/connection.js index 1aaef2a1..c2fb492f 100644 --- a/cjs/src/connection.js +++ b/cjs/src/connection.js @@ -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 } diff --git a/cjs/tests/index.js b/cjs/tests/index.js index a7ed442f..6875f6b9 100644 --- a/cjs/tests/index.js +++ b/cjs/tests/index.js @@ -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 diff --git a/deno/src/connection.js b/deno/src/connection.js index c1706e3f..7a9b742f 100644 --- a/deno/src/connection.js +++ b/deno/src/connection.js @@ -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 } diff --git a/deno/tests/index.js b/deno/tests/index.js index 788d5c6f..cd0be995 100644 --- a/deno/tests/index.js +++ b/deno/tests/index.js @@ -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 diff --git a/src/connection.js b/src/connection.js index 6a296508..ad76760d 100644 --- a/src/connection.js +++ b/src/connection.js @@ -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 } diff --git a/tests/index.js b/tests/index.js index e778465b..173c7cf8 100644 --- a/tests/index.js +++ b/tests/index.js @@ -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