2
0

Rename Config.AfterConnectFunc to AfterConnect

No need to include the type in the name.
This commit is contained in:
Jack Christensen
2019-07-13 09:52:22 -05:00
parent d2440c7fe6
commit 59941377c8
4 changed files with 18 additions and 18 deletions
+3 -3
View File
@@ -36,10 +36,10 @@ type Config struct {
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
// 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 NoticeHandler
@@ -245,7 +245,7 @@ func ParseConfig(connString string) (*Config, error) {
}
if settings["target_session_attrs"] == "read-write" {
config.AfterConnectFunc = AfterConnectTargetSessionAttrsReadWrite
config.AfterConnect = AfterConnectTargetSessionAttrsReadWrite
} else if settings["target_session_attrs"] != "any" {
return nil, errors.Errorf("unknown target_session_attrs value: %v", settings["target_session_attrs"])
}
+9 -9
View File
@@ -378,14 +378,14 @@ func TestParseConfig(t *testing.T) {
name: "target_session_attrs",
connString: "postgres://jack:secret@localhost:5432/mydb?sslmode=disable&target_session_attrs=read-write",
config: &pgconn.Config{
User: "jack",
Password: "secret",
Host: "localhost",
Port: 5432,
Database: "mydb",
TLSConfig: nil,
RuntimeParams: map[string]string{},
AfterConnectFunc: pgconn.AfterConnectTargetSessionAttrsReadWrite,
User: "jack",
Password: "secret",
Host: "localhost",
Port: 5432,
Database: "mydb",
TLSConfig: nil,
RuntimeParams: map[string]string{},
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)
// 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 expected.TLSConfig != nil {
+3 -3
View File
@@ -201,11 +201,11 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig
}
case *pgproto3.ReadyForQuery:
pgConn.status = connStatusIdle
if config.AfterConnectFunc != nil {
err := config.AfterConnectFunc(ctx, pgConn)
if config.AfterConnect != nil {
err := config.AfterConnect(ctx, pgConn)
if err != nil {
pgConn.conn.Close()
return nil, errors.Errorf("AfterConnectFunc: %v", err)
return nil, errors.Errorf("AfterConnect: %v", err)
}
}
return pgConn, nil
+3 -3
View File
@@ -187,7 +187,7 @@ func TestConnectWithFallback(t *testing.T) {
closeConn(t, conn)
}
func TestConnectWithAfterConnectFunc(t *testing.T) {
func TestConnectWithAfterConnect(t *testing.T) {
t.Parallel()
config, err := pgconn.ParseConfig(os.Getenv("PGX_TEST_CONN_STRING"))
@@ -200,7 +200,7 @@ func TestConnectWithAfterConnectFunc(t *testing.T) {
}
acceptConnCount := 0
config.AfterConnectFunc = func(ctx context.Context, conn *pgconn.PgConn) error {
config.AfterConnect = func(ctx context.Context, conn *pgconn.PgConn) error {
acceptConnCount++
if acceptConnCount < 2 {
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"))
require.NoError(t, err)
config.AfterConnectFunc = pgconn.AfterConnectTargetSessionAttrsReadWrite
config.AfterConnect = pgconn.AfterConnectTargetSessionAttrsReadWrite
config.RuntimeParams["default_transaction_read_only"] = "on"
conn, err := pgconn.ConnectConfig(context.Background(), config)