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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
package redis
import (
"context"
"sync"
"github.com/go-redis/redis/v8"
"github.com/spiral/errors"
"github.com/spiral/roadrunner/v2/pkg/pubsub"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/logger"
)
const PluginName = "redis"
type Plugin struct {
sync.Mutex
// config for RR integration
cfg *Config
// logger
log logger.Logger
// redis universal client
universalClient redis.UniversalClient
fanin *FanIn
}
func (p *Plugin) GetClient() redis.UniversalClient {
return p.universalClient
}
func (p *Plugin) Init(cfg config.Configurer, log logger.Logger) error {
const op = errors.Op("redis_plugin_init")
if !cfg.Has(PluginName) {
return errors.E(op, errors.Disabled)
}
err := cfg.UnmarshalKey(PluginName, &p.cfg)
if err != nil {
return errors.E(op, errors.Disabled, err)
}
p.cfg.InitDefaults()
p.log = log
p.universalClient = redis.NewUniversalClient(&redis.UniversalOptions{
Addrs: p.cfg.Addrs,
DB: p.cfg.DB,
Username: p.cfg.Username,
Password: p.cfg.Password,
SentinelPassword: p.cfg.SentinelPassword,
MaxRetries: p.cfg.MaxRetries,
MinRetryBackoff: p.cfg.MaxRetryBackoff,
MaxRetryBackoff: p.cfg.MaxRetryBackoff,
DialTimeout: p.cfg.DialTimeout,
ReadTimeout: p.cfg.ReadTimeout,
WriteTimeout: p.cfg.WriteTimeout,
PoolSize: p.cfg.PoolSize,
MinIdleConns: p.cfg.MinIdleConns,
MaxConnAge: p.cfg.MaxConnAge,
PoolTimeout: p.cfg.PoolTimeout,
IdleTimeout: p.cfg.IdleTimeout,
IdleCheckFrequency: p.cfg.IdleCheckFreq,
ReadOnly: p.cfg.ReadOnly,
RouteByLatency: p.cfg.RouteByLatency,
RouteRandomly: p.cfg.RouteRandomly,
MasterName: p.cfg.MasterName,
})
// init fanin
p.fanin = NewFanIn(p.universalClient, log)
return nil
}
func (p *Plugin) Serve() chan error {
errCh := make(chan error)
return errCh
}
func (p *Plugin) Stop() error {
const op = errors.Op("redis_plugin_stop")
err := p.fanin.Stop()
if err != nil {
return errors.E(op, err)
}
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() {}
func (p *Plugin) Publish(msg []*pubsub.Message) error {
p.Lock()
defer p.Unlock()
for i := 0; i < len(msg); i++ {
for j := 0; j < len(msg[i].Topics); j++ {
f := p.universalClient.Publish(context.Background(), msg[i].Topics[j], msg[i])
if f.Err() != nil {
return f.Err()
}
}
}
return nil
}
func (p *Plugin) PublishAsync(msg []*pubsub.Message) {
go func() {
p.Lock()
defer p.Unlock()
for i := 0; i < len(msg); i++ {
for j := 0; j < len(msg[i].Topics); j++ {
f := p.universalClient.Publish(context.Background(), msg[i].Topics[j], msg[i])
if f.Err() != nil {
p.log.Error("errors publishing message", "topic", msg[i].Topics[j], "error", f.Err().Error())
continue
}
}
}
}()
}
func (p *Plugin) Subscribe(topics ...string) error {
return p.fanin.AddChannel(topics...)
}
func (p *Plugin) Unsubscribe(topics ...string) error {
return p.fanin.RemoveChannel(topics...)
}
// Next return next message
func (p *Plugin) Next() (*pubsub.Message, error) {
return <-p.fanin.Consume(), nil
}
|