From 320e29ba8428a131c5b4369f9cd624c2788a4eb4 Mon Sep 17 00:00:00 2001 From: yusing Date: Thu, 2 Jan 2025 11:36:11 +0800 Subject: [PATCH] fix loadbalancer panic on weight rebalance --- internal/net/http/loadbalancer/loadbalancer.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/internal/net/http/loadbalancer/loadbalancer.go b/internal/net/http/loadbalancer/loadbalancer.go index b79f208..bea55b6 100644 --- a/internal/net/http/loadbalancer/loadbalancer.go +++ b/internal/net/http/loadbalancer/loadbalancer.go @@ -65,7 +65,6 @@ func (lb *LoadBalancer) Start(parent task.Parent) E.Error { lb.impl.OnRemoveServer(v) }) } - lb.pool.Clear() }) return nil } @@ -171,12 +170,14 @@ func (lb *LoadBalancer) rebalance() { if lb.sumWeight == maxWeight { return } - if lb.pool.Size() == 0 { + + poolSize := lb.pool.Size() + if poolSize == 0 { return } if lb.sumWeight == 0 { // distribute evenly - weightEach := maxWeight / Weight(lb.pool.Size()) - remainder := maxWeight % Weight(lb.pool.Size()) + weightEach := maxWeight / Weight(poolSize) + remainder := maxWeight % Weight(poolSize) lb.pool.RangeAll(func(_ string, s *Server) { s.Weight = weightEach lb.sumWeight += weightEach