-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker_client.go
235 lines (177 loc) · 5.57 KB
/
worker_client.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package redisSyncFanoutQueue
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/go-redis/redis/v8"
"github.com/sahmad98/go-ringbuffer"
redisLuaScriptUtils "github.com/zavitax/redis-lua-script-utils-go"
)
type GetWorkerMetricsOptions struct {
}
type WorkerMetrics struct {
ReceivedMessagesCount int64
InvalidMessagesCount int64
MinLatency time.Duration
MaxLatency time.Duration
AvgLatency time.Duration
}
type RedisQueueWorkerClient interface {
Close() error
GetMetrics(ctx context.Context, options *GetWorkerMetricsOptions) (*WorkerMetrics, error)
}
type redisQueueWorkerClient struct {
mu sync.RWMutex
wg sync.WaitGroup
options *WorkerOptions
redis_subscriber_timeout *redis.Client
redis_subscriber_message *redis.Client
redis_context context.Context
redis_cancel_func context.CancelFunc
statLastMessageLatencies *ringbuffer.RingBuffer
statRecvMsgCount int64
statInvalidMsgCount int64
keyPubsubAdminEventsRemoveClientTopic string
keyRoomPubsub string
redisKeys []*redisLuaScriptUtils.RedisKey
}
func (c *redisQueueWorkerClient) keyGlobalLock(tag string) string {
return fmt.Sprintf("%s::lock::%s", c.options.RedisKeyPrefix, tag)
}
func (c *redisQueueWorkerClient) keyRoomSetOfKnownClients(room string) string {
return fmt.Sprintf("%s::room::%s::known-clients", c.options.RedisKeyPrefix, room)
}
func NewWorkerClient(ctx context.Context, options *WorkerOptions) (RedisQueueWorkerClient, error) {
if err := options.Validate(); err != nil {
return nil, err
}
var c = &redisQueueWorkerClient{}
c.options = options
c.redis_context, c.redis_cancel_func = context.WithCancel(context.Background())
c.keyPubsubAdminEventsRemoveClientTopic = fmt.Sprintf("%s::global::pubsub::admin::removed-clients", c.options.RedisKeyPrefix)
c.keyRoomPubsub = fmt.Sprintf("%s::global::msg-queue", c.options.RedisKeyPrefix)
c.statLastMessageLatencies = ringbuffer.NewRingBuffer(100)
c.redis_subscriber_timeout = redis.NewClient(c.options.RedisOptions)
c.redis_subscriber_message = redis.NewClient(c.options.RedisOptions)
c.wg.Add(1)
go (func() {
defer c.wg.Done()
// Listen for client timeout messages
for {
msgs, err := c.redis_subscriber_timeout.BLPop(c.redis_context, time.Minute, c.keyPubsubAdminEventsRemoveClientTopic).Result()
if err == nil {
for _, msg := range msgs {
c._handleTimeoutMessage(c.redis_context, msg)
}
}
if err == context.Canceled {
return
}
}
})()
c.wg.Add(1)
go (func() {
defer c.wg.Done()
// Listen for client timeout messages
for {
msgs, err := c.redis_subscriber_message.BLPop(c.redis_context, time.Minute, c.keyRoomPubsub).Result()
if err == nil {
for _, msg := range msgs {
c._handleMessage(c.redis_context, msg)
}
} else if err == context.Canceled {
// Context cancelled, we're done
return
}
}
})()
return c, nil
}
func (c *redisQueueWorkerClient) createStdRoomArgs(clientId string, room string) *redisLuaScriptUtils.RedisScriptArguments {
args := make(redisLuaScriptUtils.RedisScriptArguments)
args["argClientID"] = clientId
args["argRoomID"] = room
args["argCurrentTimestamp"] = currentTimestamp()
return &args
}
func (c *redisQueueWorkerClient) Close() error {
c.mu.Lock()
defer c.mu.Unlock()
c.redis_cancel_func()
c.wg.Wait()
return nil
}
func (c *redisQueueWorkerClient) _handleTimeoutMessage(ctx context.Context, message string) {
parts := strings.SplitN(message, "::", 2)
clientId := parts[0]
room := parts[1]
c.options.HandleRoomClientTimeout(ctx, &clientId, &room)
}
func (c *redisQueueWorkerClient) _handleMessage(ctx context.Context, msgData string) error {
msg, _, err := parseMsgData(msgData)
if err != nil {
atomic.AddInt64(&c.statInvalidMsgCount, 1)
return err
}
c.statLastMessageLatencies.Write(msg.MessageContext.Latency)
atomic.AddInt64(&c.statRecvMsgCount, 1)
clientIds, err := c.redis_subscriber_message.ZRangeByScore(ctx, c.keyRoomSetOfKnownClients(msg.Room), &redis.ZRangeBy{
Min: "-inf",
Max: "+inf",
Offset: 0,
Count: 1000000,
}).Result()
if err != nil {
return err
}
for _, clientKey := range clientIds {
parts := strings.SplitN(clientKey, "::", 2)
clientId := parts[0]
c.options.HandleMessage(ctx, &clientId, msg)
}
return nil
}
func (c *redisQueueWorkerClient) getMetricsParseLatencies(result *WorkerMetrics) {
latencies := make([]interface{}, c.statLastMessageLatencies.Size)
copy(latencies, c.statLastMessageLatencies.Container)
var sumLatencyMs int64
var minLatencyMs int64 = 0
var maxLatencyMs int64 = 0
var numLatencies int64 = 0
if len(latencies) > 0 && latencies[0] != nil {
minLatencyMs = latencies[0].(time.Duration).Milliseconds()
}
for _, latency := range latencies {
if latency != nil {
numLatencies++
ms := latency.(time.Duration).Milliseconds()
sumLatencyMs += ms
if ms < minLatencyMs {
minLatencyMs = ms
}
if ms > maxLatencyMs {
maxLatencyMs = ms
}
}
}
result.MinLatency = time.Duration(minLatencyMs) * time.Millisecond
result.MaxLatency = time.Duration(maxLatencyMs) * time.Millisecond
if numLatencies > 0 {
result.AvgLatency = time.Duration(sumLatencyMs/numLatencies) * time.Millisecond
} else {
result.AvgLatency = time.Duration(0)
}
}
func (c *redisQueueWorkerClient) GetMetrics(ctx context.Context, options *GetWorkerMetricsOptions) (*WorkerMetrics, error) {
c.mu.RLock()
defer c.mu.RUnlock()
result := &WorkerMetrics{
ReceivedMessagesCount: c.statRecvMsgCount,
InvalidMessagesCount: c.statInvalidMsgCount,
}
c.getMetricsParseLatencies(result)
return result, nil
}