Use atomic.Load to access fields used in /varz and /subsz requests.

* Includes a unit test that checks all endpoints for data races.
This commit is contained in:
Colin Sullivan
2017-03-01 16:13:20 -07:00
parent 9dd2f7cb65
commit 3f8a0d8b4a
3 changed files with 104 additions and 10 deletions

View File

@@ -473,11 +473,11 @@ func (s *Server) HandleVarz(w http.ResponseWriter, r *http.Request) {
v.TotalConnections = s.totalClients
v.Routes = len(s.routes)
v.Remotes = len(s.remotes)
v.InMsgs = s.inMsgs
v.InBytes = s.inBytes
v.OutMsgs = s.outMsgs
v.OutBytes = s.outBytes
v.SlowConsumers = s.slowConsumers
v.InMsgs = atomic.LoadInt64(&s.inMsgs)
v.InBytes = atomic.LoadInt64(&s.inBytes)
v.OutMsgs = atomic.LoadInt64(&s.outMsgs)
v.OutBytes = atomic.LoadInt64(&s.outBytes)
v.SlowConsumers = atomic.LoadInt64(&s.slowConsumers)
v.Subscriptions = s.sl.Count()
s.httpReqStats[VarzPath]++
// Need a copy here since s.httpReqStas can change while doing

View File

@@ -503,11 +503,11 @@ func (s *Sublist) Stats() *SublistStats {
st := &SublistStats{}
st.NumSubs = s.count
st.NumCache = uint32(len(s.cache))
st.NumInserts = s.inserts
st.NumRemoves = s.removes
st.NumMatches = s.matches
if s.matches > 0 {
st.CacheHitRate = float64(s.cacheHits) / float64(s.matches)
st.NumInserts = atomic.LoadUint64(&s.inserts)
st.NumRemoves = atomic.LoadUint64(&s.removes)
st.NumMatches = atomic.LoadUint64(&s.matches)
if st.NumMatches > 0 {
st.CacheHitRate = float64(atomic.LoadUint64(&s.cacheHits)) / float64(st.NumMatches)
}
// whip through cache for fanout stats
tot, max := 0, 0