question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add ability to set connect timeout

See original GitHub issue

I read the docs on client options: https://github.com/brianc/node-postgres/wiki/Client#constructor

And didn’t see a way to set the connection timeout. A simple example of what I’d like to tune:

var pg = require('pg');

//Assuming no machine with ip 192.168.255.255 exists
var client = new pg.Client('tcp://root:root@192.168.255.255');
var start = (new Date()).getTime();
client.connect(function (err) {
        var end = (new Date()).getTime();
        console.log({
                err: err,
                elapsedTime: end - start
        });
});

Results in:

[nate@dev madtom]$ node ./test/postgres_timeout.js
{ err:
   { [Error: connect ECONNREFUSED]
     code: 'ECONNREFUSED',
     errno: 'ECONNREFUSED',
     syscall: 'connect' },
  elapsedTime: 75818 }

Notice that the elapsed time is 75 seconds.

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:3
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
zacharyrankincommented, Mar 31, 2017

hi @nfitch,

I was looking for the same thing. I am not using the pool feature, but pg.Client directly. I was playing with a couple of ideas.

Example 1:

let stream = new net.Stream()
stream.setTimeout(2000) // however many seconds
stream.on('timeout', function () {
  console.log('client timed out')
  this.destroy()
})
let config = {
  // connection info
  stream: stream
}
let client = new pg.Client(config)
client.connect(err => {
  if (err) {
    console.error(err)
  }
  console.log('connected')
})

In Example 1, if a query takes longer than 2 seconds to return results I think it will also trigger that timeout. So here was another idea I had if you just wanted to have a connection timeout.

let stream = new net.Stream()
let config = {
  // connection info
  stream: stream
}
let client = new pg.Client(config)
let connTimeout = setTimeout(() => stream.destroy, 2000)
client.connect(err => {
  if (err) {
    console.error(err)
  }
  console.log('connected')
  clearTimeout(connTimeout)
})

These are just some ideas. Let me know your thoughts.

0reactions
aliokcommented, Aug 3, 2018

@charmander thanks a lot for taking a look at that.

The context I am actually using that function is during a connection test. So, after the promise is resolved I do client.end() in the caller.

Thanks for showing me how to force kill: I will use it.

// force kill the node driver, and let libpq do its teardown
client.connection ? client.connection.stream.destroy() : client.end()

Gonna edit my previous comment to add a note to not confuse anybody 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add Timeout Capability to Express Apps with connect-timeout
We can do this by making requests that take too long to time out. The connect-timeout middleware can help us do this.
Read more >
What is "Connect Timeout" in sql server connection string?
It specifies how long you will allow your program to be held up while it establishes a database connection.
Read more >
Connect Timeout - Akamai TechDocs
Timeout, Sets the connection timeout value in seconds, minutes, hours, or days. The default is 5 seconds.
Read more >
Configuring connection timeout on Message Processors
Add the property connect.timeout.millis with an appropriate value under the <HTTPTargetConnection> element in the TargetEndpoint configuration. For example, to ...
Read more >
Timeouts • Akka HTTP - Documentation
The idle-timeout is a setting which sets the maximum inactivity time of a given connection. If no data is sent or received on...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found