policies: shoehorn an iterator in a xsync map Range().

This commit is contained in:
fiatjaf 2024-09-16 15:59:08 -03:00
parent eb92c2aac5
commit 635e91f360

View File

@ -1,6 +1,7 @@
package policies
import (
"iter"
"sync/atomic"
"time"
@ -18,13 +19,12 @@ func startRateLimitSystem[K comparable](
go func() {
for {
time.Sleep(interval)
negativeBuckets.Range(func(key K, bucket *atomic.Int32) bool {
for key, bucket := range iter.Seq2[K, *atomic.Int32](negativeBuckets.Range) {
newv := bucket.Add(int32(-tokensPerInterval))
if newv <= 0 {
negativeBuckets.Delete(key)
}
return true
})
}
}
}()