@@ -27,15 +27,15 @@ type lastLoginUpdater struct {
27
27
group * singleflight.Group
28
28
// execCfg is the executor configuration used for running update operations.
29
29
execCfg * sql.ExecutorConfig
30
- // lastUpdateCache tracks the last time each user's login time was updated
31
- // to avoid redundant database updates.
32
- lastUpdateCache * cache.UnorderedCache
33
30
34
31
mu struct {
35
32
syncutil.Mutex
36
33
// pendingUsers tracks users that need their last login time updated
37
34
// in the next singleflight operation.
38
35
pendingUsers map [username.SQLUsername ]struct {}
36
+ // lastUpdateCache tracks the last time each user's login time was updated
37
+ // to avoid redundant database updates.
38
+ lastUpdateCache * cache.UnorderedCache
39
39
}
40
40
}
41
41
@@ -69,11 +69,11 @@ func newLastLoginUpdater(execCfg *sql.ExecutorConfig) *lastLoginUpdater {
69
69
}
70
70
71
71
u := & lastLoginUpdater {
72
- group : singleflight .NewGroup ("update last login time" , "" ),
73
- execCfg : execCfg ,
74
- lastUpdateCache : cache .NewUnorderedCache (cacheConfig ),
72
+ group : singleflight .NewGroup ("update last login time" , singleflight .NoTags ),
73
+ execCfg : execCfg ,
75
74
}
76
75
u .mu .pendingUsers = make (map [username.SQLUsername ]struct {})
76
+ u .mu .lastUpdateCache = cache .NewUnorderedCache (cacheConfig )
77
77
return u
78
78
}
79
79
@@ -87,13 +87,21 @@ func (u *lastLoginUpdater) updateLastLoginTime(ctx context.Context, dbUser usern
87
87
}
88
88
89
89
// Check if we recently updated this user's login time.
90
- if lastUpdate , ok := u .lastUpdateCache .Get (dbUser ); ok {
91
- if lastUpdateTime , ok := lastUpdate .(time.Time ); ok {
92
- // Only update if it's been more than lastLoginEvictionTime since the last update.
93
- if timeutil .Since (lastUpdateTime ) < lastLoginEvictionTime {
94
- return
90
+ var recentlyUpdated bool
91
+ func () {
92
+ u .mu .Lock ()
93
+ defer u .mu .Unlock ()
94
+ if lastUpdate , ok := u .mu .lastUpdateCache .Get (dbUser ); ok {
95
+ if lastUpdateTime , ok := lastUpdate .(time.Time ); ok {
96
+ // Only update if it's been more than lastLoginEvictionTime since the last update.
97
+ if timeutil .Since (lastUpdateTime ) < lastLoginEvictionTime {
98
+ recentlyUpdated = true
99
+ }
95
100
}
96
101
}
102
+ }()
103
+ if recentlyUpdated {
104
+ return
97
105
}
98
106
99
107
// Use singleflight to ensure at most one last login time update batch
@@ -162,9 +170,13 @@ func (u *lastLoginUpdater) processPendingUpdates(ctx context.Context) error {
162
170
163
171
// Update the cache with the current time for all successfully updated users.
164
172
now := timeutil .Now ()
165
- for _ , user := range users {
166
- u .lastUpdateCache .Add (user , now )
167
- }
173
+ func () {
174
+ u .mu .Lock ()
175
+ defer u .mu .Unlock ()
176
+ for _ , user := range users {
177
+ u .mu .lastUpdateCache .Add (user , now )
178
+ }
179
+ }()
168
180
169
181
return nil
170
182
}
0 commit comments