remove HasPreferStandbyTargetSessionAttr, rename error to indicate server is not standby

pull/1281/head
sergey.bashilov 2022-06-24 14:02:59 +03:00 committed by Jack Christensen
parent 1b6543f29c
commit 618a12a094
4 changed files with 15 additions and 20 deletions

View File

@ -50,8 +50,6 @@ type Config struct {
// fallback config is tried. This allows implementing high availability behavior such as libpq does with target_session_attrs. // fallback config is tried. This allows implementing high availability behavior such as libpq does with target_session_attrs.
ValidateConnect ValidateConnectFunc ValidateConnect ValidateConnectFunc
HasPreferStandbyTargetSessionAttr bool
// AfterConnect is called after ValidateConnect. It can be used to set up the connection (e.g. Set session variables // AfterConnect is called after ValidateConnect. It can be used to set up the connection (e.g. Set session variables
// or prepare statements). If this returns an error the connection attempt fails. // or prepare statements). If this returns an error the connection attempt fails.
AfterConnect AfterConnectFunc AfterConnect AfterConnectFunc
@ -371,7 +369,6 @@ func ParseConfig(connString string) (*Config, error) {
config.ValidateConnect = ValidateConnectTargetSessionAttrsStandby config.ValidateConnect = ValidateConnectTargetSessionAttrsStandby
case "prefer-standby": case "prefer-standby":
config.ValidateConnect = ValidateConnectTargetSessionAttrsPreferStandby config.ValidateConnect = ValidateConnectTargetSessionAttrsPreferStandby
config.HasPreferStandbyTargetSessionAttr = true
case "any": case "any":
// do nothing // do nothing
default: default:
@ -825,7 +822,7 @@ func ValidateConnectTargetSessionAttrsPreferStandby(ctx context.Context, pgConn
} }
if string(result.Rows[0][0]) != "t" { if string(result.Rows[0][0]) != "t" {
return &preferStandbyNotFoundError{err: errors.New("server is not in hot standby mode")} return &NotStandbyError{err: errors.New("server is not in hot standby mode")}
} }
return nil return nil

View File

@ -612,15 +612,14 @@ func TestParseConfig(t *testing.T) {
name: "target_session_attrs prefer-standby", name: "target_session_attrs prefer-standby",
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=prefer-standby", connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=prefer-standby",
config: &pgconn.Config{ config: &pgconn.Config{
User: "jack", User: "jack",
Password: "secret", Password: "secret",
Host: "localhost", Host: "localhost",
Port: 5432, Port: 5432,
Database: "mydb", Database: "mydb",
TLSConfig: nil, TLSConfig: nil,
RuntimeParams: map[string]string{}, RuntimeParams: map[string]string{},
ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPreferStandby, ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPreferStandby,
HasPreferStandbyTargetSessionAttr: true,
}, },
}, },
{ {
@ -785,7 +784,6 @@ func assertConfigsEqual(t *testing.T, expected, actual *pgconn.Config, testName
// Can't test function equality, so just test that they are set or not. // Can't test function equality, so just test that they are set or not.
assert.Equalf(t, expected.ValidateConnect == nil, actual.ValidateConnect == nil, "%s - ValidateConnect", testName) assert.Equalf(t, expected.ValidateConnect == nil, actual.ValidateConnect == nil, "%s - ValidateConnect", testName)
assert.Equalf(t, expected.AfterConnect == nil, actual.AfterConnect == nil, "%s - AfterConnect", testName) assert.Equalf(t, expected.AfterConnect == nil, actual.AfterConnect == nil, "%s - AfterConnect", testName)
assert.Equalf(t, expected.HasPreferStandbyTargetSessionAttr, actual.HasPreferStandbyTargetSessionAttr, "%s - HasPreferStandbyTargetSessionAttr", testName)
if assert.Equalf(t, expected.TLSConfig == nil, actual.TLSConfig == nil, "%s - TLSConfig", testName) { if assert.Equalf(t, expected.TLSConfig == nil, actual.TLSConfig == nil, "%s - TLSConfig", testName) {
if expected.TLSConfig != nil { if expected.TLSConfig != nil {

View File

@ -220,19 +220,19 @@ func redactURL(u *url.URL) string {
return u.String() return u.String()
} }
type preferStandbyNotFoundError struct { type NotStandbyError struct {
err error err error
safeToRetry bool safeToRetry bool
} }
func (e *preferStandbyNotFoundError) Error() string { func (e *NotStandbyError) Error() string {
return fmt.Sprintf("standby server not found: %s", e.err.Error()) return fmt.Sprintf("standby server not found: %s", e.err.Error())
} }
func (e *preferStandbyNotFoundError) SafeToRetry() bool { func (e *NotStandbyError) SafeToRetry() bool {
return e.safeToRetry return e.safeToRetry
} }
func (e *preferStandbyNotFoundError) Unwrap() error { func (e *NotStandbyError) Unwrap() error {
return e.err return e.err
} }

View File

@ -167,8 +167,8 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err
pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE { pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE {
break break
} }
} else if cerr, ok := err.(*connectError); ok && config.HasPreferStandbyTargetSessionAttr { } else if cerr, ok := err.(*connectError); ok {
if _, ok := cerr.err.(*preferStandbyNotFoundError); ok { if _, ok := cerr.err.(*NotStandbyError); ok {
fallbackConfig = fc fallbackConfig = fc
} }
} }