mirror of
https://github.com/XTLS/Xray-core.git
synced 2025-07-28 21:04:14 +00:00
Change to TypedSyncMap
This commit is contained in:
parent
b6b51c51c8
commit
20825f6f1a
6 changed files with 29 additions and 31 deletions
|
@ -15,8 +15,8 @@ type TypedSyncMap[K, V any] struct {
|
||||||
// K is key type, V is value type
|
// K is key type, V is value type
|
||||||
// It is recommended to use pointer types for V because sync.Map might return nil
|
// It is recommended to use pointer types for V because sync.Map might return nil
|
||||||
// If sync.Map methods really returned nil, it will return the zero value of the type V
|
// If sync.Map methods really returned nil, it will return the zero value of the type V
|
||||||
func NewTypedSyncMap[K any, V any]() *TypedSyncMap[K, V] {
|
func NewTypedSyncMap[K any, V any]() TypedSyncMap[K, V] {
|
||||||
return &TypedSyncMap[K, V]{
|
return TypedSyncMap[K, V]{
|
||||||
syncMap: &sync.Map{},
|
syncMap: &sync.Map{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -363,7 +363,7 @@ func NewPacketWriter(conn net.Conn, h *Handler, ctx context.Context, UDPOverride
|
||||||
Handler: h,
|
Handler: h,
|
||||||
Context: ctx,
|
Context: ctx,
|
||||||
UDPOverride: UDPOverride,
|
UDPOverride: UDPOverride,
|
||||||
resolvedUDPAddr: resolvedUDPAddr,
|
resolvedUDPAddr: &resolvedUDPAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,17 +2,17 @@ package trojan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
"github.com/xtls/xray-core/common/protocol"
|
"github.com/xtls/xray-core/common/protocol"
|
||||||
|
"github.com/xtls/xray-core/common/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Validator stores valid trojan users.
|
// Validator stores valid trojan users.
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
||||||
email sync.Map
|
email utils.TypedSyncMap[string, *protocol.MemoryUser]
|
||||||
users sync.Map
|
users utils.TypedSyncMap[string, *protocol.MemoryUser]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a trojan user, Email must be empty or unique.
|
// Add a trojan user, Email must be empty or unique.
|
||||||
|
@ -38,7 +38,7 @@ func (v *Validator) Del(e string) error {
|
||||||
return errors.New("User ", e, " not found.")
|
return errors.New("User ", e, " not found.")
|
||||||
}
|
}
|
||||||
v.email.Delete(le)
|
v.email.Delete(le)
|
||||||
v.users.Delete(hexString(u.(*protocol.MemoryUser).Account.(*MemoryAccount).Key))
|
v.users.Delete(hexString(u.Account.(*MemoryAccount).Key))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,7 +46,7 @@ func (v *Validator) Del(e string) error {
|
||||||
func (v *Validator) Get(hash string) *protocol.MemoryUser {
|
func (v *Validator) Get(hash string) *protocol.MemoryUser {
|
||||||
u, _ := v.users.Load(hash)
|
u, _ := v.users.Load(hash)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
return u.(*protocol.MemoryUser)
|
return u
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -56,7 +56,7 @@ func (v *Validator) GetByEmail(email string) *protocol.MemoryUser {
|
||||||
email = strings.ToLower(email)
|
email = strings.ToLower(email)
|
||||||
u, _ := v.email.Load(email)
|
u, _ := v.email.Load(email)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
return u.(*protocol.MemoryUser)
|
return u
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,8 @@ func (v *Validator) GetByEmail(email string) *protocol.MemoryUser {
|
||||||
// Get all users
|
// Get all users
|
||||||
func (v *Validator) GetAll() []*protocol.MemoryUser {
|
func (v *Validator) GetAll() []*protocol.MemoryUser {
|
||||||
var u = make([]*protocol.MemoryUser, 0, 100)
|
var u = make([]*protocol.MemoryUser, 0, 100)
|
||||||
v.email.Range(func(key, value interface{}) bool {
|
v.email.Range(func(key string, value *protocol.MemoryUser) bool {
|
||||||
u = append(u, value.(*protocol.MemoryUser))
|
u = append(u, value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return u
|
return u
|
||||||
|
@ -74,7 +74,7 @@ func (v *Validator) GetAll() []*protocol.MemoryUser {
|
||||||
// Get users count
|
// Get users count
|
||||||
func (v *Validator) GetCount() int64 {
|
func (v *Validator) GetCount() int64 {
|
||||||
var c int64 = 0
|
var c int64 = 0
|
||||||
v.email.Range(func(key, value interface{}) bool {
|
v.email.Range(func(key string, value *protocol.MemoryUser) bool {
|
||||||
c++
|
c++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
|
@ -2,10 +2,10 @@ package vless
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/xtls/xray-core/common/errors"
|
"github.com/xtls/xray-core/common/errors"
|
||||||
"github.com/xtls/xray-core/common/protocol"
|
"github.com/xtls/xray-core/common/protocol"
|
||||||
|
"github.com/xtls/xray-core/common/utils"
|
||||||
"github.com/xtls/xray-core/common/uuid"
|
"github.com/xtls/xray-core/common/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -21,8 +21,8 @@ type Validator interface {
|
||||||
// MemoryValidator stores valid VLESS users.
|
// MemoryValidator stores valid VLESS users.
|
||||||
type MemoryValidator struct {
|
type MemoryValidator struct {
|
||||||
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
// Considering email's usage here, map + sync.Mutex/RWMutex may have better performance.
|
||||||
email sync.Map
|
email utils.TypedSyncMap[string, *protocol.MemoryUser]
|
||||||
users sync.Map
|
users utils.TypedSyncMap[uuid.UUID, *protocol.MemoryUser]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add a VLESS user, Email must be empty or unique.
|
// Add a VLESS user, Email must be empty or unique.
|
||||||
|
@ -48,7 +48,7 @@ func (v *MemoryValidator) Del(e string) error {
|
||||||
return errors.New("User ", e, " not found.")
|
return errors.New("User ", e, " not found.")
|
||||||
}
|
}
|
||||||
v.email.Delete(le)
|
v.email.Delete(le)
|
||||||
v.users.Delete(u.(*protocol.MemoryUser).Account.(*MemoryAccount).ID.UUID())
|
v.users.Delete(u.Account.(*MemoryAccount).ID.UUID())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +56,7 @@ func (v *MemoryValidator) Del(e string) error {
|
||||||
func (v *MemoryValidator) Get(id uuid.UUID) *protocol.MemoryUser {
|
func (v *MemoryValidator) Get(id uuid.UUID) *protocol.MemoryUser {
|
||||||
u, _ := v.users.Load(id)
|
u, _ := v.users.Load(id)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
return u.(*protocol.MemoryUser)
|
return u
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -66,7 +66,7 @@ func (v *MemoryValidator) GetByEmail(email string) *protocol.MemoryUser {
|
||||||
email = strings.ToLower(email)
|
email = strings.ToLower(email)
|
||||||
u, _ := v.email.Load(email)
|
u, _ := v.email.Load(email)
|
||||||
if u != nil {
|
if u != nil {
|
||||||
return u.(*protocol.MemoryUser)
|
return u
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -74,8 +74,8 @@ func (v *MemoryValidator) GetByEmail(email string) *protocol.MemoryUser {
|
||||||
// Get all users
|
// Get all users
|
||||||
func (v *MemoryValidator) GetAll() []*protocol.MemoryUser {
|
func (v *MemoryValidator) GetAll() []*protocol.MemoryUser {
|
||||||
var u = make([]*protocol.MemoryUser, 0, 100)
|
var u = make([]*protocol.MemoryUser, 0, 100)
|
||||||
v.email.Range(func(key, value interface{}) bool {
|
v.email.Range(func(key string, value *protocol.MemoryUser) bool {
|
||||||
u = append(u, value.(*protocol.MemoryUser))
|
u = append(u, value)
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
return u
|
return u
|
||||||
|
@ -84,7 +84,7 @@ func (v *MemoryValidator) GetAll() []*protocol.MemoryUser {
|
||||||
// Get users count
|
// Get users count
|
||||||
func (v *MemoryValidator) GetCount() int64 {
|
func (v *MemoryValidator) GetCount() int64 {
|
||||||
var c int64 = 0
|
var c int64 = 0
|
||||||
v.email.Range(func(key, value interface{}) bool {
|
v.email.Range(func(key string, value *protocol.MemoryUser) bool {
|
||||||
c++
|
c++
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
|
@ -43,7 +43,7 @@ type dialerConf struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
globalDialerMap map[dialerConf]*grpc.ClientConn
|
globalDialerMap = make(map[dialerConf]*grpc.ClientConn)
|
||||||
globalDialerAccess sync.Mutex
|
globalDialerAccess sync.Mutex
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -77,9 +77,6 @@ func getGrpcClient(ctx context.Context, dest net.Destination, streamSettings *in
|
||||||
globalDialerAccess.Lock()
|
globalDialerAccess.Lock()
|
||||||
defer globalDialerAccess.Unlock()
|
defer globalDialerAccess.Unlock()
|
||||||
|
|
||||||
if globalDialerMap == nil {
|
|
||||||
globalDialerMap = make(map[dialerConf]*grpc.ClientConn)
|
|
||||||
}
|
|
||||||
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
tlsConfig := tls.ConfigFromStreamSettings(streamSettings)
|
||||||
realityConfig := reality.ConfigFromStreamSettings(streamSettings)
|
realityConfig := reality.ConfigFromStreamSettings(streamSettings)
|
||||||
sockopt := streamSettings.SocketSettings
|
sockopt := streamSettings.SocketSettings
|
||||||
|
|
|
@ -20,6 +20,7 @@ import (
|
||||||
"github.com/xtls/xray-core/common/net"
|
"github.com/xtls/xray-core/common/net"
|
||||||
http_proto "github.com/xtls/xray-core/common/protocol/http"
|
http_proto "github.com/xtls/xray-core/common/protocol/http"
|
||||||
"github.com/xtls/xray-core/common/signal/done"
|
"github.com/xtls/xray-core/common/signal/done"
|
||||||
|
"github.com/xtls/xray-core/common/utils"
|
||||||
"github.com/xtls/xray-core/transport/internet"
|
"github.com/xtls/xray-core/transport/internet"
|
||||||
"github.com/xtls/xray-core/transport/internet/reality"
|
"github.com/xtls/xray-core/transport/internet/reality"
|
||||||
"github.com/xtls/xray-core/transport/internet/stat"
|
"github.com/xtls/xray-core/transport/internet/stat"
|
||||||
|
@ -32,7 +33,7 @@ type requestHandler struct {
|
||||||
path string
|
path string
|
||||||
ln *Listener
|
ln *Listener
|
||||||
sessionMu *sync.Mutex
|
sessionMu *sync.Mutex
|
||||||
sessions sync.Map
|
sessions utils.TypedSyncMap[string, *httpSession]
|
||||||
localAddr net.Addr
|
localAddr net.Addr
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,18 +48,18 @@ type httpSession struct {
|
||||||
|
|
||||||
func (h *requestHandler) upsertSession(sessionId string) *httpSession {
|
func (h *requestHandler) upsertSession(sessionId string) *httpSession {
|
||||||
// fast path
|
// fast path
|
||||||
currentSessionAny, ok := h.sessions.Load(sessionId)
|
currentSession, ok := h.sessions.Load(sessionId)
|
||||||
if ok {
|
if ok {
|
||||||
return currentSessionAny.(*httpSession)
|
return currentSession
|
||||||
}
|
}
|
||||||
|
|
||||||
// slow path
|
// slow path
|
||||||
h.sessionMu.Lock()
|
h.sessionMu.Lock()
|
||||||
defer h.sessionMu.Unlock()
|
defer h.sessionMu.Unlock()
|
||||||
|
|
||||||
currentSessionAny, ok = h.sessions.Load(sessionId)
|
currentSession, ok = h.sessions.Load(sessionId)
|
||||||
if ok {
|
if ok {
|
||||||
return currentSessionAny.(*httpSession)
|
return currentSession
|
||||||
}
|
}
|
||||||
|
|
||||||
s := &httpSession{
|
s := &httpSession{
|
||||||
|
@ -361,7 +362,7 @@ func ListenXH(ctx context.Context, address net.Address, port net.Port, streamSet
|
||||||
path: l.config.GetNormalizedPath(),
|
path: l.config.GetNormalizedPath(),
|
||||||
ln: l,
|
ln: l,
|
||||||
sessionMu: &sync.Mutex{},
|
sessionMu: &sync.Mutex{},
|
||||||
sessions: sync.Map{},
|
sessions: utils.NewTypedSyncMap[string, *httpSession](),
|
||||||
}
|
}
|
||||||
tlsConfig := getTLSConfig(streamSettings)
|
tlsConfig := getTLSConfig(streamSettings)
|
||||||
l.isH3 = len(tlsConfig.NextProtos) == 1 && tlsConfig.NextProtos[0] == "h3"
|
l.isH3 = len(tlsConfig.NextProtos) == 1 && tlsConfig.NextProtos[0] == "h3"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue