Skip to content

Commit

Permalink
[CONJS-254] ensuring option connectTimeout is respected
Browse files Browse the repository at this point in the history
* now respected when using unix socket or name pipe
* timeout is removed when socket is successfully established, in place of returning connection object (so when authentication is complete + 'pre-queries' if needed ended)
  • Loading branch information
rusher committed May 12, 2023
1 parent 3193c62 commit e2a7e16
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions lib/connection.js
Expand Up @@ -769,6 +769,9 @@ class Connection extends EventEmitter {
* @private
*/
streamInitSocket() {
if (this.opts.connectTimeout) {
this.timeout = setTimeout(this.connectTimeoutReached.bind(this), this.opts.connectTimeout, Date.now());
}
if (this.opts.socketPath) {
this.socket = Net.connect(this.opts.socketPath);
} else if (this.opts.stream) {
Expand Down Expand Up @@ -805,17 +808,12 @@ class Connection extends EventEmitter {
}

socketInit() {
if (this.opts.connectTimeout) {
this.timeout = setTimeout(this.connectTimeoutReached.bind(this), this.opts.connectTimeout, Date.now());
}

this.socket.on('data', this.streamIn.onData.bind(this.streamIn));
this.socket.on('error', this.socketErrorHandler.bind(this));
this.socket.on('end', this.socketErrorHandler.bind(this));
this.socket.on(
'connect',
function () {
clearTimeout(this.timeout);
if (this.status === Status.CONNECTING) {
this.status = Status.AUTHENTICATING;
this.socket.setTimeout(this.opts.socketTimeout, this.socketTimeoutReached.bind(this));
Expand Down Expand Up @@ -867,6 +865,7 @@ class Connection extends EventEmitter {
.then(this.executeInitQuery.bind(this))
.then(this.executeSessionTimeout.bind(this))
.then(() => {
clearTimeout(this.timeout);
conn.status = Status.CONNECTED;
process.nextTick(conn.connectResolveFct, conn);

Expand Down

0 comments on commit e2a7e16

Please sign in to comment.