github.com/BlossomiShymae/cute-rate-limit
main / published Apr 23, 2023 / repository
A cutesy rate limiter thats uses token bucket algorithm and blocking strategy.
cute-rate-limit
A cute rate limiter in Crystal that implements token bucket algorithm. Uses
sleep blocking strategy when limit is exceeded.
Installation
-
Add the dependency to your
shard.yml:dependencies: cute-rate-limit: github: BlossomiShymae/cute-rate-limit -
Run
shards install
Usage
require "cute-rate-limit"
A limiter uses a token bucket based algorithm that defaults to blocking
strategy sleep when limit is exceeded.
In an example web API context, the limit is 20 requests every 1 second. It would be very, very bad if we ignore this limit. :c
Rate limiters solve this problem. Using a limiter with a full bucket, the
maximum burst is 20 requests before limiter blocks with sleep. Tokens are
allocated every 1 / token_rate period, where token_rate = bucket_size / bucket_period
per second.
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 # => 19
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 a limiter with #token_bucket
cute_limiter2.token_bucket # => 100
50.times do
cute_limiter2.use
end
cute_limiter2.token_bucket # => 50
Contributing
- Fork it (https://github.com/BlossomiShymae/cute-rate-limit/fork)
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request
Contributors
- BlossomiShymae - creator and maintainer
API
- CuteRateLimit
CuteRateLimitmodule that contains rate limiting tools such asLimiter.