TokenBucketLimiter

class TokenBucketLimiter(val capacity: Long, val refillTokenInterval: Duration, clock: Clock, stateStorage: StateStorage = MemoryStateStorage()) : RateLimiter

A token bucket rate limiter implementation.

Parameters

capacity

the maximum number of tokens that can be stored in the bucket

refillTokenInterval

the interval at which tokens are added to the bucket

clock

the clock to use to get the current time

stateStorage

the state storage to use to store the bucket state

Constructors

Link copied to clipboard
constructor(capacity: Long, refillTokenInterval: Duration, clock: Clock, stateStorage: StateStorage = MemoryStateStorage())

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard
Link copied to clipboard

Functions

Link copied to clipboard
open suspend override fun awaitUntilTake()

Wait until a token is available.

open suspend override fun awaitUntilTake(key: String)

Wait until a token for the given key is available.

Link copied to clipboard
open suspend override fun tryTake(): Boolean

Try to take a token from the rate limiter.

open suspend override fun tryTake(key: String): Boolean

Try to take a token for the given key from the rate limiter.