forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_group_cache.go
162 lines (127 loc) · 3.44 KB
/
user_group_cache.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
package memsto
import (
"fmt"
"sync"
"time"
"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/pkg/errors"
"github.com/toolkits/pkg/logger"
)
type UserGroupCacheType struct {
statTotal int64
statLastUpdated int64
ctx *ctx.Context
stats *Stats
sync.RWMutex
ugs map[int64]*models.UserGroup // key: id
}
func NewUserGroupCache(ctx *ctx.Context, stats *Stats) *UserGroupCacheType {
ugc := &UserGroupCacheType{
statTotal: -1,
statLastUpdated: -1,
ctx: ctx,
stats: stats,
ugs: make(map[int64]*models.UserGroup),
}
ugc.SyncUserGroups()
return ugc
}
func (ugc *UserGroupCacheType) StatChanged(total, lastUpdated int64) bool {
if ugc.statTotal == total && ugc.statLastUpdated == lastUpdated {
return false
}
return true
}
func (ugc *UserGroupCacheType) Set(ugs map[int64]*models.UserGroup, total, lastUpdated int64) {
ugc.Lock()
ugc.ugs = ugs
ugc.Unlock()
// only one goroutine used, so no need lock
ugc.statTotal = total
ugc.statLastUpdated = lastUpdated
}
func (ugc *UserGroupCacheType) GetByUserGroupId(id int64) *models.UserGroup {
ugc.RLock()
defer ugc.RUnlock()
return ugc.ugs[id]
}
func (ugc *UserGroupCacheType) GetByUserGroupIds(ids []int64) []*models.UserGroup {
set := make(map[int64]struct{})
ugc.RLock()
defer ugc.RUnlock()
var ugs []*models.UserGroup
for _, id := range ids {
if ugc.ugs[id] == nil {
continue
}
if _, has := set[id]; has {
continue
}
ugs = append(ugs, ugc.ugs[id])
set[id] = struct{}{}
}
if ugs == nil {
return []*models.UserGroup{}
}
return ugs
}
func (ugc *UserGroupCacheType) SyncUserGroups() {
err := ugc.syncUserGroups()
if err != nil {
fmt.Println("failed to sync user groups:", err)
exit(1)
}
go ugc.loopSyncUserGroups()
}
func (ugc *UserGroupCacheType) loopSyncUserGroups() {
duration := time.Duration(9000) * time.Millisecond
for {
time.Sleep(duration)
if err := ugc.syncUserGroups(); err != nil {
logger.Warning("failed to sync user groups:", err)
}
}
}
func (ugc *UserGroupCacheType) syncUserGroups() error {
start := time.Now()
stat, err := models.UserGroupStatistics(ugc.ctx)
if err != nil {
return errors.WithMessage(err, "failed to exec UserGroupStatistics")
}
if !ugc.StatChanged(stat.Total, stat.LastUpdated) {
ugc.stats.GaugeCronDuration.WithLabelValues("sync_user_groups").Set(0)
ugc.stats.GaugeSyncNumber.WithLabelValues("sync_user_groups").Set(0)
logger.Debug("user_group not changed")
return nil
}
lst, err := models.UserGroupGetAll(ugc.ctx)
if err != nil {
return errors.WithMessage(err, "failed to exec UserGroupGetAll")
}
m := make(map[int64]*models.UserGroup)
for i := 0; i < len(lst); i++ {
m[lst[i].Id] = lst[i]
}
// fill user ids
members, err := models.UserGroupMemberGetAll(ugc.ctx)
if err != nil {
return errors.WithMessage(err, "failed to exec UserGroupMemberGetAll")
}
for i := 0; i < len(members); i++ {
ug, has := m[members[i].GroupId]
if !has {
continue
}
if ug == nil {
continue
}
ug.UserIds = append(ug.UserIds, members[i].UserId)
}
ugc.Set(m, stat.Total, stat.LastUpdated)
ms := time.Since(start).Milliseconds()
ugc.stats.GaugeCronDuration.WithLabelValues("sync_user_groups").Set(float64(ms))
ugc.stats.GaugeSyncNumber.WithLabelValues("sync_user_groups").Set(float64(len(m)))
logger.Infof("timer: sync user groups done, cost: %dms, number: %d", ms, len(m))
return nil
}