Add option to force drain rtcService/agentService connections. (#4618)

When force: true, drain as fast as possible.
This commit is contained in:
Raja Subramanian
2026-06-23 16:10:50 +05:30
committed by GitHub
parent 6658dd5454
commit 0cf53e2f0d
3 changed files with 37 additions and 20 deletions
+2 -2
View File
@@ -29,7 +29,7 @@ import (
type AgentService interface { type AgentService interface {
HandleConnection(context.Context, agent.SignalConn, agent.WorkerRegistration) HandleConnection(context.Context, agent.SignalConn, agent.WorkerRegistration)
DrainConnections(time.Duration) DrainConnections(time.Duration, bool)
} }
type TestServer struct { type TestServer struct {
@@ -140,7 +140,7 @@ func (h *TestServer) SimulateAgentWorker(opts ...SimulatedWorkerOption) *AgentWo
} }
func (h *TestServer) Close() { func (h *TestServer) Close() {
h.DrainConnections(1) h.DrainConnections(1, false)
} }
var _ agent.SignalConn = (*AgentWorker)(nil) var _ agent.SignalConn = (*AgentWorker)(nil)
+20 -10
View File
@@ -485,19 +485,29 @@ func (h *AgentHandler) CheckEnabled(ctx context.Context, req *rpc.CheckEnabledRe
}, nil }, nil
} }
func (h *AgentHandler) DrainConnections(interval time.Duration) { func (h *AgentHandler) DrainConnections(interval time.Duration, force bool) {
// jitter drain start if !force {
time.Sleep(time.Duration(rand.Int63n(int64(interval)))) // jitter drain start
time.Sleep(time.Duration(rand.Int63n(int64(interval))))
t := time.NewTicker(interval) t := time.NewTicker(interval)
defer t.Stop() defer t.Stop()
h.mu.Lock() h.mu.Lock()
defer h.mu.Unlock() defer h.mu.Unlock()
for _, w := range h.workers { for _, w := range h.workers {
w.Close() w.Close()
<-t.C <-t.C
}
} else {
// drain as quickly as possible when forced
h.mu.Lock()
defer h.mu.Unlock()
for _, w := range h.workers {
w.Close()
}
} }
} }
+15 -8
View File
@@ -628,20 +628,27 @@ func (s *RTCService) serve(w http.ResponseWriter, r *http.Request, needsJoinRequ
} }
} }
func (s *RTCService) DrainConnections(interval time.Duration) { func (s *RTCService) DrainConnections(interval time.Duration, force bool) {
s.mu.Lock() s.mu.Lock()
conns := maps.Clone(s.connections) conns := maps.Clone(s.connections)
s.mu.Unlock() s.mu.Unlock()
// jitter drain start if !force {
time.Sleep(time.Duration(rand.Int63n(int64(interval)))) // jitter drain start
time.Sleep(time.Duration(rand.Int63n(int64(interval))))
t := time.NewTicker(interval) t := time.NewTicker(interval)
defer t.Stop() defer t.Stop()
for c := range conns { for c := range conns {
_ = c.Close() _ = c.Close()
<-t.C <-t.C
}
} else {
// drain as quickly as possible when forced
for c := range conns {
_ = c.Close()
}
} }
} }