diff options
author | Valery Piashchynski <[email protected]> | 2021-02-02 00:56:49 +0300 |
---|---|---|
committer | Valery Piashchynski <[email protected]> | 2021-02-02 00:56:49 +0300 |
commit | 6d48d31ac7fb6b7a9170f2f253e204521f244c9e (patch) | |
tree | ac64346997b307808d8a6624dcd1a5a7b2e887f1 /plugins/rpc | |
parent | 7149a8ee1c0935bb5c8d13312ba66b78b9c4d174 (diff) |
Update RPC plugin, use hashmap instead of slice to store pluggable
plugins
Fix issue with log channels
Diffstat (limited to 'plugins/rpc')
-rw-r--r-- | plugins/rpc/plugin.go | 31 |
1 files changed, 16 insertions, 15 deletions
diff --git a/plugins/rpc/plugin.go b/plugins/rpc/plugin.go index e13768f0..94fec0b6 100644 --- a/plugins/rpc/plugin.go +++ b/plugins/rpc/plugin.go @@ -15,18 +15,13 @@ import ( // PluginName contains default plugin name. const PluginName = "RPC" -type pluggable struct { - service RPCer - name string -} - // Plugin is RPC service. type Plugin struct { cfg Config log logger.Logger rpc *rpc.Server // set of the plugins, which are implement RPCer interface and can be plugged into the RR via RPC - plugins []pluggable + plugins map[string]RPCer listener net.Listener closed *uint32 } @@ -42,14 +37,23 @@ func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error { if err != nil { return errors.E(op, errors.Disabled, err) } + // Init defaults s.cfg.InitDefaults() - + // Init pluggable plugins map + s.plugins = make(map[string]RPCer) + // init logs s.log = log + // set up state state := uint32(0) s.closed = &state atomic.StoreUint32(s.closed, 0) - return s.cfg.Valid() + // validate config + err = s.cfg.Valid() + if err != nil { + return errors.E(op, err) + } + return nil } // Serve serves the service. @@ -62,14 +66,14 @@ func (s *Plugin) Serve() chan error { services := make([]string, 0, len(s.plugins)) // Attach all services - for i := 0; i < len(s.plugins); i++ { - err := s.Register(s.plugins[i].name, s.plugins[i].service.RPC()) + for name := range s.plugins { + err := s.Register(name, s.plugins[name].RPC()) if err != nil { errCh <- errors.E(op, err) return errCh } - services = append(services, s.plugins[i].name) + services = append(services, name) } var err error @@ -128,10 +132,7 @@ func (s *Plugin) Collects() []interface{} { // RegisterPlugin registers RPC service plugin. func (s *Plugin) RegisterPlugin(name endure.Named, p RPCer) { - s.plugins = append(s.plugins, pluggable{ - service: p, - name: name.Name(), - }) + s.plugins[name.Name()] = p } // Register publishes in the server the set of methods of the |