From 2f156c7add3a4026af92c7a9626ec2ea85e17f61 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 --- pgconn.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/pgconn.go b/pgconn.go index 776141f9..87ba0096 100644 --- a/pgconn.go +++ b/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)