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 package policies
import ( import (
"iter"
"sync/atomic" "sync/atomic"
"time" "time"
@ -18,13 +19,12 @@ func startRateLimitSystem[K comparable](
go func() { go func() {
for { for {
time.Sleep(interval) 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)) newv := bucket.Add(int32(-tokensPerInterval))
if newv <= 0 { if newv <= 0 {
negativeBuckets.Delete(key) negativeBuckets.Delete(key)
} }
return true }
})
} }
}() }()