From 87155bfc7193efd348db05df7c31e6ae71d890fe Mon Sep 17 00:00:00 2001 From: Rinka Date: Thu, 31 Aug 2023 18:01:06 +0800 Subject: [PATCH] fix(router): panic caused by concurrent map read and write (#2678) --- app/router/weight.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/router/weight.go b/app/router/weight.go index ec7711b99..dc6e90b52 100644 --- a/app/router/weight.go +++ b/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