blob: fd4ed15f3502a5fb70d91de839186143bf479d47 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
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) Provide(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() bool {
return true
}
|