0
mirror of https://github.com/XTLS/Xray-core.git synced 2025-06-17 21:59:47 +03:00
This commit is contained in:
风扇滑翔翼
2025-06-13 09:19:35 +00:00
committed by GitHub
parent 9436956e2a
commit 4bc9f8ed37
2 changed files with 25 additions and 7 deletions

View File

@ -1,4 +1,4 @@
package util package utils
import ( import (
"sync" "sync"
@ -6,6 +6,7 @@ import (
// TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values. // TypedSyncMap is a wrapper of sync.Map that provides type-safe for keys and values.
// No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa // No need to use type assertions every time, so you can have more time to enjoy other things like GochiUsa
// If sync.Map returned nil, it will return the zero value of the type V.
type TypedSyncMap[K, V any] struct { type TypedSyncMap[K, V any] struct {
syncMap *sync.Map syncMap *sync.Map
} }
@ -34,26 +35,43 @@ func (m *TypedSyncMap[K, V]) Delete(key K) {
func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) { func (m *TypedSyncMap[K, V]) Load(key K) (value V, ok bool) {
anyValue, ok := m.syncMap.Load(key) anyValue, ok := m.syncMap.Load(key)
return anyValue.(V), ok // anyValue might be nil
if anyValue != nil {
value = anyValue.(V)
}
return value, ok
} }
func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) { func (m *TypedSyncMap[K, V]) LoadAndDelete(key K) (value V, loaded bool) {
anyValue, loaded := m.syncMap.LoadAndDelete(key) anyValue, loaded := m.syncMap.LoadAndDelete(key)
return anyValue.(V), loaded if anyValue != nil {
value = anyValue.(V)
}
return value, loaded
} }
func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) { func (m *TypedSyncMap[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool) {
anyActual, loaded := m.syncMap.LoadOrStore(key, value) anyActual, loaded := m.syncMap.LoadOrStore(key, value)
return anyActual.(V), loaded if anyActual != nil {
actual = anyActual.(V)
}
return actual, loaded
} }
func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) { func (m *TypedSyncMap[K, V]) Range(f func(key K, value V) bool) {
m.syncMap.Range(func(key, value any) bool { m.syncMap.Range(func(key, value any) bool {
return f(key.(K), value.(V)) return f(key.(K), value.(V))
}) })
} }
func (m *TypedSyncMap[K, V]) Store(key K, value V) { func (m *TypedSyncMap[K, V]) Store(key K, value V) {
m.syncMap.Store(key, value) m.syncMap.Store(key, value)
} }
func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) { func (m *TypedSyncMap[K, V]) Swap(key K, value V) (previous V, loaded bool) {
anyPrevious, loaded := m.syncMap.Swap(key, value) anyPrevious, loaded := m.syncMap.Swap(key, value)
return anyPrevious.(V), loaded if anyPrevious != nil {
previous = anyPrevious.(V)
}
return previous, loaded
} }

View File

@ -344,7 +344,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: util.NewTypedSyncMap[string, net.Address](), resolvedUDPAddr: utils.NewTypedSyncMap[string, net.Address](),
} }
} }
@ -362,7 +362,7 @@ type PacketWriter struct {
// But resolver will return a random one if the domain has many IPs // But resolver will return a random one if the domain has many IPs
// Resulting in these packets being sent to many different IPs randomly // Resulting in these packets being sent to many different IPs randomly
// So, cache and keep the resolve result // So, cache and keep the resolve result
resolvedUDPAddr *util.TypedSyncMap[string, net.Address] resolvedUDPAddr *utils.TypedSyncMap[string, net.Address]
} }
func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error { func (w *PacketWriter) WriteMultiBuffer(mb buf.MultiBuffer) error {