From c9fbeb37d8887b29bf67c7d00f5cd9880478f0e8 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 8 Dec 2017 16:16:54 +0900 Subject: [PATCH] ing --- registry/rpc_registry.go | 5 +++++ registry/service_map.go | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/registry/rpc_registry.go b/registry/rpc_registry.go index 29a9b6b..bd4bc2e 100644 --- a/registry/rpc_registry.go +++ b/registry/rpc_registry.go @@ -15,6 +15,7 @@ func NewRPCRegistry() RPCRegistry { type RPCRegistry interface { RPCInvoker + GetService(name string) interface{} RegisterService(receiver interface{}, name string) error } @@ -43,6 +44,10 @@ func (rr *rpcRegistry) RegisterService(receiver interface{}, name string) error return rr.services.register(receiver, name) } +func (rr *rpcRegistry) GetService(name string) interface{} { + return rr.services.getService(name) +} + // HasMethod returns true if the given method is registered. // // The method uses a dotted notation as in "Service.Method". diff --git a/registry/service_map.go b/registry/service_map.go index 9eb1803..10635d1 100644 --- a/registry/service_map.go +++ b/registry/service_map.go @@ -77,10 +77,19 @@ func getValue(t reflect.Type) reflect.Value { // serviceMap is a registry for services. type serviceMap struct { - mutex sync.Mutex + mutex sync.RWMutex services map[string]*service } +func (sm *serviceMap) getService(name string) interface{} { + sm.mutex.RLock() + defer sm.mutex.RUnlock() + if sm.services == nil { + return nil + } + return sm.services[name] +} + // register adds a new service using reflection to extract its methods. func (sm *serviceMap) register(rcvr interface{}, name string) error { // Setup service.