mirror of
https://github.com/yusing/godoxy.git
synced 2025-06-07 12:02:34 +02:00
fix(route): add mutex lock for load balancer updates to prevent race conditions
This commit is contained in:
parent
bd0fe36c53
commit
09c244ef3c
1 changed files with 7 additions and 0 deletions
|
@ -3,6 +3,7 @@ package route
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agent"
|
"github.com/yusing/go-proxy/agent/pkg/agent"
|
||||||
"github.com/yusing/go-proxy/agent/pkg/agentproxy"
|
"github.com/yusing/go-proxy/agent/pkg/agentproxy"
|
||||||
|
@ -156,12 +157,17 @@ func (r *ReveseProxyRoute) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||||
r.handler.ServeHTTP(w, req)
|
r.handler.ServeHTTP(w, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lbLock sync.Mutex
|
||||||
|
|
||||||
func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
|
func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
|
||||||
var lb *loadbalancer.LoadBalancer
|
var lb *loadbalancer.LoadBalancer
|
||||||
cfg := r.LoadBalance
|
cfg := r.LoadBalance
|
||||||
|
lbLock.Lock()
|
||||||
|
|
||||||
l, ok := routes.HTTP.Get(cfg.Link)
|
l, ok := routes.HTTP.Get(cfg.Link)
|
||||||
var linked *ReveseProxyRoute
|
var linked *ReveseProxyRoute
|
||||||
if ok {
|
if ok {
|
||||||
|
lbLock.Unlock()
|
||||||
linked = l.(*ReveseProxyRoute)
|
linked = l.(*ReveseProxyRoute)
|
||||||
lb = linked.loadBalancer
|
lb = linked.loadBalancer
|
||||||
lb.UpdateConfigIfNeeded(cfg)
|
lb.UpdateConfigIfNeeded(cfg)
|
||||||
|
@ -186,6 +192,7 @@ func (r *ReveseProxyRoute) addToLoadBalancer(parent task.Parent) {
|
||||||
routes.HTTP.DelKey(cfg.Link)
|
routes.HTTP.DelKey(cfg.Link)
|
||||||
routes.All.DelKey(cfg.Link)
|
routes.All.DelKey(cfg.Link)
|
||||||
})
|
})
|
||||||
|
lbLock.Unlock()
|
||||||
}
|
}
|
||||||
r.loadBalancer = lb
|
r.loadBalancer = lb
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue