Cleanup.
This commit is contained in:
@@ -53,17 +53,16 @@ func New(conf Config) *Limiter {
|
||||
return lim
|
||||
}
|
||||
|
||||
func (lim *Limiter) getWaitTime(count int64) (time.Duration, error) {
|
||||
func (lim *Limiter) getWaitTime() (time.Duration, error) {
|
||||
lim.lock.Lock()
|
||||
defer lim.lock.Unlock()
|
||||
|
||||
dt := time.Since(lim.lastRequest)
|
||||
|
||||
waitTime := lim.waitTime - dt + time.Duration(count)*lim.fillPeriod
|
||||
waitTime := lim.waitTime - dt
|
||||
waitTime = max(waitTime, lim.minWaitTime) + lim.fillPeriod
|
||||
|
||||
if waitTime < lim.minWaitTime {
|
||||
waitTime = lim.minWaitTime
|
||||
} else if waitTime > lim.maxWaitTime {
|
||||
if waitTime > lim.maxWaitTime {
|
||||
return 0, ErrBackoff
|
||||
}
|
||||
|
||||
@@ -77,14 +76,7 @@ func (lim *Limiter) getWaitTime(count int64) (time.Duration, error) {
|
||||
// maxWaitTime before returning. If the timeout would need to be more than
|
||||
// maxWaitTime to enforce the rate limit, ErrBackoff is returned.
|
||||
func (lim *Limiter) Limit() error {
|
||||
dt, err := lim.getWaitTime(1)
|
||||
dt, err := lim.getWaitTime()
|
||||
time.Sleep(dt) // Will return immediately for dt <= 0.
|
||||
return err
|
||||
}
|
||||
|
||||
// Apply the limiter for multiple items at once.
|
||||
func (lim *Limiter) LimitMultiple(count int64) error {
|
||||
dt, err := lim.getWaitTime(count)
|
||||
time.Sleep(dt)
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user