forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_cache.go
196 lines (159 loc) · 4.63 KB
/
user_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
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
package memsto
import (
"fmt"
"sync"
"time"
"github.com/ccfos/nightingale/v6/dumper"
"github.com/ccfos/nightingale/v6/models"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/flashduty"
"github.com/pkg/errors"
"github.com/toolkits/pkg/logger"
)
type UserCacheType struct {
statTotal int64
statLastUpdated int64
configsTotal int64
configsLastUpdated int64
ctx *ctx.Context
stats *Stats
sync.RWMutex
users map[int64]*models.User // key: id
}
func NewUserCache(ctx *ctx.Context, stats *Stats) *UserCacheType {
uc := &UserCacheType{
statTotal: -1,
statLastUpdated: -1,
ctx: ctx,
stats: stats,
users: make(map[int64]*models.User),
}
uc.SyncUsers()
return uc
}
func (uc *UserCacheType) StatChanged(total, lastUpdated, configsTotal, configsLastUpdated int64) bool {
if uc.statTotal == total && uc.statLastUpdated == lastUpdated && uc.configsTotal == configsTotal && uc.configsLastUpdated == configsLastUpdated {
return false
}
return true
}
func (uc *UserCacheType) Set(m map[int64]*models.User, total, lastUpdated, configsTotal, configsLastUpdated int64) {
uc.Lock()
uc.users = m
uc.Unlock()
// only one goroutine used, so no need lock
uc.statTotal = total
uc.statLastUpdated = lastUpdated
uc.configsTotal = configsTotal
uc.configsLastUpdated = configsLastUpdated
}
func (uc *UserCacheType) GetByUserId(id int64) *models.User {
uc.RLock()
defer uc.RUnlock()
return uc.users[id]
}
func (uc *UserCacheType) GetByUsername(name string) *models.User {
uc.RLock()
defer uc.RUnlock()
for _, v := range uc.users {
if v.Username == name {
return v
}
}
return nil
}
func (uc *UserCacheType) GetByUserIds(ids []int64) []*models.User {
set := make(map[int64]struct{})
uc.RLock()
defer uc.RUnlock()
var users []*models.User
for _, id := range ids {
if uc.users[id] == nil {
continue
}
if _, has := set[id]; has {
continue
}
users = append(users, uc.users[id])
set[id] = struct{}{}
}
if users == nil {
users = []*models.User{}
}
return users
}
func (uc *UserCacheType) GetMaintainerUsers() []*models.User {
uc.RLock()
defer uc.RUnlock()
var users []*models.User
for _, v := range uc.users {
if v.Maintainer == 1 {
users = append(users, v)
}
}
if users == nil {
users = []*models.User{}
}
return users
}
func (uc *UserCacheType) SyncUsers() {
err := uc.syncUsers()
if err != nil {
fmt.Println("failed to sync users:", err)
exit(1)
}
go uc.loopSyncUsers()
}
func (uc *UserCacheType) loopSyncUsers() {
duration := time.Duration(9000) * time.Millisecond
for {
time.Sleep(duration)
if err := uc.syncUsers(); err != nil {
logger.Warning("failed to sync users:", err)
}
}
}
func (uc *UserCacheType) syncUsers() error {
start := time.Now()
stat, err := models.UserStatistics(uc.ctx)
if err != nil {
dumper.PutSyncRecord("users", start.Unix(), -1, -1, "failed to query statistics: "+err.Error())
return errors.WithMessage(err, "failed to exec UserStatistics")
}
configsStat, err := models.ConfigsUserVariableStatistics(uc.ctx)
if err != nil {
dumper.PutSyncRecord("user_variables", start.Unix(), -1, -1, "failed to query statistics: "+err.Error())
return errors.WithMessage(err, "failed to exec ConfigsUserVariableStatistics")
}
if !uc.StatChanged(stat.Total, stat.LastUpdated, configsStat.Total, configsStat.LastUpdated) {
uc.stats.GaugeCronDuration.WithLabelValues("sync_users").Set(0)
uc.stats.GaugeSyncNumber.WithLabelValues("sync_users").Set(0)
dumper.PutSyncRecord("users", start.Unix(), -1, -1, "not changed")
return nil
}
lst, err := models.UserGetAll(uc.ctx)
if err != nil {
dumper.PutSyncRecord("users", start.Unix(), -1, -1, "failed to query records: "+err.Error())
return errors.WithMessage(err, "failed to exec UserGetAll")
}
m := make(map[int64]*models.User)
for i := 0; i < len(lst); i++ {
m[lst[i].Id] = lst[i]
}
uc.Set(m, stat.Total, stat.LastUpdated, configsStat.Total, configsStat.LastUpdated)
if flashduty.NeedSyncUser(uc.ctx) {
go func() {
err := flashduty.SyncUsersChange(uc.ctx, lst)
if err != nil {
logger.Warning("failed to sync users to flashduty:", err)
dumper.PutSyncRecord("users", start.Unix(), -1, -1, "failed to sync to flashduty: "+err.Error())
}
}()
}
ms := time.Since(start).Milliseconds()
uc.stats.GaugeCronDuration.WithLabelValues("sync_users").Set(float64(ms))
uc.stats.GaugeSyncNumber.WithLabelValues("sync_users").Set(float64(len(m)))
logger.Infof("timer: sync users done, cost: %dms, number: %d", ms, len(m))
dumper.PutSyncRecord("users", start.Unix(), ms, len(m), "success")
return nil
}