Merge remote-tracking branch 'pgconn/master' into v5-dev

pull/1281/head v5.0.0-beta.2
Jack Christensen 2022-08-13 10:19:49 -05:00
commit 8eae4a2a3e
1 changed files with 10 additions and 8 deletions

View File

@ -117,19 +117,13 @@ func ConnectWithOptions(ctx context.Context, connString string, parseConfigOptio
// authentication error will terminate the chain of attempts (like libpq: // authentication error will terminate the chain of attempts (like libpq:
// https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS) and be returned as the error. Otherwise, // https://www.postgresql.org/docs/11/libpq-connect.html#LIBPQ-MULTIPLE-HOSTS) and be returned as the error. Otherwise,
// if all attempts fail the last error is returned. // if all attempts fail the last error is returned.
func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err error) { func ConnectConfig(octx context.Context, config *Config) (pgConn *PgConn, err error) {
// Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from // Default values are set in ParseConfig. Enforce initial creation by ParseConfig rather than setting defaults from
// zero values. // zero values.
if !config.createdByParseConfig { if !config.createdByParseConfig {
panic("config must be created by ParseConfig") panic("config must be created by ParseConfig")
} }
// ConnectTimeout restricts the whole connection process.
if config.ConnectTimeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, config.ConnectTimeout)
defer cancel()
}
// Simplify usage by treating primary config and fallbacks the same. // Simplify usage by treating primary config and fallbacks the same.
fallbackConfigs := []*FallbackConfig{ fallbackConfigs := []*FallbackConfig{
{ {
@ -139,7 +133,7 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err
}, },
} }
fallbackConfigs = append(fallbackConfigs, config.Fallbacks...) fallbackConfigs = append(fallbackConfigs, config.Fallbacks...)
ctx := octx
fallbackConfigs, err = expandWithIPs(ctx, config.LookupFunc, fallbackConfigs) fallbackConfigs, err = expandWithIPs(ctx, config.LookupFunc, fallbackConfigs)
if err != nil { if err != nil {
return nil, &connectError{config: config, msg: "hostname resolving error", err: err} return nil, &connectError{config: config, msg: "hostname resolving error", err: err}
@ -152,6 +146,14 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err
foundBestServer := false foundBestServer := false
var fallbackConfig *FallbackConfig var fallbackConfig *FallbackConfig
for _, fc := range fallbackConfigs { for _, 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()
} else {
ctx = octx
}
pgConn, err = connect(ctx, config, fc, false) pgConn, err = connect(ctx, config, fc, false)
if err == nil { if err == nil {
foundBestServer = true foundBestServer = true