Skip to content

Commit

Permalink
CancelRequest: Wait for the cancel request to be acknowledged by the …
Browse files Browse the repository at this point in the history
…server
  • Loading branch information
levakin authored and jackc committed Oct 14, 2023
1 parent 5d0f904 commit 304697d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions pgconn/pgconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -942,18 +942,17 @@ 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))
_, err = cancelConn.Write(buf)
if err != nil {
return err
}
binary.BigEndian.PutUint32(buf[8:12], pgConn.pid)
binary.BigEndian.PutUint32(buf[12:16], pgConn.secretKey)

_, err = cancelConn.Read(buf)
if err != io.EOF {
return err
if _, err := cancelConn.Write(buf); err != nil {
return fmt.Errorf("write to connection for cancellation: %w", err)
}

// Wait for the cancel request to be acknowledged by the server.
// It copies the behavior of the libpq: https://github.com/postgres/postgres/blob/REL_16_0/src/interfaces/libpq/fe-connect.c#L4946-L4960
_, _ = cancelConn.Read(buf)

return nil
}

Expand Down

0 comments on commit 304697d

Please sign in to comment.