CancelRequest: Wait for the cancel request to be acknowledged by the server

pull/1789/head
Anton Levakin 2023-10-13 19:31:41 +02:00 committed by Jack Christensen
parent 5d0f904831
commit 304697de36
1 changed files with 8 additions and 9 deletions

View File

@ -942,17 +942,16 @@ func (pgConn *PgConn) CancelRequest(ctx context.Context) error {
buf := make([]byte, 16) buf := make([]byte, 16)
binary.BigEndian.PutUint32(buf[0:4], 16) binary.BigEndian.PutUint32(buf[0:4], 16)
binary.BigEndian.PutUint32(buf[4:8], 80877102) binary.BigEndian.PutUint32(buf[4:8], 80877102)
binary.BigEndian.PutUint32(buf[8:12], uint32(pgConn.pid)) binary.BigEndian.PutUint32(buf[8:12], pgConn.pid)
binary.BigEndian.PutUint32(buf[12:16], uint32(pgConn.secretKey)) binary.BigEndian.PutUint32(buf[12:16], pgConn.secretKey)
_, err = cancelConn.Write(buf)
if err != nil { if _, err := cancelConn.Write(buf); err != nil {
return err return fmt.Errorf("write to connection for cancellation: %w", err)
} }
_, err = cancelConn.Read(buf) // Wait for the cancel request to be acknowledged by the server.
if err != io.EOF { // It copies the behavior of the libpq: https://github.com/postgres/postgres/blob/REL_16_0/src/interfaces/libpq/fe-connect.c#L4946-L4960
return err _, _ = cancelConn.Read(buf)
}
return nil return nil
} }