summaryrefslogtreecommitdiff
path: root/plugins/redis/plugin.go
blob: fe465340dc15f18849ebf7aaf590b320971b0313 (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
package redis

import (
	"github.com/go-redis/redis/v8"
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/logger"
)

const PluginName = "redis"

type Plugin struct {
	// config for RR integration
	cfg *Config
	// logger
	log logger.Logger
	// redis universal client
	universalClient redis.UniversalClient
}

func (s *Plugin) GetClient() redis.UniversalClient {
	return s.universalClient
}

func (s *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
	const op = errors.Op("redis plugin init")
	s.cfg = &Config{}
	s.cfg.InitDefaults()

	err := cfg.UnmarshalKey(PluginName, &s.cfg)
	if err != nil {
		return errors.E(op, errors.Disabled, err)
	}

	s.log = log

	s.universalClient = redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs:              s.cfg.Addrs,
		DB:                 s.cfg.DB,
		Username:           s.cfg.Username,
		Password:           s.cfg.Password,
		SentinelPassword:   s.cfg.SentinelPassword,
		MaxRetries:         s.cfg.MaxRetries,
		MinRetryBackoff:    s.cfg.MaxRetryBackoff,
		MaxRetryBackoff:    s.cfg.MaxRetryBackoff,
		DialTimeout:        s.cfg.DialTimeout,
		ReadTimeout:        s.cfg.ReadTimeout,
		WriteTimeout:       s.cfg.WriteTimeout,
		PoolSize:           s.cfg.PoolSize,
		MinIdleConns:       s.cfg.MinIdleConns,
		MaxConnAge:         s.cfg.MaxConnAge,
		PoolTimeout:        s.cfg.PoolTimeout,
		IdleTimeout:        s.cfg.IdleTimeout,
		IdleCheckFrequency: s.cfg.IdleCheckFreq,
		ReadOnly:           s.cfg.ReadOnly,
		RouteByLatency:     s.cfg.RouteByLatency,
		RouteRandomly:      s.cfg.RouteRandomly,
		MasterName:         s.cfg.MasterName,
	})

	return nil
}

func (s *Plugin) Serve() chan error {
	errCh := make(chan error, 1)
	return errCh
}

func (s Plugin) Stop() error {
	return s.universalClient.Close()
}

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