Skip to content

Commit

Permalink
connect_timeout is not obeyed for sslmode=allow|prefer
Browse files Browse the repository at this point in the history
connect_timeout given in conn string was not obeyed if sslmode is not specified or equals sslmode=allow|prefer. It took twice the amount of time specified by connect_timeout in conn string. While this behavior is correct if multi-host is provided in conn string, it doesn't look correct in case of single host. This behavior was also not matching with libpq.

Ref: [1672](jackc/pgx#1672)
  • Loading branch information
smaher-edb authored and jackc committed Jul 15, 2023
1 parent f9ad18f commit 9b31034
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,15 @@ func ConnectConfig(octx context.Context, config *Config) (pgConn *PgConn, err er

foundBestServer := false
var fallbackConfig *FallbackConfig
for _, fc := range fallbackConfigs {
for i, fc := range fallbackConfigs {
// ConnectTimeout restricts the whole connection process.
if config.ConnectTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(octx, config.ConnectTimeout)
defer cancel()
// create new context first time or when previous host was different
if i == 0 || (fallbackConfigs[i].Host != fallbackConfigs[i-1].Host) {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(octx, config.ConnectTimeout)
defer cancel()
}
} else {
ctx = octx
}
Expand Down Expand Up @@ -599,9 +602,10 @@ func (pgConn *PgConn) PID() uint32 {
// TxStatus returns the current TxStatus as reported by the server in the ReadyForQuery message.
//
// Possible return values:
// 'I' - idle / not in transaction
// 'T' - in a transaction
// 'E' - in a failed transaction
//
// 'I' - idle / not in transaction
// 'T' - in a transaction
// 'E' - in a failed transaction
//
// See https://www.postgresql.org/docs/current/protocol-message-formats.html.
func (pgConn *PgConn) TxStatus() byte {
Expand Down

0 comments on commit 9b31034

Please sign in to comment.