bbgo_origin/pkg/util/rate_limit.go

20 lines
400 B
Go
Raw Permalink Normal View History

2020-06-08 02:42:50 +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
}