-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitor.go
184 lines (147 loc) · 3.57 KB
/
monitor.go
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
package redisutils
import (
"sync"
"time"
"github.com/gomodule/redigo/redis"
"github.com/sirupsen/logrus"
)
type MonitorState string
const (
MonitorStateConnecting = MonitorState("connecting")
MonitorStateConnected = MonitorState("connected")
MonitorStateDisconnected = MonitorState("disconnected")
)
type MonitorOptions struct {
// wait time between two connects
WaitDuration time.Duration
// On state change callback
OnStateChange func(state MonitorState)
// Logger
LoggerEntry *logrus.Entry
}
type MonitorOption func(*MonitorOptions)
func MonitorWaitDuration(waitDuration time.Duration) MonitorOption {
return func(options *MonitorOptions) {
options.WaitDuration = waitDuration
}
}
func MonitorOnStateChange(onStateChange func(state MonitorState)) MonitorOption {
return func(options *MonitorOptions) {
options.OnStateChange = onStateChange
}
}
func MonitorLogger(logger *logrus.Logger) MonitorOption {
return func(options *MonitorOptions) {
options.LoggerEntry = logger.WithFields(nil)
}
}
func MonitorLoggerEntry(entry *logrus.Entry) MonitorOption {
return func(options *MonitorOptions) {
options.LoggerEntry = entry
}
}
type Monitor struct {
dial Dialer
channelName string
onMessage func(data []byte)
waitDuration time.Duration
onStateChange func(state MonitorState)
logger *logrus.Entry
started bool
currentConn redis.Conn
currentConnMutex sync.Mutex
currentState MonitorState
}
func NewMonitor(dial Dialer, channelName string, onMessage func(data []byte), opts ...MonitorOption) *Monitor {
options := &MonitorOptions{
WaitDuration: 1 * time.Second,
OnStateChange: nil,
LoggerEntry: logrus.StandardLogger().WithFields(nil),
}
for _, setter := range opts {
setter(options)
}
m := &Monitor{
dial: dial,
channelName: channelName,
onMessage: onMessage,
waitDuration: options.WaitDuration,
onStateChange: options.OnStateChange,
logger: options.LoggerEntry,
started: true,
currentConn: nil,
currentState: MonitorState(""),
}
go m.start()
return m
}
func (m *Monitor) Close() error {
m.started = false
m.currentConnMutex.Lock()
defer m.currentConnMutex.Unlock()
if m.currentConn != nil {
m.currentConn.Close()
m.currentConn = nil
}
return nil
}
func (m *Monitor) setState(state MonitorState) {
if m.currentState == state {
return
}
m.currentState = state
m.logger.WithField("state", string(state)).Debug("Redis monitor state changed")
if m.onStateChange != nil {
m.onStateChange(state)
}
}
func (m *Monitor) do() {
defer func() {
if m.currentState == MonitorStateConnected {
m.setState(MonitorStateDisconnected)
}
}()
m.setState(MonitorStateConnecting)
conn, err := m.dial()
if err != nil {
m.logger.WithError(err).Warn("Redis monitor dial failed")
return
}
m.currentConnMutex.Lock()
m.currentConn = conn
m.currentConnMutex.Unlock()
defer func() {
m.currentConnMutex.Lock()
defer m.currentConnMutex.Unlock()
if m.currentConn != nil {
m.currentConn.Close()
}
}()
psc := redis.PubSubConn{Conn: conn}
err = psc.Subscribe(m.channelName)
if err != nil {
m.logger.WithError(err).Warn("Redis monitor subscribe failed")
return
}
for {
switch v := psc.Receive().(type) {
case redis.Message:
if m.started {
m.onMessage(v.Data)
}
case redis.Subscription:
m.setState(MonitorStateConnected)
case error:
if m.started {
m.logger.WithError(v).Warn("Redis monitor receive error")
}
return
}
}
}
func (m *Monitor) start() {
for m.started {
m.do()
time.Sleep(m.waitDuration)
}
}