From 1b6543f29c8c08ccfc51a8d426ea44d960ae4d3e Mon Sep 17 00:00:00 2001 From: "sergey.bashilov" Date: Mon, 20 Jun 2022 12:15:15 +0300 Subject: [PATCH] fix typos --- config.go | 8 ++++---- config_test.go | 2 +- errors.go | 8 ++++---- pgconn.go | 24 ++++++++++-------------- 4 files changed, 19 insertions(+), 23 deletions(-) diff --git a/config.go b/config.go index fc5b3c0f..dac7b95b 100644 --- a/config.go +++ b/config.go @@ -370,7 +370,7 @@ func ParseConfig(connString string) (*Config, error) { case "standby": config.ValidateConnect = ValidateConnectTargetSessionAttrsStandby case "prefer-standby": - config.ValidateConnect = ValidateConnectTargetSessionAttrsPrefferStandby + config.ValidateConnect = ValidateConnectTargetSessionAttrsPreferStandby config.HasPreferStandbyTargetSessionAttr = true case "any": // do nothing @@ -816,16 +816,16 @@ func ValidateConnectTargetSessionAttrsPrimary(ctx context.Context, pgConn *PgCon return nil } -// ValidateConnectTargetSessionAttrsPrimary is an ValidateConnectFunc that implements libpq compatible +// ValidateConnectTargetSessionAttrsPreferStandby is an ValidateConnectFunc that implements libpq compatible // target_session_attrs=prefer-standby. -func ValidateConnectTargetSessionAttrsPrefferStandby(ctx context.Context, pgConn *PgConn) error { +func ValidateConnectTargetSessionAttrsPreferStandby(ctx context.Context, pgConn *PgConn) error { result := pgConn.ExecParams(ctx, "select pg_is_in_recovery()", nil, nil, nil, nil).Read() if result.Err != nil { return result.Err } if string(result.Rows[0][0]) != "t" { - return &preferStanbyNotFoundError{err: errors.New("server is not in hot standby mode")} + return &preferStandbyNotFoundError{err: errors.New("server is not in hot standby mode")} } return nil diff --git a/config_test.go b/config_test.go index 6311f1f1..c8d8cee6 100644 --- a/config_test.go +++ b/config_test.go @@ -619,7 +619,7 @@ func TestParseConfig(t *testing.T) { Database: "mydb", TLSConfig: nil, RuntimeParams: map[string]string{}, - ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPrefferStandby, + ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPreferStandby, HasPreferStandbyTargetSessionAttr: true, }, }, diff --git a/errors.go b/errors.go index 2bc74df7..7ed8889c 100644 --- a/errors.go +++ b/errors.go @@ -220,19 +220,19 @@ func redactURL(u *url.URL) string { return u.String() } -type preferStanbyNotFoundError struct { +type preferStandbyNotFoundError struct { err error safeToRetry bool } -func (e *preferStanbyNotFoundError) Error() string { +func (e *preferStandbyNotFoundError) Error() string { return fmt.Sprintf("standby server not found: %s", e.err.Error()) } -func (e *preferStanbyNotFoundError) SafeToRetry() bool { +func (e *preferStandbyNotFoundError) SafeToRetry() bool { return e.safeToRetry } -func (e *preferStanbyNotFoundError) Unwrap() error { +func (e *preferStandbyNotFoundError) Unwrap() error { return e.err } diff --git a/pgconn.go b/pgconn.go index 8e7ac668..1a1d3505 100644 --- a/pgconn.go +++ b/pgconn.go @@ -157,11 +157,18 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err break } else if pgerr, ok := err.(*PgError); ok { err = &connectError{config: config, msg: "server error", err: pgerr} - if checkPgError(pgerr) { + const ERRCODE_INVALID_PASSWORD = "28P01" // wrong password + const ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION = "28000" // wrong password or bad pg_hba.conf settings + const ERRCODE_INVALID_CATALOG_NAME = "3D000" // db does not exist + const ERRCODE_INSUFFICIENT_PRIVILEGE = "42501" // missing connect privilege + if pgerr.Code == ERRCODE_INVALID_PASSWORD || + pgerr.Code == ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION || + pgerr.Code == ERRCODE_INVALID_CATALOG_NAME || + pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE { break } } else if cerr, ok := err.(*connectError); ok && config.HasPreferStandbyTargetSessionAttr { - if _, ok := cerr.err.(*preferStanbyNotFoundError); ok { + if _, ok := cerr.err.(*preferStandbyNotFoundError); ok { fallbackConfig = fc } } @@ -173,7 +180,7 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err if pgerr, ok := err.(*PgError); ok { err = &connectError{config: config, msg: "server error", err: pgerr} } - config.ValidateConnect = ValidateConnectTargetSessionAttrsPrefferStandby + config.ValidateConnect = ValidateConnectTargetSessionAttrsPreferStandby } if err != nil { @@ -191,17 +198,6 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err return pgConn, nil } -func checkPgError(pgerr *PgError) bool { - const ERRCODE_INVALID_PASSWORD = "28P01" // wrong password - const ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION = "28000" // wrong password or bad pg_hba.conf settings - const ERRCODE_INVALID_CATALOG_NAME = "3D000" // db does not exist - const ERRCODE_INSUFFICIENT_PRIVILEGE = "42501" // missing connect privilege - return pgerr.Code == ERRCODE_INVALID_PASSWORD || - pgerr.Code == ERRCODE_INVALID_AUTHORIZATION_SPECIFICATION || - pgerr.Code == ERRCODE_INVALID_CATALOG_NAME || - pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE -} - func expandWithIPs(ctx context.Context, lookupFn LookupFunc, fallbacks []*FallbackConfig) ([]*FallbackConfig, error) { var configs []*FallbackConfig