mirror of
https://github.com/XTLS/Xray-core.git
synced 2026-07-08 12:48:58 +00:00
Optimize with double-checked locking for better concurrency
Co-authored-by: RPRX <63339210+RPRX@users.noreply.github.com>
This commit is contained in:
@@ -83,9 +83,16 @@ 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
|
||||||
|
// First check without write lock for better concurrency
|
||||||
|
s.infoMutex.RLock()
|
||||||
|
needsUpdate := s.info.dispatcher == nil || s.info.dispatcher != dispatcher
|
||||||
|
s.infoMutex.RUnlock()
|
||||||
|
|
||||||
|
if needsUpdate {
|
||||||
s.infoMutex.Lock()
|
s.infoMutex.Lock()
|
||||||
|
// Double-check after acquiring write lock
|
||||||
if s.info.dispatcher == nil || s.info.dispatcher != dispatcher {
|
if s.info.dispatcher == nil || s.info.dispatcher != dispatcher {
|
||||||
s.info = routingInfo{
|
s.info = routingInfo{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
@@ -95,6 +102,7 @@ func (s *Server) Process(ctx context.Context, network net.Network, conn stat.Con
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 {
|
||||||
|
|||||||
Reference in New Issue
Block a user