fix: avoid crash copy map before reading (#8825)

code of this form is always racy, when the
map itself is being written to as well

```
func (r Map) retMap() map[string]string {
     .. lock ..
     return r.internalMap
}

func (r Map) addMap(k, v string) {
     .. lock ..
     r.internalMap[k] = v
}
```

Anyone reading from `retMap()` is not protected
because of locking and we need to make sure
to avoid code in this manner. Always safe to
copy the map and return.
master
Harshavardhana 5 years ago committed by GitHub
parent 080e0c2323
commit b1ad99edbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 22
      cmd/http-stats.go

@ -95,7 +95,7 @@ func newConnStats() *ConnStats {
// HTTPAPIStats holds statistics information about // HTTPAPIStats holds statistics information about
// a given API in the requests. // a given API in the requests.
type HTTPAPIStats struct { type HTTPAPIStats struct {
APIStats map[string]int apiStats map[string]int
sync.RWMutex sync.RWMutex
} }
@ -106,14 +106,14 @@ func (stats *HTTPAPIStats) Inc(api string) {
if stats == nil { if stats == nil {
return return
} }
if stats.APIStats == nil { if stats.apiStats == nil {
stats.APIStats = make(map[string]int) stats.apiStats = make(map[string]int)
} }
if _, ok := stats.APIStats[api]; ok { if _, ok := stats.apiStats[api]; ok {
stats.APIStats[api]++ stats.apiStats[api]++
return return
} }
stats.APIStats[api] = 1 stats.apiStats[api] = 1
} }
// Dec increments the api stats counter. // Dec increments the api stats counter.
@ -123,8 +123,8 @@ func (stats *HTTPAPIStats) Dec(api string) {
if stats == nil { if stats == nil {
return return
} }
if val, ok := stats.APIStats[api]; ok && val > 0 { if val, ok := stats.apiStats[api]; ok && val > 0 {
stats.APIStats[api]-- stats.apiStats[api]--
} }
} }
@ -132,7 +132,11 @@ func (stats *HTTPAPIStats) Dec(api string) {
func (stats *HTTPAPIStats) Load() map[string]int { func (stats *HTTPAPIStats) Load() map[string]int {
stats.Lock() stats.Lock()
defer stats.Unlock() defer stats.Unlock()
return stats.APIStats var apiStats = make(map[string]int, len(stats.apiStats))
for k, v := range stats.apiStats {
apiStats[k] = v
}
return apiStats
} }
// HTTPStats holds statistics information about // HTTPStats holds statistics information about

Loading…
Cancel
Save