limiter
Version, currently 0.14 versions
- 0.4latestApr 10, 2021
- 0.3not indexedApr 10, 2021
- 0.2not indexedApr 10, 2021
- 0.1not indexedApr 10, 2021
github.com/kostya/limiter
Rate limiter for Crystal. Memory and Redis based.
36 stars
0 dependents
License: MIT
Nothing has been indexed for 0.1 yet. The tag is recorded, its shard.yml has not been read, so the manifest and dependency list below are empty because they are unknown rather than because they are absent.
Installation
# Add this to your shard.yml
dependencies:
limiter:
github: kostya/limiter
version: ~> 0.1Then run:
shards installshard.yml
No shard.yml has been indexed for 0.1. You can read it on the repository.
Dependencies
Unknown: the shard.yml for this version has not been read yet.
README
This README is the one indexed from the repository at its latest ref, not from the tag for this version.
# limiter
Rate limiter for Crystal. Memory and Redis based. Redis limiter is shared (unlike Memory limiter which is local for process), so it can be used across multiple processes.
## Installation
Add this to your application's `shard.yml`:
```yaml
dependencies:
limiter:
github: kostya/limiter
```
## Basic Limiter Usage
```crystal
require "limiter"
limiter = Limiter::Memory.new
limiter.add_limit(2.seconds, 10) # allow 10 requests per 2.seconds
limiter.add_limit(1.hour, 1000) # allow 1000 requests per 1.hour
res = limiter.request? { some_high_cost_action } # => return value of block or nil
res = limiter.request! { some_high_cost_action } # => return value of block or raise Limiter::Error
```
## Example Memory Limiter
```crystal
require "limiter"
limiter = Limiter::Memory.new
limiter.add_limit(2.seconds, 10) # allow 10 requests per 2.seconds
limiter.add_limit(1.hour, 1000) # allow 1000 requests per 1.hour
record Result, val : Float64
def some_high_cost_action : Result
# ...
sleep 0.1
# ...
return Result.new(rand)
end
res = [] of Result
limited_count = 0
1000.times do
if val = limiter.request? { some_high_cost_action }
res << val
else
limited_count += 1
end
end
p res.size
p limited_count
```
## Example Redis Limiter
```crystal
require "redis"
require "limiter"
redis_client = Redis::PooledClient.new
limiter = Limiter::Redis(Redis::PooledClient).new(redis_client, "my_limiter1")
limiter.add_limit(2.seconds, 10) # allow 10 requests per 2.seconds
limiter.add_limit(1.hour, 1000) # allow 1000 requests per 1.hour
record Result, val : Float64
def some_high_cost_action : Result
# ...
sleep 0.1
# ...
return Result.new(rand)
end
res = [] of Result
50.times do
if val = limiter.request? { some_high_cost_action }
res << val
else
x = limiter.next_usage_after
puts "processed: #{res.size}, next usage after #{x} seconds"
sleep(x)
end
end
puts "processed #{res.size}"
```
Documentation
Built from the current release. The first visit to a release nobody has asked for starts its build.
Links
This release
- Version
0.1- Tagged
- Apr 10, 2021
- Commit
a8b272a26da0- Indexed
- not yet
Dependents
No indexed shard depends on this one yet.
Repository
github.com/kostya/limiter
Metadata
- Created
- Aug 12, 2026
- Updated
- Aug 12, 2026
- Synced
- Aug 12, 2026
- Versions
- 4