Skip to content

Commit 7d452ea

Browse files
committed
bugfix: ConnectionMulti update addresses data race
We need to update an addresses slice under a lock because we use the slice in ConnectionMulti.getCurrentConnection(). Part of #218
1 parent 44cf220 commit 7d452ea

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

multi/multi.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,9 @@ func (connMulti *ConnectionMulti) checker() {
212212
connMulti.deleteConnectionFromPool(v)
213213
}
214214
}
215+
connMulti.mutex.Lock()
215216
connMulti.addrs = addrs
217+
connMulti.mutex.Unlock()
216218
}
217219
case <-timer.C:
218220
for _, addr := range connMulti.addrs {

multi/multi_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,18 @@ func TestRefresh(t *testing.T) {
200200
t.Errorf("conn is nil after Connect")
201201
return
202202
}
203+
204+
multiConn.mutex.RLock()
203205
curAddr := multiConn.addrs[0]
206+
multiConn.mutex.RUnlock()
204207

205208
// Wait for refresh timer.
206209
// Scenario 1 nodeload, 1 refresh, 1 nodeload.
207210
time.Sleep(10 * time.Second)
208211

212+
multiConn.mutex.RLock()
209213
newAddr := multiConn.addrs[0]
214+
multiConn.mutex.RUnlock()
210215

211216
if curAddr == newAddr {
212217
t.Errorf("Expect address refresh")

0 commit comments

Comments
 (0)