SuspendingBucket

SuspendingBucket is an opaque wrapper around Bucket4j's LockFreeBucket, and implements an interface semantically equivalent to BlockingBucket. Whereas Bucket4j's blocking behavior is just that, SuspendingBucket instead delays, making it safe to use in a coroutine context.

Types

Link copied to clipboard
object Companion

Properties

Link copied to clipboard

Returns the number of currently available tokens. Delegates directly to the underlying LockFreeBucket's getAvailableTokens implementation.

Link copied to clipboard
val configuration: BucketConfiguration

Returns the Bucket4j BucketConfiguration of the underlying LockFreeBucket. Delegates directly to the underlying LockFreeBucket's getConfiguration implementation.

Functions

Link copied to clipboard
fun addTokens(tokensToAdd: Long)

Adds the specified number of tokensToAdd to the bucket, not exceeding the capacity of the bucket. Delegates directly to the underlying LockFreeBucket's addTokens implementation.

Link copied to clipboard
suspend fun consume(tokensToConsume: Long)

Consumes the specified number of tokensToConsume, suspending as long as necessary to accumulate the requisite number of tokens. This algorithm is a direct copy of Bucket4j BlockingBucket's consume, except it suspends the calling coroutine's execution while waiting for available tokens, rather than blocking the thread. See the Bucket4j consume documentation for more details.

Link copied to clipboard
fun forceAddTokens(tokensToAdd: Long)

Adds the specified number of tokensToAdd to the bucket, even if it would result in the bucket containing more than its specified capacity. Delegates directly to the underlying LockFreeBucket's forceAddTokens implementation.

Link copied to clipboard
fun replaceConfiguration(newConfiguration: BucketConfiguration, tokensInheritanceStrategy: TokensInheritanceStrategy)

Replaces the bucket's configuration. This delegates directly to the underlying LockFreeBucket's replaceConfiguration implementation.

Link copied to clipboard
fun reset()

Resets all tokens to max capacity. Delegates directly to the underlying LockFreeBucket's reset implementation.

Link copied to clipboard
open override fun toString(): String

Delegates directly to the underlying LockFreeBucket's toString implementation.

Link copied to clipboard
fun tryConsume(tokensToConsume: Long): Boolean

Tries to consume the specified number of tokensToConsume. Delegates directly to the underlying LockFreeBucket's tryConsume implementation.

suspend fun tryConsume(tokensToConsume: Long, maxWaitTime: Duration): Boolean

Tries to consume the specified number of tokensToConsume, suspending up to maxWaitTime if necessary in order to accumulate enough tokens to consume the specified amount.