ratelimit

Version, currently master branch1 version
  • master branchlatestOct 31, 2015

github.com/ukd1/crystal-ratelimit

A rate limiting class for Crystal

7 stars
0 dependents
License: MIT

Installation

# Add this to your shard.yml
dependencies:
  ratelimit:
    github: ukd1/crystal-ratelimit
    branch: master

master is a branch, not a release, so this tracks it rather than pinning a version.

Then run:

shards install

shard.yml

Crystal
no constraint declared
License
MIT
Author
Russell Smith <russ@rainforestqa.com>

Dependencies

Runtime Dependencies

  • redis~> 1.2.0github: stefanwille/crystal-redis

README

# ratelimit

[![Build Status](https://travis-ci.org/ukd1/crystal-ratelimit.svg)](https://travis-ci.org/ukd1/crystal-ratelimit)

Simple rate limiting class, backed by Redis sorted sets that use a minimal amounts of keys.

## Installation

Add this to your application's `shard.yml`:

```yaml
dependencies:
  ratelimit:
    github: ukd1/ratelimit
```

## Usage

```crystal
require "redis"
require "ratelimit"

redis = Redis.new()

# make a new rate limit instace, passing in a redis connection
r = Ratelimit.new(redis)

# limit request over this interval, in milliseconds
interval = 100

# allow max of this many requests in the interval
limit = 10

# loop until we go over the limit
until r.over_limit?("test", limit, interval)
  puts "Doing request"
  r.add("test", interval)
end

puts "Hit limit : #{r.used("test")}"
```

## Development

You'll need a working Redis database.

## Contributing

1. Fork it ( https://github.com/[your-github-name]/ratelimit/fork )
2. Create your feature branch (git checkout -b my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin my-new-feature)
5. Create a new Pull Request

## Contributors

- [[ukd1]](https://github.com/ukd1) Russell Smith - creator, maintainer