Rename Config.AfterConnectFunc to AfterConnect
No need to include the type in the name.
This commit is contained in:
@@ -36,10 +36,10 @@ type Config struct {
|
|||||||
|
|
||||||
Fallbacks []*FallbackConfig
|
Fallbacks []*FallbackConfig
|
||||||
|
|
||||||
// AfterConnectFunc is called after successful connection. It can be used to set up the connection or to validate that
|
// AfterConnect is called after successful connection. It can be used to set up the connection or to validate that
|
||||||
// server is acceptable. If this returns an error the connection is closed and the next fallback config is tried. This
|
// server is acceptable. If this returns an error the connection is closed and the next fallback config is tried. This
|
||||||
// allows implementing high availability behavior such as libpq does with target_session_attrs.
|
// allows implementing high availability behavior such as libpq does with target_session_attrs.
|
||||||
AfterConnectFunc AfterConnectFunc
|
AfterConnect AfterConnectFunc
|
||||||
|
|
||||||
// OnNotice is a callback function called when a notice response is received.
|
// OnNotice is a callback function called when a notice response is received.
|
||||||
OnNotice NoticeHandler
|
OnNotice NoticeHandler
|
||||||
@@ -245,7 +245,7 @@ func ParseConfig(connString string) (*Config, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if settings["target_session_attrs"] == "read-write" {
|
if settings["target_session_attrs"] == "read-write" {
|
||||||
config.AfterConnectFunc = AfterConnectTargetSessionAttrsReadWrite
|
config.AfterConnect = AfterConnectTargetSessionAttrsReadWrite
|
||||||
} else if settings["target_session_attrs"] != "any" {
|
} else if settings["target_session_attrs"] != "any" {
|
||||||
return nil, errors.Errorf("unknown target_session_attrs value: %v", settings["target_session_attrs"])
|
return nil, errors.Errorf("unknown target_session_attrs value: %v", settings["target_session_attrs"])
|
||||||
}
|
}
|
||||||
|
|||||||
+9
-9
@@ -378,14 +378,14 @@ func TestParseConfig(t *testing.T) {
|
|||||||
name: "target_session_attrs",
|
name: "target_session_attrs",
|
||||||
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=read-write",
|
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=read-write",
|
||||||
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{},
|
||||||
AfterConnectFunc: pgconn.AfterConnectTargetSessionAttrsReadWrite,
|
AfterConnect: pgconn.AfterConnectTargetSessionAttrsReadWrite,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -416,7 +416,7 @@ func assertConfigsEqual(t *testing.T, expected, actual *pgconn.Config, testName
|
|||||||
assert.Equalf(t, expected.RuntimeParams, actual.RuntimeParams, "%s - RuntimeParams", testName)
|
assert.Equalf(t, expected.RuntimeParams, actual.RuntimeParams, "%s - RuntimeParams", 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.AfterConnectFunc == nil, actual.AfterConnectFunc == nil, "%s - AfterConnectFunc", testName)
|
assert.Equalf(t, expected.AfterConnect == nil, actual.AfterConnect == nil, "%s - AfterConnect", 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 {
|
||||||
|
|||||||
@@ -201,11 +201,11 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig
|
|||||||
}
|
}
|
||||||
case *pgproto3.ReadyForQuery:
|
case *pgproto3.ReadyForQuery:
|
||||||
pgConn.status = connStatusIdle
|
pgConn.status = connStatusIdle
|
||||||
if config.AfterConnectFunc != nil {
|
if config.AfterConnect != nil {
|
||||||
err := config.AfterConnectFunc(ctx, pgConn)
|
err := config.AfterConnect(ctx, pgConn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pgConn.conn.Close()
|
pgConn.conn.Close()
|
||||||
return nil, errors.Errorf("AfterConnectFunc: %v", err)
|
return nil, errors.Errorf("AfterConnect: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return pgConn, nil
|
return pgConn, nil
|
||||||
|
|||||||
+3
-3
@@ -187,7 +187,7 @@ func TestConnectWithFallback(t *testing.T) {
|
|||||||
closeConn(t, conn)
|
closeConn(t, conn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnectWithAfterConnectFunc(t *testing.T) {
|
func TestConnectWithAfterConnect(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
|
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
|
||||||
@@ -200,7 +200,7 @@ func TestConnectWithAfterConnectFunc(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
acceptConnCount := 0
|
acceptConnCount := 0
|
||||||
config.AfterConnectFunc = func(ctx context.Context, conn *pgconn.PgConn) error {
|
config.AfterConnect = func(ctx context.Context, conn *pgconn.PgConn) error {
|
||||||
acceptConnCount++
|
acceptConnCount++
|
||||||
if acceptConnCount < 2 {
|
if acceptConnCount < 2 {
|
||||||
return errors.New("reject first conn")
|
return errors.New("reject first conn")
|
||||||
@@ -232,7 +232,7 @@ func TestConnectWithAfterConnectTargetSessionAttrsReadWrite(t *testing.T) {
|
|||||||
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
|
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
config.AfterConnectFunc = pgconn.AfterConnectTargetSessionAttrsReadWrite
|
config.AfterConnect = pgconn.AfterConnectTargetSessionAttrsReadWrite
|
||||||
config.RuntimeParams["default_transaction_read_only"] = "on"
|
config.RuntimeParams["default_transaction_read_only"] = "on"
|
||||||
|
|
||||||
conn, err := pgconn.ConnectConfig(context.Background(), config)
|
conn, err := pgconn.ConnectConfig(context.Background(), config)
|
||||||
|
|||||||
Reference in New Issue
Block a user