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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
package redis
import (
"context"
"sync"
"github.com/go-redis/redis/v8"
"github.com/spiral/errors"
websocketsv1 "github.com/spiral/roadrunner/v2/pkg/proto/websockets/v1beta"
"github.com/spiral/roadrunner/v2/plugins/config"
"github.com/spiral/roadrunner/v2/plugins/logger"
"google.golang.org/protobuf/proto"
)
const PluginName = "redis"
type Plugin struct {
sync.RWMutex
// config for RR integration
cfg *Config
// logger
log logger.Logger
// redis universal client
universalClient redis.UniversalClient
// fanIn implementation used to deliver messages from all channels to the single websocket point
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 []byte) error {
p.Lock()
defer p.Unlock()
m := &websocketsv1.Message{}
err := proto.Unmarshal(msg, m)
if err != nil {
return errors.E(err)
}
for j := 0; j < len(m.GetTopics()); j++ {
f := p.universalClient.Publish(context.Background(), m.GetTopics()[j], msg)
if f.Err() != nil {
return f.Err()
}
}
return nil
}
func (p *Plugin) PublishAsync(msg []byte) {
go func() {
p.Lock()
defer p.Unlock()
m := &websocketsv1.Message{}
err := proto.Unmarshal(msg, m)
if err != nil {
p.log.Error("message unmarshal error")
return
}
for j := 0; j < len(m.GetTopics()); j++ {
f := p.universalClient.Publish(context.Background(), m.GetTopics()[j], msg)
if f.Err() != nil {
p.log.Error("redis publish", "error", f.Err())
}
}
}()
}
func (p *Plugin) Subscribe(connectionID string, topics ...string) error {
// just add a connection
for i := 0; i < len(topics); i++ {
// key - topic
// value - connectionID
hset := p.universalClient.SAdd(context.Background(), topics[i], connectionID)
res, err := hset.Result()
if err != nil {
return err
}
if res == 0 {
p.log.Warn("could not subscribe to the provided topic", "connectionID", connectionID, "topic", topics[i])
continue
}
}
// and subscribe after
return p.fanin.sub(topics...)
}
func (p *Plugin) Unsubscribe(connectionID string, topics ...string) error {
// Remove topics from the storage
for i := 0; i < len(topics); i++ {
srem := p.universalClient.SRem(context.Background(), topics[i], connectionID)
if srem.Err() != nil {
return srem.Err()
}
}
for i := 0; i < len(topics); i++ {
// if there are no such topics, we can safely unsubscribe from the redis
exists := p.universalClient.Exists(context.Background(), topics[i])
res, err := exists.Result()
if err != nil {
return err
}
// if we have associated connections - skip
if res == 1 { // exists means that topic still exists and some other nodes may have connections associated with it
continue
}
// else - unsubscribe
err = p.fanin.unsub(topics[i])
if err != nil {
return err
}
}
return nil
}
func (p *Plugin) Connections(topic string, res map[string]struct{}) {
hget := p.universalClient.SMembersMap(context.Background(), topic)
r, err := hget.Result()
if err != nil {
panic(err)
}
// assighn connections
// res expected to be from the sync.Pool
for k := range r {
res[k] = struct{}{}
}
}
// Next return next message
func (p *Plugin) Next() (*websocketsv1.Message, error) {
return <-p.fanin.consume(), nil
}
|