spikeProtected

fun spikeProtected(capacity: Long, duration: Duration, spikeCapacity: Long, spikeDuration: Duration): BucketFactory(source)
fun spikeProtected(capacity: Long, duration: Duration, spikeCapacity: Long, spikeDuration: Duration): BucketFactory(source)

Creates a BucketFactory with:

When a token is used, both bandwidths need to have a token available.

In other words, the tokens of the greedy bucket will be progressively refilled over the duration, such as the entire bucket would have been refilled when the duration has elapsed. While the intervally refilled bucket will only be refilled once a token has been used, and the entire duration has elapsed.

This avoids users from trying to use all resources in a short time, forcing them to space out their usage.

Example

For a bucket with six tokens and a duration of 1 hour, one token will be added every 10 minutes, but if the spike bucket has 2 tokens and a duration of 10 minutes, then the user will only be able to do 2 requests at once, then wait 10 minutes before doing 2 again, until the 6 tokens / 10 minutes bucket is empty.

Parameters

capacity

The number of tokens for the base bucket

duration

The duration during which the tokens of the base bucket are progressively refilled

spikeCapacity

The number of tokens for the spike bucket, limiting burst usages

spikeDuration

The duration after which the entire spike bucket will be refilled

See also

Throws