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 (default is prefer) 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.

The root cause was to implement sslmode=allow|prefer conn are tried twice. First with TLSConfig and if that doesn't work then without TLSConfig. The fix for this issue now uses the same context if same host is being tried out. This change won't affect the existing multi-host behavior.

This PR goal is to close issue [/issues/1672](#1672)
  • Loading branch information
smaher-edb authored and jackc committed Jul 15, 2023
1 parent bd3e0d4 commit f47f0cf
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,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

0 comments on commit f47f0cf

Please sign in to comment.