Routing: Add mutex for `Attributes` temporarily (#3908)

https://github.com/XTLS/Xray-core/pull/3908#issuecomment-2412859858
This commit is contained in:
风扇滑翔翼 2024-10-15 12:22:32 +08:00 committed by GitHub
parent 82bd5f3046
commit 86257531ee
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -4,6 +4,7 @@ package session // import "github.com/xtls/xray-core/common/session"
import ( import (
"context" "context"
"math/rand" "math/rand"
"sync"
c "github.com/xtls/xray-core/common/ctx" c "github.com/xtls/xray-core/common/ctx"
"github.com/xtls/xray-core/common/errors" "github.com/xtls/xray-core/common/errors"
@ -91,6 +92,10 @@ type Content struct {
Attributes map[string]string Attributes map[string]string
SkipDNSResolve bool SkipDNSResolve bool
mu sync.Mutex
isLocked bool
} }
// Sockopt is the settings for socket connection. // Sockopt is the settings for socket connection.
@ -99,8 +104,22 @@ type Sockopt struct {
Mark int32 Mark int32
} }
// Some how when using mux, there will be a same ctx between different requests
// This will cause problem as it's designed for single request, like concurrent map writes
// Add a Mutex as a temp solution
// SetAttribute attaches additional string attributes to content. // SetAttribute attaches additional string attributes to content.
func (c *Content) SetAttribute(name string, value string) { func (c *Content) SetAttribute(name string, value string) {
if c.isLocked {
errors.LogError(context.Background(), "Multiple goroutines are tring to access one routing content, tring to write ", name, ":", value)
}
c.mu.Lock()
c.isLocked = true
defer func() {
c.isLocked = false
c.mu.Unlock()
}()
if c.Attributes == nil { if c.Attributes == nil {
c.Attributes = make(map[string]string) c.Attributes = make(map[string]string)
} }