Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose host address when timeout exceeded #1943

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pgconn/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ func normalizeTimeoutError(ctx context.Context, err error) error {
if ctx.Err() == context.Canceled {
// Since the timeout was caused by a context cancellation, the actual error is context.Canceled not the timeout error.
return context.Canceled
} else if ctx.Err() == context.DeadlineExceeded {
return &errTimeout{err: ctx.Err()}
} else if errors.Is(ctx.Err(), context.DeadlineExceeded) {
return &errTimeout{err: fmt.Errorf("%s: %w", netErr.Error(), context.DeadlineExceeded)}
} else {
return &errTimeout{err: netErr}
}
Expand Down
18 changes: 14 additions & 4 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,13 @@ func ConnectConfig(octx context.Context, config *Config) (pgConn *PgConn, err er
} else {
ctx = octx
}
pgConn, err = connect(ctx, config, fc, false)
if err == nil {
var fberr error
pgConn, fberr = connect(ctx, config, fc, false)
if fberr == nil {
foundBestServer = true
err = nil
break
} else if pgerr, ok := err.(*PgError); ok {
} else if pgerr, ok := fberr.(*PgError); ok {
err = &ConnectError{Config: config, msg: "server error", err: pgerr}
const ERRCODE_INVALID_PASSWORD = "28P01" // wrong password
const ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION = "28000" // wrong password or bad pg_hba.conf settings
Expand All @@ -189,10 +191,18 @@ func ConnectConfig(octx context.Context, config *Config) (pgConn *PgConn, err er
pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE {
break
}
} else if cerr, ok := err.(*ConnectError); ok {
} else if cerr, ok := fberr.(*ConnectError); ok {
if _, ok := cerr.err.(*errTimeout); ok && err != nil {
// once we reach first `context.DeadlineExceeded` it's useless to check other fallbacks
// we should keep first error before deadline exceeded as resulted error to keep root cause message
break
}
err = fberr
if _, ok := cerr.err.(*NotPreferredError); ok {
fallbackConfig = fc
}
} else {
err = fberr
}
}

Expand Down