MCPcopy
hub / github.com/jackc/pgx / HandleCancel

Method HandleCancel

pgconn/pgconn.go:2978–3006  ·  view source on GitHub ↗
(context.Context)

Source from the content-addressed store, hash-verified

2976}
2977
2978func (h *CancelRequestContextWatcherHandler) HandleCancel(context.Context) {
2979 h.cancelFinishedChan = make(chan struct{})
2980 var handleUnwatchedAfterCancelCalledCtx context.Context
2981 handleUnwatchedAfterCancelCalledCtx, h.handleUnwatchAfterCancelCalled = context.WithCancel(context.Background())
2982
2983 deadline := time.Now().Add(h.DeadlineDelay)
2984 h.Conn.conn.SetDeadline(deadline)
2985
2986 go func() {
2987 defer close(h.cancelFinishedChan)
2988
2989 select {
2990 case <-handleUnwatchedAfterCancelCalledCtx.Done():
2991 return
2992 case <-time.After(h.CancelRequestDelay):
2993 }
2994
2995 cancelRequestCtx, cancel := context.WithDeadline(handleUnwatchedAfterCancelCalledCtx, deadline)
2996 defer cancel()
2997 h.Conn.CancelRequest(cancelRequestCtx)
2998
2999 // CancelRequest is inherently racy. Even though the cancel request has been received by the server at this point,
3000 // it hasn't necessarily been delivered to the other connection. If we immediately return and the connection is
3001 // immediately used then it is possible the CancelRequest will actually cancel our next query. The
3002 // TestCancelRequestContextWatcherHandler Stress test can produce this error without the sleep below. The sleep time
3003 // is arbitrary, but should be sufficient to prevent this error case.
3004 time.Sleep(100 * time.Millisecond)
3005 }()
3006}
3007
3008func (h *CancelRequestContextWatcherHandler) HandleUnwatchAfterCancel() {
3009 h.handleUnwatchAfterCancelCalled()

Callers

nothing calls this directly

Calls 2

CancelRequestMethod · 0.80
SetDeadlineMethod · 0.45

Tested by

no test coverage detected