Browse Source

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

Rinka 2 years ago
parent
commit
87155bfc71
1 changed files with 4 additions and 0 deletions
  1. 4 0
      app/router/weight.go

+ 4 - 0
app/router/weight.go

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