Skip to content

Commit bcf3b86

Browse files
GODRIVER-2952 Update context.Canceled equality comparisons (#1490)
Co-authored-by: Ramit Mittal <[email protected]>
1 parent c14556a commit bcf3b86

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

x/mongo/driver/topology/connection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ func transformNetworkError(ctx context.Context, originalError error, contextDead
314314
}
315315

316316
// If there was an error and the context was cancelled, we assume it happened due to the cancellation.
317-
if ctx.Err() == context.Canceled {
317+
if errors.Is(ctx.Err(), context.Canceled) {
318318
return context.Canceled
319319
}
320320

@@ -858,15 +858,15 @@ func newCancellListener() *cancellListener {
858858

859859
// Listen blocks until the provided context is cancelled or listening is aborted
860860
// via the StopListening function. If this detects that the context has been
861-
// cancelled (i.e. ctx.Err() == context.Canceled), the provided callback is
861+
// cancelled (i.e. errors.Is(ctx.Err(), context.Canceled), the provided callback is
862862
// called to abort in-progress work. Even if the context expires, this function
863863
// will block until StopListening is called.
864864
func (c *cancellListener) Listen(ctx context.Context, abortFn func()) {
865865
c.aborted = false
866866

867867
select {
868868
case <-ctx.Done():
869-
if ctx.Err() == context.Canceled {
869+
if errors.Is(ctx.Err(), context.Canceled) {
870870
c.aborted = true
871871
abortFn()
872872
}

x/mongo/driver/topology/connection_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ func TestConnection(t *testing.T) {
283283
assert.True(t, ok, "expected error %v to be of type %T", connectErr, ConnectionError{})
284284

285285
isTimeout := func(err error) bool {
286-
if err == context.DeadlineExceeded {
286+
if errors.Is(err, context.DeadlineExceeded) {
287287
return true
288288
}
289289
if ne, ok := err.(net.Error); ok {

x/mongo/driver/topology/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func (s *Server) ProcessError(err error, conn driver.Connection) driver.ProcessE
525525
if netErr, ok := wrappedConnErr.(net.Error); ok && netErr.Timeout() {
526526
return driver.NoChange
527527
}
528-
if wrappedConnErr == context.Canceled || wrappedConnErr == context.DeadlineExceeded {
528+
if errors.Is(wrappedConnErr, context.Canceled) || errors.Is(wrappedConnErr, context.DeadlineExceeded) {
529529
return driver.NoChange
530530
}
531531

@@ -625,7 +625,7 @@ func (s *Server) update() {
625625
// Retry after the first timeout before clearing the pool in case of a FAAS pause as
626626
// described in GODRIVER-2577.
627627
if err := unwrapConnectionError(desc.LastError); err != nil && timeoutCnt < 1 {
628-
if err == context.Canceled || err == context.DeadlineExceeded {
628+
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
629629
timeoutCnt++
630630
// We want to immediately retry on timeout error. Continue to next loop.
631631
return true

0 commit comments

Comments
 (0)