Fix concurrent write crash. Fixes #578
This commit is contained in:
parent
36921bb089
commit
c812a0abf5
@ -2,9 +2,12 @@ package utils
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"sort"
|
"sort"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var l = sync.Mutex{}
|
||||||
|
|
||||||
// The "start" timestamp of a timing event.
|
// The "start" timestamp of a timing event.
|
||||||
var _pointsInTime = make(map[string]time.Time)
|
var _pointsInTime = make(map[string]time.Time)
|
||||||
|
|
||||||
@ -13,10 +16,12 @@ var _durationStorage = make(map[string][]float64)
|
|||||||
|
|
||||||
// StartPerformanceMonitor will keep track of the start time of this event.
|
// StartPerformanceMonitor will keep track of the start time of this event.
|
||||||
func StartPerformanceMonitor(key string) {
|
func StartPerformanceMonitor(key string) {
|
||||||
|
l.Lock()
|
||||||
if len(_durationStorage[key]) > 20 {
|
if len(_durationStorage[key]) > 20 {
|
||||||
_durationStorage[key] = removeHighValue(_durationStorage[key])
|
_durationStorage[key] = removeHighValue(_durationStorage[key])
|
||||||
}
|
}
|
||||||
_pointsInTime[key] = time.Now()
|
_pointsInTime[key] = time.Now()
|
||||||
|
l.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAveragePerformance will return the average durations for the event.
|
// GetAveragePerformance will return the average durations for the event.
|
||||||
@ -26,12 +31,15 @@ func GetAveragePerformance(key string) float64 {
|
|||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
l.Lock()
|
||||||
delta := time.Since(timestamp).Seconds()
|
delta := time.Since(timestamp).Seconds()
|
||||||
_durationStorage[key] = append(_durationStorage[key], delta)
|
_durationStorage[key] = append(_durationStorage[key], delta)
|
||||||
if len(_durationStorage[key]) < 8 {
|
if len(_durationStorage[key]) < 8 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
_durationStorage[key] = removeHighValue(_durationStorage[key])
|
_durationStorage[key] = removeHighValue(_durationStorage[key])
|
||||||
|
l.Unlock()
|
||||||
|
|
||||||
return avg(_durationStorage[key])
|
return avg(_durationStorage[key])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user