add limiter

This commit is contained in:
2025-04-05 11:53:00 +11:00
parent e01d9805c6
commit 4be44c4eb2
2 changed files with 53 additions and 0 deletions

48
rate_limiter.go Normal file
View File

@@ -0,0 +1,48 @@
package igdb
import (
"sync"
"time"
)
type rateLimiter struct {
mu sync.Mutex
rate int
interval time.Duration
tokens int
lastRefill time.Time
}
func newRateLimiter(rate int) *rateLimiter {
return &rateLimiter{
rate: rate,
interval: time.Second,
tokens: rate,
lastRefill: time.Now(),
}
}
func (r *rateLimiter) wait() {
r.mu.Lock()
defer r.mu.Unlock()
now := time.Now()
elapsed := now.Sub(r.lastRefill)
if elapsed >= r.interval {
r.tokens = r.rate
r.lastRefill = now
}
if r.tokens <= 0 {
waitTime := r.interval - elapsed
r.mu.Unlock()
time.Sleep(waitTime)
r.mu.Lock()
r.tokens = r.rate - 1
r.lastRefill = time.Now()
return
}
r.tokens--
}