RateLimiter

class RateLimiter @JvmOverloads constructor(permitsPerSecond: Long, ticker: Ticker, sleeper: Sleeper)

A deterministic testable rate limiter that uses two variables:

  • Permits per second. This is the application's configured rate. We express as a per-second rate but use it as a time-between-permits. For example, 250 permits per second is a permit every 4 milliseconds. This may be zero, in which case all acquire attempts will return false.

  • Window size. If the application specified 250 permits per second, that doesn't specify how many permits can be returned at once. An implementation could strictly return 1 permit every 4 milliseconds, or batches of 1000 permits every 4 seconds. This class hard codes the window size to 1 second. Small windows shrink batch sizes which is inefficient; large windows grow batch sizes which is bursty. This class uses 1 second to balance latency and throughput.

The implementation tracks a future timestamp that permits are consumed until.

This class is similar to Guava's rate limiter. Unlike Guava's rate limiter this class is testable by application code using the rate limiter. It also has very predictable behavior because its internal mechanisms are simpler than Guava's.

Constructors

Link copied to clipboard
fun RateLimiter(permitsPerSecond: Long, ticker: Ticker = Ticker.DEFAULT, sleeper: Sleeper = Sleeper.DEFAULT)

Functions

Link copied to clipboard
fun getPermitsRemaining(unit: TimeUnit, timeout: Long): Long

Returns the maximum number of permits that could have been acquired by a call to tryAcquire, assuming the caller passed the same timeout and unit.

Link copied to clipboard
fun tryAcquire(permitCount: Long, timeout: Long, unit: TimeUnit): Boolean

Attempt to acquire permitCount permits, sleeping up to timeout if necessary for them to become available.

Properties

Link copied to clipboard
var permitsPerSecond: Long