1
0
mirror of https://github.com/Mrs4s/go-cqhttp.git synced 2025-05-04 19:17:37 +08:00
go-cqhttp/global/ratelimit.go
Ink-33 7b71cd9219
Shorten url
update comment
2021-02-05 02:22:44 +08:00

24 lines
422 B
Go

package global
import (
"context"
"golang.org/x/time/rate"
)
var limiter *rate.Limiter
var 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)
}