Skip to content

Commit

Permalink
GODRIVER-2810 Guard rttMonitor connection
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonvasquez committed Oct 27, 2023
1 parent 20d1b17 commit b686f7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ RUN export DEBIAN_FRONTEND=noninteractive && \
tzdata \
gpg \
apt-utils \
libc6-dev \
gcc \
make && \
apt-add-repository ppa:longsleep/golang-backports && \
apt-get -qq update && \
Expand Down
32 changes: 26 additions & 6 deletions x/mongo/driver/topology/rtt_monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ type rttConfig struct {
}

type rttMonitor struct {
mu sync.RWMutex // mu guards samples, offset, minRTT, averageRTT, and averageRTTSet
mu sync.RWMutex // mu guards samples, offset, minRTT, averageRTT, and averageRTTSet

// connMu guards connecting and disconnecting. This is necessary since
// disconnecting will await the cancelation of a started connection. The
// use case for rttMonitor.connect needs to be goroutine safe.
connMu sync.Mutex
samples []time.Duration
offset int
minRTT time.Duration
Expand All @@ -52,6 +57,7 @@ type rttMonitor struct {
ctx context.Context
cancelFn context.CancelFunc
started bool
done chan struct{}
}

var _ driver.RTTMonitor = &rttMonitor{}
Expand All @@ -75,20 +81,34 @@ func newRTTMonitor(cfg *rttConfig) *rttMonitor {
}

func (r *rttMonitor) connect() {
r.closeWg.Add(1)
r.connMu.Lock()
defer r.connMu.Unlock()

r.started = true
go r.start()
r.closeWg.Add(1)

go func() {
defer r.closeWg.Done()

r.start()
}()
}

func (r *rttMonitor) disconnect() {
// Signal for the routine to stop.
r.connMu.Lock()
defer r.connMu.Unlock()

if !r.started {
return
}

r.cancelFn()

// Wait for the existing connection to complete.
r.closeWg.Wait()
}

func (r *rttMonitor) start() {
defer r.closeWg.Done()

var conn *connection
defer func() {
if conn != nil {
Expand Down

0 comments on commit b686f7c

Please sign in to comment.