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

View File

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

View File

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

View File

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