diff --git a/conn.go b/conn.go index 380691cb..72e9652c 100644 --- a/conn.go +++ b/conn.go @@ -48,7 +48,7 @@ type Conn struct { reader *bufio.Reader // buffered reader to improve read performance wbuf [1024]byte writeBuf WriteBuf - PID int32 // backend pid + pid int32 // backend pid SecretKey int32 // key to use to send a cancel query message to the server RuntimeParams map[string]string // parameters that have been reported by the server PgTypes map[OID]PgType // oids to PgTypes @@ -381,6 +381,11 @@ func (c *Conn) loadInetConstants() error { return nil } +// PID returns the backend PID for this connection. +func (c *Conn) PID() int32 { + return c.pid +} + // Close closes a connection. It is safe to call Close on a already closed // connection. func (c *Conn) Close() (err error) { @@ -1140,7 +1145,7 @@ func (c *Conn) rxErrorResponse(r *msgReader) (err PgError) { } func (c *Conn) rxBackendKeyData(r *msgReader) { - c.PID = r.readInt32() + c.pid = r.readInt32() c.SecretKey = r.readInt32() } @@ -1251,7 +1256,7 @@ func (c *Conn) shouldLog(lvl int) bool { } func (c *Conn) log(lvl int, msg string, ctx ...interface{}) { - if c.PID != 0 { + if c.pid != 0 { ctx = append(ctx, "pid", c.PID) } diff --git a/conn_pool_test.go b/conn_pool_test.go index 212a79d9..f6f166d8 100644 --- a/conn_pool_test.go +++ b/conn_pool_test.go @@ -428,7 +428,7 @@ func TestPoolReleaseDiscardsDeadConnections(t *testing.T) { } }() - if _, err = c2.Exec("select pg_terminate_backend($1)", c1.PID); err != nil { + if _, err = c2.Exec("select pg_terminate_backend($1)", c1.PID()); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -599,7 +599,7 @@ func TestConnPoolBeginRetry(t *testing.T) { pool.Release(victimConn) // Terminate connection that was released to pool - if _, err = killerConn.Exec("select pg_terminate_backend($1)", victimConn.PID); err != nil { + if _, err = killerConn.Exec("select pg_terminate_backend($1)", victimConn.PID()); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -616,7 +616,7 @@ func TestConnPoolBeginRetry(t *testing.T) { if err != nil { t.Fatalf("tx.QueryRow Scan failed: %v", err) } - if txPID == victimConn.PID { + if txPID == victimConn.PID() { t.Error("Expected txPID to defer from killed conn pid, but it didn't") } }() diff --git a/conn_test.go b/conn_test.go index 10f40552..ecd7e88d 100644 --- a/conn_test.go +++ b/conn_test.go @@ -27,7 +27,7 @@ func TestConnect(t *testing.T) { t.Error("Runtime parameters not stored") } - if conn.PID == 0 { + if conn.PID() == 0 { t.Error("Backend PID not stored") } @@ -1255,7 +1255,7 @@ func TestFatalRxError(t *testing.T) { } defer otherConn.Close() - if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.PID); err != nil { + if _, err := otherConn.Exec("select pg_terminate_backend($1)", conn.PID()); err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } @@ -1281,7 +1281,7 @@ func TestFatalTxError(t *testing.T) { } defer otherConn.Close() - _, err = otherConn.Exec("select pg_terminate_backend($1)", conn.PID) + _, err = otherConn.Exec("select pg_terminate_backend($1)", conn.PID()) if err != nil { t.Fatalf("Unable to kill backend PostgreSQL process: %v", err) } diff --git a/v3.md b/v3.md index 58887aed..424635fc 100644 --- a/v3.md +++ b/v3.md @@ -16,6 +16,8 @@ Replace BeginIso with BeginEx. BeginEx adds support for read/write mode and defe Transaction isolation level constants are now typed strings instead of bare strings. +Conn.Pid changed to accessor method Conn.PID() + ## TODO / Possible / Investigate Organize errors better