trpc.group/trpc-go/trpc-go@v1.0.3/naming/registry/registry.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 // Package registry registers servers. A server report itself on start. 15 package registry 16 17 import ( 18 "errors" 19 "sync" 20 ) 21 22 // ErrNotImplement is the not implemented error. 23 var ErrNotImplement = errors.New("not implement") 24 25 // DefaultRegistry is the default registry. 26 var DefaultRegistry Registry = &NoopRegistry{} 27 28 // SetDefaultRegistry sets the default registry. 29 func SetDefaultRegistry(r Registry) { 30 DefaultRegistry = r 31 } 32 33 // Registry is the interface that defines a register. 34 type Registry interface { 35 Register(service string, opt ...Option) error 36 Deregister(service string) error 37 } 38 39 var ( 40 registries = make(map[string]Registry) 41 lock = sync.RWMutex{} 42 ) 43 44 // Register registers a named registry. Each service has its own registry. 45 func Register(name string, s Registry) { 46 lock.Lock() 47 registries[name] = s 48 lock.Unlock() 49 } 50 51 // Get gets a named registry. 52 func Get(name string) Registry { 53 lock.RLock() 54 r := registries[name] 55 lock.RUnlock() 56 return r 57 } 58 59 // NoopRegistry is the noop registry. 60 type NoopRegistry struct{} 61 62 // Register always returns ErrNotImplement. 63 func (noop *NoopRegistry) Register(service string, opt ...Option) error { 64 return ErrNotImplement 65 } 66 67 // Deregister always return ErrNotImplement. 68 func (noop *NoopRegistry) Deregister(service string) error { 69 return ErrNotImplement 70 }