Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wizardone/raterr
Rate limiting for Ruby applications
https://github.com/wizardone/raterr
cache hash limiter period rails-cache rate rate-limiter ruby visitor
Last synced: 21 days ago
JSON representation
Rate limiting for Ruby applications
- Host: GitHub
- URL: https://github.com/wizardone/raterr
- Owner: wizardone
- License: mit
- Created: 2017-09-29T05:57:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-12-14T02:51:23.000Z (about 3 years ago)
- Last Synced: 2024-04-25T04:44:17.674Z (9 months ago)
- Topics: cache, hash, limiter, period, rails-cache, rate, rate-limiter, ruby, visitor
- Language: Ruby
- Homepage:
- Size: 33.2 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Raterr
[![Build Status](https://travis-ci.org/wizardone/raterr.svg?branch=master)](https://travis-ci.org/wizardone/raterr)
[![codecov](https://codecov.io/gh/wizardone/raterr/branch/master/graph/badge.svg)](https://codecov.io/gh/wizardone/raterr)`Raterr` allows you to enforce rate limiting restrictions based on ip
address.## Installation
Add this line to your application's Gemfile:
```ruby
gem 'raterr'
```And then execute:
$ bundle
Or install it yourself as:
$ gem install raterr
## Usage
For Ruby based application you need to tell `Raterr` what store to use.
Currently it supports a simple hash, or `Redis`
It is best to load the store in an initializer of any other kind of file
that is loaded initially.
```ruby
# Use either
Raterr.store = Hash.new
# Or
Raterr.store = Redis.new
```
To enforce rate limiting use:
```ruby
Raterr.enforce(request, period: :minute, max: 200, code: 429)
```
`request` might be any data type that has an `ip` method. For Rails
applications that would be the request method.The result of `Raterr.enforce` is always a pseudo status. In case the
rate limit has not been reached you will get a pseudo `200` status + the
number of attempts made. This allows you to do additional checks (say a
warning if you are about to reach the threshhold). If
it has been reached you will get whatever status you configured, or the
default one, which is 429 + a text message.An example usage in a Rails application would be:
```ruby
class MyController < Base::ApplicationController
before_action :rate_limit, only: :indexdef index
# Do something cool
endprivate
def rate_limit
result = Raterr.enforce(request, period: :minute, max: 10)
if result[:status] == 429
# Do whatever you want to do when the rate limit is reached
render plain: result[:text], status: result[:status] and return
else
puts "Number of attempts: #{result[:attempts]}"
end
end
end
```
If you want to add limiting to the whole app you would put it in an
application controller and so on...You can configure the period error code and the max attempts. The allowed periods
are: `:minute, :hour, :day`.
The default error code is `429`.
The default max attempts is `100`.## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/wizardone/raterr. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).