This commit is contained in:
WJQSERVER 2024-11-01 03:58:47 +08:00
parent 2a5570a447
commit 6f67f6f5b4
9 changed files with 72 additions and 5 deletions

21
rate/rate.go Normal file
View file

@ -0,0 +1,21 @@
package rate
import (
"time"
"golang.org/x/time/rate"
)
type RateLimiter struct {
limiter *rate.Limiter
}
func New(limit int, burst int, duration time.Duration) *RateLimiter {
return &RateLimiter{
limiter: rate.NewLimiter(rate.Limit(float64(limit)/duration.Seconds()), burst),
}
}
func (rl *RateLimiter) Allow() bool {
return rl.limiter.Allow()
}