github.com/xmplusdev/xmcore@v1.8.11-0.20240412132628-5518b55526af/app/router/balancing_override.go (about) 1 package router 2 3 import ( 4 sync "sync" 5 ) 6 7 func (r *Router) OverrideBalancer(balancer string, target string) error { 8 var b *Balancer 9 for tag, bl := range r.balancers { 10 if tag == balancer { 11 b = bl 12 break 13 } 14 } 15 if b == nil { 16 return newError("balancer '", balancer, "' not found") 17 } 18 b.override.Put(target) 19 return nil 20 } 21 22 type overrideSettings struct { 23 target string 24 } 25 26 type override struct { 27 access sync.RWMutex 28 settings overrideSettings 29 } 30 31 // Get gets the override settings 32 func (o *override) Get() string { 33 o.access.RLock() 34 defer o.access.RUnlock() 35 return o.settings.target 36 } 37 38 // Put updates the override settings 39 func (o *override) Put(target string) { 40 o.access.Lock() 41 defer o.access.Unlock() 42 o.settings.target = target 43 } 44 45 // Clear clears the override settings 46 func (o *override) Clear() { 47 o.access.Lock() 48 defer o.access.Unlock() 49 o.settings.target = "" 50 }