fix(router): panic caused by concurrent map read and write (#2678)

This commit is contained in:
Rinka 2023-08-31 18:01:06 +08:00 committed by GitHub
parent 88b5b551c0
commit 87155bfc71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 0 deletions

View File

@ -4,6 +4,7 @@ import (
"regexp"
"strconv"
"strings"
"sync"
)
type weightScaler func(value, weight float64) float64
@ -26,10 +27,13 @@ type WeightManager struct {
cache map[string]float64
scaler weightScaler
defaultWeight float64
mu sync.Mutex
}
// Get gets the weight of specified tag
func (s *WeightManager) Get(tag string) float64 {
s.mu.Lock()
defer s.mu.Unlock()
weight, ok := s.cache[tag]
if ok {
return weight