Optimize with double-checked locking for better concurrency

Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-01-09 10:35:22 +00:00
parent 385867e82b
commit 52f7f3d174
+17 -9
View File
@@ -83,18 +83,26 @@ func (*Server) Network() []net.Network {
// Process implements proxy.Inbound. // Process implements proxy.Inbound.
func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error { func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Connection, dispatcher routing.Dispatcher) error {
// Use RWMutex to safely handle concurrent access to routing info // Use double-checked locking to safely handle concurrent access to routing info
// Only update if not set or if dispatcher is different // Only update if not set or if dispatcher is different
s.infoMutex.Lock() // First check without write lock for better concurrency
if s.info.dispatcher == nil || s.info.dispatcher != dispatcher { s.infoMutex.RLock()
s.info = routingInfo{ needsUpdate := s.info.dispatcher == nil || s.info.dispatcher != dispatcher
ctx: ctx, s.infoMutex.RUnlock()
dispatcher: dispatcher,
inboundTag: session.InboundFromContext(ctx), if needsUpdate {
contentTag: session.ContentFromContext(ctx), s.infoMutex.Lock()
// Double-check after acquiring write lock
if s.info.dispatcher == nil || s.info.dispatcher != dispatcher {
s.info = routingInfo{
ctx: ctx,
dispatcher: dispatcher,
inboundTag: session.InboundFromContext(ctx),
contentTag: session.ContentFromContext(ctx),
}
} }
s.infoMutex.Unlock()
} }
s.infoMutex.Unlock()
ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String()) ep, err := s.bindServer.ParseEndpoint(conn.RemoteAddr().String())
if err != nil { if err != nil {