Skip to content

Commit 064dc86

Browse files
author
Divjot Arora
committed
shane's comments
1 parent a354fd8 commit 064dc86

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

x/mongo/driver/topology/server.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func (s *Server) Disconnect(ctx context.Context) error {
217217
// been cancelled.
218218
s.globalCtxCancel()
219219
s.cancelCheck()
220-
s.done <- struct{}{}
220+
close(s.done)
221221

222222
s.rttMonitor.disconnect()
223223
err := s.pool.disconnect(ctx)
@@ -427,40 +427,33 @@ func (s *Server) update() {
427427
}
428428
}
429429

430-
var disconnectedWhileWaiting bool
431430
waitUntilNextCheck := func() {
432431
// Wait until heartbeatFrequency elapses, an application operation requests an immediate check, or the server
433432
// is disconnecting.
434433
select {
435434
case <-heartbeatTicker.C:
436435
case <-checkNow:
437436
case <-done:
438-
disconnectedWhileWaiting = true
437+
// Return because the next update iteration will check the done channel again and clean up.
439438
return
440439
}
441440

442441
// Ensure we only return if minHeartbeatFrequency has elapsed or the server is disconnecting.
443442
select {
444443
case <-rateLimiter.C:
445444
case <-done:
446-
disconnectedWhileWaiting = true
447445
return
448446
}
449447
}
450448

451449
for {
452-
// Check if the server is disconnecting. If the disconnect happened between the next check and now, we'll read
453-
// a value from the done channel. If it happened during waitUntilNextCheck, the value has already been read
454-
// from the channel, so we check the disconnectedWhileWaiting flag.
455-
var disconnecting bool
450+
// Check if the server is disconnecting. Even if waitForNextCheck has already read from the done channel, we
451+
// can safely read from it again because Disconnect closes the channel.
456452
select {
457453
case <-done:
458-
disconnecting = true
459-
default:
460-
}
461-
if disconnecting || disconnectedWhileWaiting {
462454
closeServer()
463455
return
456+
default:
464457
}
465458

466459
previousDescription := s.Description()
@@ -479,6 +472,10 @@ func (s *Server) update() {
479472
}
480473

481474
s.updateDescription(desc)
475+
if desc.LastError != nil {
476+
// Clear the pool once the description has been updated to Unknown.
477+
s.pool.clear()
478+
}
482479

483480
// If the server supports streaming or we're already streaming, we want to move to streaming the next response
484481
// without waiting. If the server has transitioned to Unknown from a network error, we want to do another
@@ -674,9 +671,9 @@ func (s *Server) check() (description.Server, error) {
674671
return emptyDescription, errCheckCancelled
675672
}
676673

677-
// An error occurred. We clear the pool for all errors and return an Unknown description.
674+
// An error occurred. We reset the RTT monitor for all errors and return an Unknown description. The pool must
675+
// also be cleared, but only after the description has already been updated, so that is handled by the caller.
678676
s.rttMonitor.reset()
679-
s.pool.clear()
680677
return description.NewServerFromError(s.address, err), nil
681678
}
682679

0 commit comments

Comments
 (0)