forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_group_member.go
59 lines (45 loc) · 1.2 KB
/
user_group_member.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
package timer
import (
"fmt"
"math/rand"
"time"
"github.com/didi/nightingale/v5/cache"
"github.com/didi/nightingale/v5/models"
"github.com/toolkits/pkg/logger"
)
func SyncUserGroupMember() {
if err := syncUserGroupMember(); err != nil {
fmt.Println(err)
exit(1)
}
go loopSyncUserGroupMember()
}
func loopSyncUserGroupMember() {
randtime := rand.Intn(60000)
fmt.Printf("timer: sync group users: random sleep %dms\n", randtime)
time.Sleep(time.Duration(randtime) * time.Millisecond)
interval := time.Duration(60) * time.Second
for {
time.Sleep(interval)
if err := syncUserGroupMember(); err != nil {
logger.Warning(err)
}
}
}
func syncUserGroupMember() error {
start := time.Now()
members, err := models.UserGroupMemberGetAll()
if err != nil {
return fmt.Errorf("UserGroupMemberGetAll error: %v", err)
}
memberMap := make(map[int64]map[int64]struct{})
for _, m := range members {
if _, exists := memberMap[m.GroupId]; !exists {
memberMap[m.GroupId] = make(map[int64]struct{})
}
memberMap[m.GroupId][m.UserId] = struct{}{}
}
cache.UserGroupMember.SetAll(memberMap)
logger.Debugf("timer: sync group users done, cost: %dms", time.Since(start).Milliseconds())
return nil
}