-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdeletequeue.go
427 lines (387 loc) · 11.8 KB
/
deletequeue.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"os"
"strings"
"sync"
"time"
"github.com/go-redis/redis/v8"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
log "maunium.net/go/maulogger/v2"
"maunium.net/go/mautrix/id"
)
type LeavingRoom struct {
RoomID id.RoomID `json:"roomID"`
Kick []id.UserID `json:"kick"`
}
type PendingRoom struct {
RoomID id.RoomID `json:"roomID"`
QueueTime time.Time `json:"queueTime"`
}
var queueLog = log.Sub("Queue")
var leaveQueue chan *LeavingRoom
var deleteQueue chan id.RoomID
var rds *redis.Client
var leaveQueueKey = "yeetserv:leave_queue"
var deleteQueueKey = "yeetserv:delete_queue"
var pauseDeleteQueueKey = "yeetserv:pause_delete_queue"
var errorQueueKey = "yeetserv:error_queue"
var promLeaveQueueGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "yeetserv_leave_queue_length",
Help: "Current length of yeetserv's leave queue",
},
)
var promDeleteQueueGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "yeetserv_delete_queue_length",
Help: "Current length of yeetserv's delete queue",
},
)
var promErrorQueueGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "yeetserv_error_queue_length",
Help: "Current length of yeetserv's error queue",
},
)
var promLeaveCounter = promauto.NewCounter(
prometheus.CounterOpts{
Name: "yeetserv_leave_count",
Help: "Number of leaves performed",
},
)
var promDeleteCounter = promauto.NewCounter(
prometheus.CounterOpts{
Name: "yeetserv_delete_count",
Help: "Number of deletes performed",
},
)
var promLeaveSeconds = promauto.NewHistogram(
prometheus.HistogramOpts{
Name: "yeetserv_leave_seconds",
Help: "Time taken to leave in seconds",
Buckets: []float64{10, 30, 60, 300, 600, 1200, 3600},
},
)
var promDeleteSeconds = promauto.NewHistogram(
prometheus.HistogramOpts{
Name: "yeetserv_delete_seconds",
Help: "Time taken to delete in seconds",
Buckets: []float64{10, 30, 60, 300, 600, 1200, 3600},
},
)
var promLeaveQueueNextAgeGauge = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "yeetserv_leave_queue_next_age_seconds",
Help: "Current age of the next item in the leave queue in seconds",
},
)
var promLeaveQueuePostponeDurationGuage = promauto.NewGauge(
prometheus.GaugeOpts{
Name: "yeetserv_leave_queue_postpone_duration_seconds",
Help: "How long yeetserv should wait before leaving a room in seconds",
},
)
func initQueue() {
if len(cfg.RedisURL) > 0 {
log.Debugln("Initializing redis client")
redisURL, err := url.Parse(cfg.RedisURL)
if err != nil {
log.Fatalln("Bad redis URL:", err)
os.Exit(4)
}
var opts redis.Options
opts.Addr = redisURL.Host
opts.Username = redisURL.User.Username()
opts.Password, _ = redisURL.User.Password()
rds = redis.NewClient(&opts)
if cfg.DryRun {
leaveQueueKey = strings.Replace(leaveQueueKey, ":", ":dry_run:", 1)
deleteQueueKey = strings.Replace(deleteQueueKey, ":", ":dry_run:", 1)
errorQueueKey = strings.Replace(errorQueueKey, ":", ":dry_run:", 1)
}
log.Debugln("Redis leave queue key:", leaveQueueKey)
log.Debugln("Redis delete queue key:", deleteQueueKey)
log.Debugln("Redis error queue key:", errorQueueKey)
} else {
leaveQueue = make(chan *LeavingRoom, 8192)
deleteQueue = make(chan id.RoomID, 8192)
}
promLeaveQueuePostponeDurationGuage.Set(cfg.PostponeDeletion.Seconds())
}
func loopQueueStats(ctx context.Context, wg *sync.WaitGroup) {
if rds == nil {
return
}
defer func() {
queueLog.Infoln("Queue stats updater exiting")
wg.Done()
}()
for {
promLeaveQueueGauge.Set(float64(rds.LLen(ctx, leaveQueueKey).Val()))
promDeleteQueueGauge.Set(float64(rds.LLen(ctx, deleteQueueKey).Val()))
promErrorQueueGauge.Set(float64(rds.LLen(ctx, errorQueueKey).Val()))
select {
case <-time.After(30 * time.Second):
case <-ctx.Done():
return
}
}
}
func PushLeaveQueue(ctx context.Context, roomID id.RoomID, usersToKick []id.UserID) error {
leavingRoom := &LeavingRoom{RoomID: roomID, Kick: usersToKick}
if rds != nil {
jsonData, err := json.Marshal(leavingRoom)
if err != nil {
return fmt.Errorf("failed to marshal %s to redis: %w", roomID, err)
}
err = rds.RPush(ctx, leaveQueueKey, jsonData).Err()
if err != nil {
return fmt.Errorf("failed to push %s to redis: %w", roomID, err)
}
} else {
leaveQueue <- leavingRoom
promLeaveQueueGauge.Set(float64(len(leaveQueue)))
}
return nil
}
func PushDeleteQueue(ctx context.Context, roomID id.RoomID) error {
if rds != nil {
pendingRoom := &PendingRoom{RoomID: roomID, QueueTime: time.Now()}
jsonData, err := json.Marshal(pendingRoom)
if err != nil {
return fmt.Errorf("failed to marshal %s to redis: %w", roomID, err)
}
err = rds.RPush(ctx, deleteQueueKey, jsonData).Err()
if err != nil {
return fmt.Errorf("failed to push %s to redis: %w", roomID, err)
}
} else {
deleteQueue <- roomID
promDeleteQueueGauge.Set(float64(len(deleteQueue)))
}
return nil
}
func loopLeaveQueue(ctx context.Context, wg *sync.WaitGroup) {
defer func() {
queueLog.Infoln("Queue leave loop exiting")
wg.Done()
}()
for {
success := consumeLeaveQueue(ctx)
var wait time.Duration
// we go as fast as we can unless there's nothing to do
if !success {
wait = time.Second * 1
}
select {
case <-time.After(wait):
case <-ctx.Done():
return
}
}
}
func loopDeleteQueue(ctx context.Context, wg *sync.WaitGroup) {
defer func() {
queueLog.Infoln("Queue delete loop exiting")
wg.Done()
}()
for {
consumeDeleteQueue(ctx)
select {
case <-time.After(cfg.QueueSleep):
case <-ctx.Done():
return
}
}
}
func pushErrorQueue(roomID id.RoomID) {
if rds == nil {
return
}
queueLog.Debugln("Marking", roomID, "as errored in redis")
ctx := context.Background()
err := rds.RPush(ctx, errorQueueKey, roomID.String()).Err()
if err != nil {
queueLog.Errorln("Failed to mark %s as errored in redis: %v", roomID, err)
return
}
}
func popLeaveQueue(ctx context.Context) (*LeavingRoom, bool) {
if rds != nil {
nextItem, err := rds.BLPop(ctx, 0, leaveQueueKey).Result()
if err != nil {
if !errors.Is(err, context.Canceled) {
queueLog.Errorln("Failed to get next leave item from redis:", err)
}
return nil, false
}
leavingRoom := &LeavingRoom{}
if err := json.Unmarshal([]byte(nextItem[1]), leavingRoom); err != nil {
queueLog.Errorln("Failed to unmarshal next leave item from redis:", err)
return nil, false
}
return leavingRoom, true
} else {
select {
case leavingRoom := <-leaveQueue:
promLeaveQueueGauge.Set(float64(len(leaveQueue)))
return leavingRoom, true
case <-ctx.Done():
promLeaveQueueGauge.Set(0)
return nil, false
}
}
}
func consumeLeaveQueue(ctx context.Context) bool {
leavingRoom, ok := popLeaveQueue(ctx)
if !ok {
return false
}
if cfg.DryRun {
queueLog.Debugfln("Not requesting admin API to leave room %s (dry run)", leavingRoom.RoomID)
} else {
queueLog.Debugfln("Requesting admin API to leave room %s", leavingRoom.RoomID)
}
startTime := time.Now()
adminContext := context.WithValue(ctx, logContextKey, queueLog)
for _, userID := range leavingRoom.Kick {
if userClient, err := AdminLogin(adminContext, userID); err != nil {
queueLog.Warnfln("Failed to log in as %s to leave %s: %v", userID, leavingRoom.RoomID, err)
} else if cfg.DryRun {
queueLog.Debugfln("Not leaving %s as %s as we're in dry run mode", leavingRoom.RoomID, userID)
} else if _, err = userClient.LeaveRoom(leavingRoom.RoomID); err != nil {
queueLog.Warnfln("Failed to leave %s as %s: %w", leavingRoom.RoomID, userID, err)
} else {
queueLog.Debugfln("Successfully left %s as %s", leavingRoom.RoomID, userID)
}
}
aliases, err := adminClient.GetAliases(leavingRoom.RoomID)
if aliases != nil {
for _, alias := range aliases.Aliases {
if cfg.DryRun {
queueLog.Debugfln("Not removing alias %s of %s as we're in dry run mode", alias, leavingRoom.RoomID)
} else {
if _, deleteErr := asmuxClient.DeleteAlias(alias); deleteErr != nil {
queueLog.Warnfln("Failed to remove alias %s of %s: %v", alias, leavingRoom.RoomID, deleteErr)
} else {
queueLog.Debugfln("Successfully removed alias %s of %s", alias, leavingRoom.RoomID)
}
}
}
}
if err == nil {
err = PushDeleteQueue(context.Background(), leavingRoom.RoomID)
}
if err != nil {
queueLog.Warnfln("Failed to push %s to delete queue: %w", leavingRoom.RoomID, err)
if err = PushLeaveQueue(ctx, leavingRoom.RoomID, leavingRoom.Kick); err != nil {
queueLog.Errorfln("Failed to put room %s back to leave queue: %v", leavingRoom.RoomID, err)
}
return false
} else {
leaveTime := time.Now().Sub(startTime)
queueLog.Debugln("Room", leavingRoom.RoomID, "successfully left in", leaveTime, "and moved to delete queue")
promLeaveCounter.Add(1)
promLeaveSeconds.Observe(leaveTime.Seconds())
return true
}
}
func popDeleteQueue(ctx context.Context) (id.RoomID, bool) {
if rds != nil {
nextItem, err := rds.LRange(ctx, deleteQueueKey, 0, 0).Result()
if err != nil {
queueLog.Errorln("Failed to peek next item from redis:", err)
return "", false
}
if len(nextItem) == 0 {
return "", false
}
// we only check for due if we get valid json, otherwise it's a legacy plain room id OR requeued error room ID
pendingRoom := &PendingRoom{}
if err := json.Unmarshal([]byte(nextItem[0]), pendingRoom); err == nil {
sinceQueueTime := time.Since(pendingRoom.QueueTime)
promLeaveQueueNextAgeGauge.Set(sinceQueueTime.Seconds())
if sinceQueueTime < cfg.PostponeDeletion {
queueLog.Debugfln("Next item from delete queue is due on %v", pendingRoom.QueueTime.Add(cfg.PostponeDeletion))
return "", false
}
}
// really pop it now, we assume no one pushes to the head of the queue
nextItem, err = rds.BLPop(ctx, 0, deleteQueueKey).Result()
if err != nil {
if !errors.Is(err, context.Canceled) {
queueLog.Errorln("Failed to get next item from redis:", err)
}
return "", false
}
if err := json.Unmarshal([]byte(nextItem[1]), pendingRoom); err == nil {
return pendingRoom.RoomID, true
}
return id.RoomID(nextItem[1]), true
} else {
select {
case roomID := <-deleteQueue:
promDeleteQueueGauge.Set(float64(len(deleteQueue)))
return roomID, true
case <-ctx.Done():
promDeleteQueueGauge.Set(0)
return "", false
}
}
}
func waitIfDeletePaused(ctx context.Context) {
for {
paused, err := rds.Get(ctx, pauseDeleteQueueKey).Result()
if err != nil && paused == "" {
return
}
queueLog.Debugln("Waiting, deletes currently paused")
time.Sleep(10 * time.Second)
}
}
func consumeDeleteQueue(ctx context.Context) {
if rds != nil {
waitIfDeletePaused(ctx)
}
roomID, ok := popDeleteQueue(ctx)
if !ok {
return
}
if cfg.DryRun {
queueLog.Debugfln("Not requesting admin API to clean up room %s (dry run)", roomID)
} else {
queueLog.Debugfln("Requesting admin API to clean up room %s", roomID)
}
startTime := time.Now()
if len(cfg.AsmuxAccessToken) > 0 && cfg.AsmuxMainURL != nil {
queueLog.Debugln("Requesting asmux to forget about room", roomID)
err := asmuxDeleteRoom(ctx, roomID)
if err != nil {
queueLog.Warnfln("Failed to request asmux to forget about room %s: %v", roomID, err)
}
}
_, err := adminDeleteRoom(ctx, ReqDeleteRoom{RoomID: roomID, Purge: true, ForcePurge: cfg.ForcePurge})
if err != nil {
if errors.Is(err, context.Canceled) {
queueLog.Debugfln("Context was canceled while cleaning up %s, putting it back in the queue", roomID)
err = PushDeleteQueue(context.Background(), roomID)
if err != nil {
queueLog.Errorfln("Failed to put %s back in the queue: %v", roomID, err)
}
} else {
queueLog.Warnfln("Failed to clean up %s: %v", roomID, err)
go pushErrorQueue(roomID)
}
} else {
deleteTime := time.Now().Sub(startTime)
queueLog.Debugln("Room", roomID, "successfully cleaned up in", deleteTime)
promDeleteCounter.Add(1)
promDeleteSeconds.Observe(deleteTime.Seconds())
}
}