Skip to content

Commit 7afa5b7

Browse files
committed
Fixed some issue while testing with cloud cluster
1 parent 3df97e8 commit 7afa5b7

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

packages/pg/lib/client.js

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class Client extends EventEmitter {
100100
this.ssl = this.connectionParameters.ssl || false
101101
this.config = config
102102
this.prevHostIfUsePublic = this.host
103+
this.urlHost = this.host
103104
// As with Password, make SSL->Key (the private key) non-enumerable.
104105
// It won't show up in stack traces
105106
// or if the client is console.logged
@@ -253,7 +254,6 @@ class Client extends EventEmitter {
253254
this.prevHostIfUsePublic = currentHost
254255
this.host = serverInfo.public_ip
255256
}
256-
257257
if (this.host && this.host.indexOf('/') === 0) {
258258
con.connect(this.host + '/.s.PGSQL.' + this.port)
259259
} else {
@@ -319,7 +319,7 @@ class Client extends EventEmitter {
319319
var client = new Client(currConnectionString)
320320
this.attachErrorListenerOnClientConnection(client)
321321
let lookup = util.promisify(dns.lookup)
322-
let addresses = [client.host]
322+
let addresses = [{ address: client.host }]
323323
await lookup(client.host, { family: 0, all: true }).then((res) => {
324324
addresses = res
325325
})
@@ -448,7 +448,23 @@ class Client extends EventEmitter {
448448
}
449449
if (this.checkConnectionMapEmpty() && Client.failedHosts.size === 0) {
450450
lock.release()
451-
callback(error)
451+
// try with url host and mark that connection type as non-load_balanced
452+
this.host = this.urlHost
453+
this.connectionParameters.host = this.host
454+
this.connectionParameters.load_balance = false
455+
this.connection =
456+
this.config.connection ||
457+
new Connection({
458+
stream: this.config.stream,
459+
ssl: this.connectionParameters.ssl,
460+
keepAlive: this.config.keepAlive || false,
461+
keepAliveInitialDelayMillis: this.config.keepAliveInitialDelayMillis || 0,
462+
encoding: this.connectionParameters.client_encoding || 'utf8',
463+
})
464+
this._connecting = false
465+
Client.hostServerInfo.clear()
466+
Client.connectionMap.clear()
467+
this.connect(callback)
452468
return
453469
}
454470
lock.release()
@@ -485,7 +501,22 @@ class Client extends EventEmitter {
485501
}
486502
if (this.checkConnectionMapEmpty() && Client.failedHosts.size === 0) {
487503
lock.release()
488-
reject(error)
504+
this.host = this.urlHost
505+
this.connectionParameters.host = this.host
506+
this.connectionParameters.load_balance = false
507+
this.connection =
508+
this.config.connection ||
509+
new Connection({
510+
stream: this.config.stream,
511+
ssl: this.connectionParameters.ssl,
512+
keepAlive: this.config.keepAlive || false,
513+
keepAliveInitialDelayMillis: this.config.keepAliveInitialDelayMillis || 0,
514+
encoding: this.connectionParameters.client_encoding || 'utf8',
515+
})
516+
this._connecting = false
517+
Client.hostServerInfo.clear()
518+
Client.connectionMap.clear()
519+
this.connect(callback)
489520
return
490521
}
491522
lock.release()

0 commit comments

Comments
 (0)