summaryrefslogtreecommitdiff
path: root/plugins/redis/plugin.go
blob: 961182a93958b1fdc626c45be190d62da9eb1f2a (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
70
71
72
73
74
75
76
77
package redis

import (
	"sync"

	"github.com/go-redis/redis/v8"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/common/kv"
	"github.com/spiral/roadrunner/v2/common/pubsub"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	redis_kv "github.com/spiral/roadrunner/v2/plugins/redis/kv"
	redis_pubsub "github.com/spiral/roadrunner/v2/plugins/redis/pubsub"
)

const PluginName = "redis"

type Plugin struct {
	sync.RWMutex
	// config for RR integration
	cfgPlugin config.Configurer
	// logger
	log logger.Logger
	// redis universal client
	universalClient redis.UniversalClient

	// fanIn implementation used to deliver messages from all channels to the single websocket point
	stopCh chan struct{}
}

func (p *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
	p.log = log
	p.cfgPlugin = cfg
	p.stopCh = make(chan struct{}, 1)

	return nil
}

func (p *Plugin) Serve() chan error {
	return make(chan error)
}

func (p *Plugin) Stop() error {
	const op = errors.Op("redis_plugin_stop")
	p.stopCh <- struct{}{}

	if p.universalClient != nil {
		err := p.universalClient.Close()
		if err != nil {
			return errors.E(op, err)
		}
	}

	return nil
}

func (p *Plugin) Name() string {
	return PluginName
}

// Available interface implementation
func (p *Plugin) Available() {}

// KVConstruct provides KV storage implementation over the redis plugin
func (p *Plugin) KVConstruct(key string) (kv.Storage, error) {
	const op = errors.Op("redis_plugin_provide")
	st, err := redis_kv.NewRedisDriver(p.log, key, p.cfgPlugin)
	if err != nil {
		return nil, errors.E(op, err)
	}

	return st, nil
}

func (p *Plugin) PSConstruct(key string) (pubsub.PubSub, error) {
	return redis_pubsub.NewPubSubDriver(p.log, key, p.cfgPlugin, p.stopCh)
}