func newRatioController(tm timetools.TimeProvider, rampUp time.Duration) *ratioController { return &ratioController{ duration: rampUp, tm: tm, start: tm.UtcNow(), } }
// newTokenBucket crates a `tokenBucket` instance for the specified `Rate`. func newTokenBucket(rate *rate, clock timetools.TimeProvider) *tokenBucket { return &tokenBucket{ period: rate.period, timePerToken: time.Duration(int64(rate.period) / rate.average), burst: rate.burst, clock: clock, lastRefresh: clock.UtcNow(), availableTokens: rate.burst, } }
func NewTokenBucket(rate Rate, maxTokens int64, timeProvider timetools.TimeProvider) (*TokenBucket, error) { if rate.Period == 0 || rate.Units == 0 { return nil, fmt.Errorf("Invalid rate: %v", rate) } if maxTokens <= 0 { return nil, fmt.Errorf("Invalid maxTokens, should be >0: %d", maxTokens) } if timeProvider == nil { return nil, fmt.Errorf("Supply time provider") } return &TokenBucket{ refillPeriod: time.Duration(int64(rate.Period) / rate.Units), maxTokens: maxTokens, // in case of maxBurst is 0, maxTokens available at should be 1 timeProvider: timeProvider, lastRefill: timeProvider.UtcNow(), tokens: maxTokens, }, nil }