Skip to content

Commit

Permalink
Make int64 based atomic ratelimiter default (#101)
Browse files Browse the repository at this point in the history
* Fix return timestamp discrepancy between regular atomic limiter and int64 based one
* Make int64 based atomic limiter default

Long story: this was added in #85, but reverted in #91 due to #90. #95 fixed the issue, so we're moving forward with the new implementation.
  • Loading branch information
storozhukBM authored Oct 31, 2022
1 parent b62b799 commit a12885f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 8 additions & 2 deletions limiter_atomic_int64.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ func (t *atomicInt64Limiter) Take() time.Time {
break
}
}
t.clock.Sleep(time.Duration(newTimeOfNextPermissionIssue - now))
return time.Unix(0, newTimeOfNextPermissionIssue)

sleepDuration := time.Duration(newTimeOfNextPermissionIssue - now)
if sleepDuration > 0 {
t.clock.Sleep(sleepDuration)
return time.Unix(0, newTimeOfNextPermissionIssue)
}
// return now if we don't sleep as atomicLimiter does
return time.Unix(0, now)
}
2 changes: 1 addition & 1 deletion ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type config struct {

// New returns a Limiter that will limit to the given RPS.
func New(rate int, opts ...Option) Limiter {
return newAtomicBased(rate, opts...)
return newAtomicInt64Based(rate, opts...)
}

// buildConfig combines defaults with options.
Expand Down

0 comments on commit a12885f

Please sign in to comment.