-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcleanup.go
More file actions
63 lines (49 loc) · 1.65 KB
/
Copy pathcleanup.go
File metadata and controls
63 lines (49 loc) · 1.65 KB
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
package main
import (
"time"
)
func startCleanupTimer() {
ticker := time.NewTicker(10 * time.Second)
for {
currentTime := <-ticker.C
go checkOnGoingMessagesCooldown(currentTime)
go checkOngoingReportCooldowns(currentTime)
go checkOngoingReportCleanup(currentTime)
}
}
func removeNeededCooldownUsers(currentTime time.Time, mapPointer map[string]time.Time) {
markedForRemoval := make([]string, 0)
for userID, cooldown := range mapPointer {
if currentTime.After(cooldown) {
markedForRemoval = append(markedForRemoval, userID)
}
}
for _, userID := range markedForRemoval {
delete(mapPointer, userID)
}
}
func checkOnGoingMessagesCooldown(currentTime time.Time) {
userCooldownsMessagesMutex.Lock()
defer userCooldownsMessagesMutex.Unlock()
removeNeededCooldownUsers(currentTime, userCooldownsMessages)
}
func checkOngoingReportCooldowns(currentTime time.Time) {
currentUsersOnReportMutex.Lock()
defer currentUsersOnReportMutex.Unlock()
removeNeededCooldownUsers(currentTime, currentUsersOnReportCooldown)
}
func checkOngoingReportCleanup(currentTime time.Time) {
currentReportsMutex.Lock()
defer currentReportsMutex.Unlock()
markedForRemoval := make([]string, 0)
for userID, report := range currentOngoingReports {
// If this validates true that means the last interaction with the user has been larger than our timeout
if currentTime.After(report.lastInteraction.Add(time.Duration(config.ReportTimeoutMinutes) * time.Minute)) {
markedForRemoval = append(markedForRemoval, userID)
}
}
for _, userID := range markedForRemoval {
delete(currentOngoingReports, userID)
sendMessageToDM(config.Messages.InactiveReport, userID)
}
}