From be7a7b44dcb873accc7570432599193e777e5d38 Mon Sep 17 00:00:00 2001 From: Jack Christensen Date: Mon, 31 Dec 2018 18:03:55 -0600 Subject: [PATCH] Access PID and SecretKey via method --- conn.go | 6 +++--- pgconn/pgconn.go | 22 ++++++++++++++++------ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/conn.go b/conn.go index 4197d9a2..bb1b4c0c 100644 --- a/conn.go +++ b/conn.go @@ -467,7 +467,7 @@ func (c *Conn) crateDBTypesQuery(err error) (*pgtype.ConnInfo, error) { // PID returns the backend PID for this connection. func (c *Conn) PID() uint32 { - return c.pgConn.PID + return c.pgConn.PID() } // LocalAddr returns the underlying connection's local address @@ -1031,8 +1031,8 @@ func (c *Conn) log(lvl LogLevel, msg string, data map[string]interface{}) { if data == nil { data = map[string]interface{}{} } - if c.pgConn != nil && c.pgConn.PID != 0 { - data["pid"] = c.pgConn.PID + if c.pgConn != nil && c.pgConn.PID() != 0 { + data["pid"] = c.pgConn.PID() } c.logger.Log(lvl, msg, data) diff --git a/pgconn/pgconn.go b/pgconn/pgconn.go index 776141f9..87ba0096 100644 --- a/pgconn/pgconn.go +++ b/pgconn/pgconn.go @@ -59,8 +59,8 @@ var ErrTLSRefused = errors.New("server refused TLS connection") // PgConn is a low-level PostgreSQL connection handle. It is not safe for concurrent usage. type PgConn struct { conn net.Conn // the underlying TCP or unix domain socket connection - PID uint32 // backend pid - SecretKey uint32 // key to use to send a cancel query message to the server + pid uint32 // backend pid + secretKey uint32 // key to use to send a cancel query message to the server parameterStatuses map[string]string // parameters that have been reported by the server TxStatus byte Frontend *pgproto3.Frontend @@ -179,8 +179,8 @@ func connect(ctx context.Context, config *Config, fallbackConfig *FallbackConfig switch msg := msg.(type) { case *pgproto3.BackendKeyData: - pgConn.PID = msg.ProcessID - pgConn.SecretKey = msg.SecretKey + pgConn.pid = msg.ProcessID + pgConn.secretKey = msg.SecretKey case *pgproto3.Authentication: if err = pgConn.rxAuthenticationX(msg); err != nil { pgConn.conn.Close() @@ -304,6 +304,16 @@ func (pgConn *PgConn) Conn() net.Conn { return pgConn.conn } +// PID returns the backend PID. +func (pgConn *PgConn) PID() uint32 { + return pgConn.pid +} + +// SecretKey returns the backend secret key used to send a cancel query message to the server. +func (pgConn *PgConn) SecretKey() uint32 { + return pgConn.secretKey +} + // Close closes a connection. It is safe to call Close on a already closed connection. Close attempts a clean close by // sending the exit message to PostgreSQL. However, this could block so ctx is available to limit the time to wait. The // underlying net.Conn.Close() will always be called regardless of any other errors. @@ -701,8 +711,8 @@ func (pgConn *PgConn) CancelRequest(ctx context.Context) error { buf := make([]byte, 16) binary.BigEndian.PutUint32(buf[0:4], 16) binary.BigEndian.PutUint32(buf[4:8], 80877102) - binary.BigEndian.PutUint32(buf[8:12], uint32(pgConn.PID)) - binary.BigEndian.PutUint32(buf[12:16], uint32(pgConn.SecretKey)) + binary.BigEndian.PutUint32(buf[8:12], uint32(pgConn.pid)) + binary.BigEndian.PutUint32(buf[12:16], uint32(pgConn.secretKey)) _, err = cancelConn.Write(buf) if err != nil { return preferContextOverNetTimeoutError(ctx, err)