github.com/sagernet/sing@v0.4.0-beta.19.0.20240518125136-f67a0988a636/service/registry.go (about) 1 package service 2 3 import ( 4 "sync" 5 ) 6 7 type Registry interface { 8 Register(serviceType any, service any) any 9 Get(serviceType any) any 10 } 11 12 func NewRegistry() Registry { 13 return &defaultRegistry{ 14 serviceTypes: make(map[any]any), 15 } 16 } 17 18 type defaultRegistry struct { 19 serviceTypes map[any]any 20 access sync.RWMutex 21 } 22 23 func (r *defaultRegistry) Register(serviceType any, service any) any { 24 r.access.Lock() 25 defer r.access.Unlock() 26 oldService := r.serviceTypes[serviceType] 27 r.serviceTypes[serviceType] = service 28 return oldService 29 } 30 31 func (r *defaultRegistry) Get(serviceType any) any { 32 r.access.RLock() 33 defer r.access.RUnlock() 34 return r.serviceTypes[serviceType] 35 }