qbtrade/pkg/util/rate_limit.go

20 lines
400 B
Go
Raw Normal View History

2024-06-27 14:42:38 +00:00
package util
import (
"fmt"
"time"
"golang.org/x/time/rate"
)
func ShouldDelay(l *rate.Limiter, minInterval time.Duration) time.Duration {
return l.Reserve().Delay()
}
func NewValidLimiter(r rate.Limit, b int) (*rate.Limiter, error) {
if b <= 0 || r <= 0 {
return nil, fmt.Errorf("Bad rate limit config. Insufficient tokens. (rate=%f, b=%d)", r, b)
}
return rate.NewLimiter(r, b), nil
}