class

CuteRateLimit::Limiter

Inherits Reference < Object

A limiter uses a token bucket based algorithm that defaults to blocking strategy sleep when limit is exceeded.

To create a limiter with a bucket_size of 20 tokens and a bucket_period of 1000 milliseconds:

cute_limiter = CuteRateLimit::Limiter.new 20 1*1000

For verbose readability:

cute_limiter = CuteRateLimit::Limiter.new 20 1.second.total_milliseconds.to_i

To use a limiter:

cute_limiter.use

Multiple limiters can be used together in tandem:

cute_limiter1 = CuteRateLimit::Limiter.new 20 1*1000
cute_limiter2 = CuteRateLimit::Limiter.new 100 120*1000

cute_limiter1.use
cute_limiter2.use

Check the bucket of limiter with #token_bucket

Constructors

new(bucket_size : Int64, bucket_period : Int64)

Creates a limiter with bucket_size and bucket_period.

NOTE: The units of milliseconds are used for bucket_period when creating a limiter.

Source

Instance methods

to_s(io : IO)

Prints the current instance information of Limiter

cute_limiter.to_s # => <Limiter 0 of @bucket_size=20, @bucket_period=1000, @token_bucket=20>
Source
token_bucket

Returns the number of tokens in bucket this limiter has

cute_limiter.token_bucket # => *token_bucket*
Source
use

Calls #fill_token_bucket and tries to take away a token. #use will sleep until token_bucket is allocated a token to take from.

Returns the number of tokens in bucket this limiter has

cute_limiter.use # => *token_bucket*
Source