From 618a12a094636d97ec67a2950adc1dd7dac37241 Mon Sep 17 00:00:00 2001 From: "sergey.bashilov" Date: Fri, 24 Jun 2022 14:02:59 +0300 Subject: [PATCH] remove HasPreferStandbyTargetSessionAttr, rename error to indicate server is not standby --- config.go | 5 +---- config_test.go | 18 ++++++++---------- errors.go | 8 ++++---- pgconn.go | 4 ++-- 4 files changed, 15 insertions(+), 20 deletions(-) diff --git a/config.go b/config.go index dac7b95b..4ca09dda 100644 --- a/config.go +++ b/config.go @@ -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. ValidateConnect ValidateConnectFunc - HasPreferStandbyTargetSessionAttr bool - // 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. AfterConnect AfterConnectFunc @@ -371,7 +369,6 @@ func ParseConfig(connString string) (*Config, error) { config.ValidateConnect = ValidateConnectTargetSessionAttrsStandby case "prefer-standby": config.ValidateConnect = ValidateConnectTargetSessionAttrsPreferStandby - config.HasPreferStandbyTargetSessionAttr = true case "any": // do nothing default: @@ -825,7 +822,7 @@ func ValidateConnectTargetSessionAttrsPreferStandby(ctx context.Context, pgConn } 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 diff --git a/config_test.go b/config_test.go index c8d8cee6..6b48ea27 100644 --- a/config_test.go +++ b/config_test.go @@ -612,15 +612,14 @@ func TestParseConfig(t *testing.T) { name: "target_session_attrs prefer-standby", connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=prefer-standby", config: &pgconn.Config{ - User: "jack", - Password: "secret", - Host: "localhost", - Port: 5432, - Database: "mydb", - TLSConfig: nil, - RuntimeParams: map[string]string{}, - ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPreferStandby, - HasPreferStandbyTargetSessionAttr: true, + User: "jack", + Password: "secret", + Host: "localhost", + Port: 5432, + Database: "mydb", + TLSConfig: nil, + RuntimeParams: map[string]string{}, + ValidateConnect: pgconn.ValidateConnectTargetSessionAttrsPreferStandby, }, }, { @@ -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. 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.HasPreferStandbyTargetSessionAttr, actual.HasPreferStandbyTargetSessionAttr, "%s - HasPreferStandbyTargetSessionAttr", testName) if assert.Equalf(t, expected.TLSConfig == nil, actual.TLSConfig == nil, "%s - TLSConfig", testName) { if expected.TLSConfig != nil { diff --git a/errors.go b/errors.go index 7ed8889c..9f04476d 100644 --- a/errors.go +++ b/errors.go @@ -220,19 +220,19 @@ func redactURL(u *url.URL) string { return u.String() } -type preferStandbyNotFoundError struct { +type NotStandbyError struct { err error safeToRetry bool } -func (e *preferStandbyNotFoundError) Error() string { +func (e *NotStandbyError) Error() string { return fmt.Sprintf("standby server not found: %s", e.err.Error()) } -func (e *preferStandbyNotFoundError) SafeToRetry() bool { +func (e *NotStandbyError) SafeToRetry() bool { return e.safeToRetry } -func (e *preferStandbyNotFoundError) Unwrap() error { +func (e *NotStandbyError) Unwrap() error { return e.err } diff --git a/pgconn.go b/pgconn.go index 1a1d3505..5e436ffe 100644 --- a/pgconn.go +++ b/pgconn.go @@ -167,8 +167,8 @@ func ConnectConfig(ctx context.Context, config *Config) (pgConn *PgConn, err err pgerr.Code == ERRCODE_INSUFFICIENT_PRIVILEGE { break } - } else if cerr, ok := err.(*connectError); ok && config.HasPreferStandbyTargetSessionAttr { - if _, ok := cerr.err.(*preferStandbyNotFoundError); ok { + } else if cerr, ok := err.(*connectError); ok { + if _, ok := cerr.err.(*NotStandbyError); ok { fallbackConfig = fc } }