1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-05 11:33:48 +08:00
go-cqhttp/global/ratelimit.go
2021-04-04 14:16:49 +00:00

26 lines
428 B
Go

package global
import (
"context"
"golang.org/x/time/rate"
)
var (
limiter *rate.Limiter
limitEnable = false
)
// RateLimit 执行API调用速率限制
func RateLimit(ctx context.Context) {
if limitEnable {
_ = limiter.Wait(ctx)
}
}
// InitLimiter 初始化速率限制器
func InitLimiter(frequency float64, bucketSize int) {
limitEnable = true
limiter = rate.NewLimiter(rate.Limit(frequency), bucketSize)
}