Преглед на файлове

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

Rinka преди 2 години
родител
ревизия
87155bfc71
променени са 1 файла, в които са добавени 4 реда и са изтрити 0 реда
  1. 4 0
      app/router/weight.go

+ 4 - 0
app/router/weight.go

@@ -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