mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-03-11 15:34:56 +00:00
lib/promscrape: remove possible data race when cleaning up internStringsMap
This commit is contained in:
parent
82e34984dd
commit
c75d0095f5
1 changed files with 9 additions and 4 deletions
|
@ -1222,26 +1222,31 @@ func internLabelStrings(labels []prompbmarshal.Label) {
|
|||
}
|
||||
|
||||
func internString(s string) string {
|
||||
if v, ok := internStringsMap.Load(s); ok {
|
||||
m := internStringsMap.Load().(*sync.Map)
|
||||
if v, ok := m.Load(s); ok {
|
||||
sp := v.(*string)
|
||||
return *sp
|
||||
}
|
||||
// Make a new copy for s in order to remove references from possible bigger string s refers to.
|
||||
sCopy := string(append([]byte{}, s...))
|
||||
internStringsMap.Store(sCopy, &sCopy)
|
||||
m.Store(sCopy, &sCopy)
|
||||
n := atomic.AddUint64(&internStringsMapLen, 1)
|
||||
if n > 100e3 {
|
||||
atomic.StoreUint64(&internStringsMapLen, 0)
|
||||
internStringsMap = &sync.Map{}
|
||||
internStringsMap.Store(&sync.Map{})
|
||||
}
|
||||
return sCopy
|
||||
}
|
||||
|
||||
var (
|
||||
internStringsMap = &sync.Map{}
|
||||
internStringsMap atomic.Value
|
||||
internStringsMapLen uint64
|
||||
)
|
||||
|
||||
func init() {
|
||||
internStringsMap.Store(&sync.Map{})
|
||||
}
|
||||
|
||||
func getParamsFromLabels(labels []prompbmarshal.Label, paramsOrig map[string][]string) map[string][]string {
|
||||
// See https://www.robustperception.io/life-of-a-label
|
||||
m := make(map[string][]string)
|
||||
|
|
Loading…
Reference in a new issue