139
// 2. For each candidate: Lock, re-check, delete, Unlock
140
// This ensures read operations are only briefly blocked during actual deletions.
142
>
nowNano := now.UnixNano()
143
>
ttlNano := r.ttlNano
144
>
145
>
// Phase 1: collect expired keys under read lock
146
>
var expiredKeys []K
147
>
r.RLock()
148
>
for k, e := range r.rateLimiters {
149
>
if nowNano-e.lastAccess.Load() > ttlNano {
150
>
expiredKeys = append(expiredKeys, k)
151
>
}
152
}
154
>
155
>
// Phase 2: delete each expired key individually
156
>
for _, k := range expiredKeys {
157
>
r.Lock()
158
>
if e, ok := r.rateLimiters[k]; ok && nowNano-e.lastAccess.Load() > ttlNano {
159
>
delete(r.rateLimiters, k)
160
>
}
161
>
r.Unlock()
162
}
163
}