This commit is contained in:
crusader 2017-12-08 16:16:54 +09:00
parent f22a5d5df4
commit c9fbeb37d8
2 changed files with 15 additions and 1 deletions

View File

@ -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".

View File

@ -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.