fix loadbalancing when two container have the same alias

This commit is contained in:
yusing
2025-02-17 11:16:34 +08:00
parent a8a209f0b0
commit 75d272be14
4 changed files with 14 additions and 11 deletions

View File

@@ -125,12 +125,12 @@ func (lb *LoadBalancer) AddServer(srv Server) {
lb.poolMu.Lock()
defer lb.poolMu.Unlock()
if lb.pool.Has(srv.Name()) {
old, _ := lb.pool.Load(srv.Name())
if lb.pool.Has(srv.Key()) {
old, _ := lb.pool.Load(srv.Key())
lb.sumWeight -= old.Weight()
lb.impl.OnRemoveServer(old)
}
lb.pool.Store(srv.Name(), srv)
lb.pool.Store(srv.Key(), srv)
lb.sumWeight += srv.Weight()
lb.rebalance()
@@ -146,11 +146,11 @@ func (lb *LoadBalancer) RemoveServer(srv Server) {
lb.poolMu.Lock()
defer lb.poolMu.Unlock()
if !lb.pool.Has(srv.Name()) {
if !lb.pool.Has(srv.Key()) {
return
}
lb.pool.Delete(srv.Name())
lb.pool.Delete(srv.Key())
lb.sumWeight -= srv.Weight()
lb.rebalance()
@@ -244,7 +244,7 @@ func (lb *LoadBalancer) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
func (lb *LoadBalancer) MarshalJSON() ([]byte, error) {
extra := make(map[string]any)
lb.pool.RangeAll(func(k string, v Server) {
extra[v.Name()] = v
extra[v.Key()] = v
})
return (&monitor.JSONRepresentation{