fix typos

pull/1281/head
sergey.bashilov 2022-06-20 12:15:15 +03:00 committed by Jack Christensen
parent 25935a39b6
commit 1b6543f29c
4 changed files with 19 additions and 23 deletions

View File

@ -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

View File

@ -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,
},
},

View File

@ -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
}

View File

@ -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