diff options
author | Valery Piashchynski <[email protected]> | 2021-06-14 18:18:58 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2021-06-14 18:18:58 +0300 |
commit | e30bc57f13023c15775925b7cc3b3fcbb9f2b55c (patch) | |
tree | d116452cf8d7a53d4dd3b431bdb76ee76f134ea1 | |
parent | aeaf67900cbd9dff5bd7318eb4d79e302331e457 (diff) | |
parent | 63687937d0189359c48288e16afca8f56c3cda93 (diff) |
#725 feat(plugin): make `in-memory` plugin general for the KV and WS
#725 feat(plugin): make `in-memory` plugin general for the KV and WS
-rw-r--r-- | plugins/kv/drivers/memory/plugin.go | 68 | ||||
-rw-r--r-- | plugins/memory/config.go (renamed from plugins/kv/drivers/memory/config.go) | 0 | ||||
-rw-r--r-- | plugins/memory/kv.go (renamed from plugins/kv/drivers/memory/driver.go) | 0 | ||||
-rw-r--r-- | plugins/memory/plugin.go | 42 | ||||
-rw-r--r-- | tests/plugins/kv/storage_plugin_test.go | 2 |
5 files changed, 41 insertions, 71 deletions
diff --git a/plugins/kv/drivers/memory/plugin.go b/plugins/kv/drivers/memory/plugin.go deleted file mode 100644 index da81017e..00000000 --- a/plugins/kv/drivers/memory/plugin.go +++ /dev/null @@ -1,68 +0,0 @@ -package memory - -import ( - "github.com/spiral/errors" - "github.com/spiral/roadrunner/v2/plugins/config" - "github.com/spiral/roadrunner/v2/plugins/kv" - "github.com/spiral/roadrunner/v2/plugins/logger" -) - -// PluginName is user friendly name for the plugin -const PluginName = "memory" - -type Plugin struct { - // heap is user map for the key-value pairs - stop chan struct{} - - log logger.Logger - cfgPlugin config.Configurer - drivers uint -} - -func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error { - const op = errors.Op("in_memory_plugin_init") - if !cfg.Has(kv.PluginName) { - return errors.E(op, errors.Disabled) - } - - s.log = log - s.cfgPlugin = cfg - s.stop = make(chan struct{}, 1) - return nil -} - -func (s *Plugin) Serve() chan error { - return make(chan error, 1) -} - -func (s *Plugin) Stop() error { - if s.drivers > 0 { - for i := uint(0); i < s.drivers; i++ { - // send close signal to every driver - s.stop <- struct{}{} - } - } - return nil -} - -func (s *Plugin) KVProvide(key string) (kv.Storage, error) { - const op = errors.Op("inmemory_plugin_provide") - st, err := NewInMemoryDriver(s.log, key, s.cfgPlugin, s.stop) - if err != nil { - return nil, errors.E(op, err) - } - - // save driver number to release resources after Stop - s.drivers++ - - return st, nil -} - -// Name returns plugin user-friendly name -func (s *Plugin) Name() string { - return PluginName -} - -// Available interface implementation -func (s *Plugin) Available() { -} diff --git a/plugins/kv/drivers/memory/config.go b/plugins/memory/config.go index e51d09c5..e51d09c5 100644 --- a/plugins/kv/drivers/memory/config.go +++ b/plugins/memory/config.go diff --git a/plugins/kv/drivers/memory/driver.go b/plugins/memory/kv.go index 9b7d7259..9b7d7259 100644 --- a/plugins/kv/drivers/memory/driver.go +++ b/plugins/memory/kv.go diff --git a/plugins/memory/plugin.go b/plugins/memory/plugin.go index 650f0b4b..6151ebf0 100644 --- a/plugins/memory/plugin.go +++ b/plugins/memory/plugin.go @@ -1,18 +1,43 @@ package memory import ( + "github.com/spiral/errors" "github.com/spiral/roadrunner/v2/pkg/pubsub" + "github.com/spiral/roadrunner/v2/plugins/config" + "github.com/spiral/roadrunner/v2/plugins/kv" "github.com/spiral/roadrunner/v2/plugins/logger" ) const PluginName string = "memory" type Plugin struct { - log logger.Logger + // heap is user map for the key-value pairs + stop chan struct{} + + log logger.Logger + cfgPlugin config.Configurer + drivers uint } -func (p *Plugin) Init(log logger.Logger) error { +func (p *Plugin) Init(log logger.Logger, cfg config.Configurer) error { + p.log = log p.log = log + p.cfgPlugin = cfg + p.stop = make(chan struct{}, 1) + return nil +} + +func (p *Plugin) Serve() chan error { + return make(chan error, 1) +} + +func (p *Plugin) Stop() error { + if p.drivers > 0 { + for i := uint(0); i < p.drivers; i++ { + // send close signal to every driver + p.stop <- struct{}{} + } + } return nil } @@ -20,6 +45,19 @@ func (p *Plugin) PSProvide(key string) (pubsub.PubSub, error) { return NewPubSubDriver(p.log, key) } +func (p *Plugin) KVProvide(key string) (kv.Storage, error) { + const op = errors.Op("inmemory_plugin_provide") + st, err := NewInMemoryDriver(p.log, key, p.cfgPlugin, p.stop) + if err != nil { + return nil, errors.E(op, err) + } + + // save driver number to release resources after Stop + p.drivers++ + + return st, nil +} + func (p *Plugin) Name() string { return PluginName } diff --git a/tests/plugins/kv/storage_plugin_test.go b/tests/plugins/kv/storage_plugin_test.go index 0ded9fbd..24b66ae1 100644 --- a/tests/plugins/kv/storage_plugin_test.go +++ b/tests/plugins/kv/storage_plugin_test.go @@ -17,8 +17,8 @@ import ( "github.com/spiral/roadrunner/v2/plugins/kv" "github.com/spiral/roadrunner/v2/plugins/kv/drivers/boltdb" "github.com/spiral/roadrunner/v2/plugins/kv/drivers/memcached" - "github.com/spiral/roadrunner/v2/plugins/kv/drivers/memory" "github.com/spiral/roadrunner/v2/plugins/logger" + "github.com/spiral/roadrunner/v2/plugins/memory" "github.com/spiral/roadrunner/v2/plugins/redis" rpcPlugin "github.com/spiral/roadrunner/v2/plugins/rpc" "github.com/stretchr/testify/assert" |