From a95cfe5cc56b2d85bfc20ca5b5f9c05fd1eb287b Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Sat, 14 Jan 2023 09:19:00 -0600 Subject: [PATCH] Fix connect with multiple hostnames when one can't be resolved If multiple hostnames are provided and one cannot be resolved the others should still be tried. Longterm, it would be nice for the connect process to return a list of errors rather than just one. fixes https://github.com/jackc/pgx/issues/1464 --- pgconn/pgconn.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pgconn/pgconn.go b/pgconn/pgconn.go index 59fa35c6..0a4863f1 100644 --- a/pgconn/pgconn.go +++ b/pgconn/pgconn.go @@ -203,6 +203,8 @@ func ConnectConfig(octx context.Context, config *Config) (pgConn *PgConn, err er func expandWithIPs(ctx context.Context, lookupFn LookupFunc, fallbacks []*FallbackConfig) ([]*FallbackConfig, error) { var configs []*FallbackConfig + var lookupErrors []error + for _, fb := range fallbacks { // skip resolve for unix sockets if isAbsolutePath(fb.Host) { @@ -217,7 +219,8 @@ func expandWithIPs(ctx context.Context, lookupFn LookupFunc, fallbacks []*Fallba ips, err := lookupFn(ctx, fb.Host) if err != nil { - return nil, err + lookupErrors = append(lookupErrors, err) + continue } for _, ip := range ips { @@ -242,6 +245,12 @@ func expandWithIPs(ctx context.Context, lookupFn LookupFunc, fallbacks []*Fallba } } + // See https://github.com/jackc/pgx/issues/1464. When Go 1.20 can be used in pgx consider using errors.Join so all + // errors are reported. + if len(configs) == 0 && len(lookupErrors) > 0 { + return nil, lookupErrors[0] + } + return configs, nil }