0.0
No commit activity in last 3 years
No release in over 3 years
Throttle code in your Rails application.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies

Development

~> 1.17
~> 5.0
~> 10.0

Runtime

 Project Readme

RailsThrottle CircleCI Gem Version

RailsThrottle is a simple library used to throttle code in your Rails application.

Installation

Add this line to your application's Gemfile:

gem "rails_throttle", "~> 0.2.0"

And then execute:

$ bundle

Or install it yourself as:

$ gem install rails_throttle

Usage

  • See full documentation here: https://www.rubydoc.info/gems/rails_throttle/0.4.0/RailsThrottle/Throttle
  • Example configuration (for production, you can configure other environments in a similar way):
    # In config/environments/production.rb
    RailsThrottle.backend = ActiveSupport::Cache::RedisStore.new "localhost:6379/0"
    
    # Alternatively, also supports any of the other caching strategies of ActiveSupport::Cache.
    # For details, see https://api.rubyonrails.org/classes/ActiveSupport/Cache.html.
  • Example usage:
    RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 1
    RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => 2
    RailsThrottle::Throttle.decrement("foo", period: 2.seconds) # => 1
    RailsThrottle::Throttle.reset("foo")
    RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds, increment: 4) # => 4
    RailsThrottle::Throttle.increment("foo", limit: 4, period: 2.seconds) # => RailsThrottle::ThrottleError
    RailsThrottle::Throttle.throttled?("foo", limit: 5) # => true
    
    # ... wait 2 seconds ...
    
    RailsThrottle::Throttle.throttled?("foo", limit: 5) # => false

Development

Just the usual clone and bundle to install dependencies. Run rake test to run tests.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/peterzhu2118/rails_throttle.

License

The gem is available as open source under the terms of the MIT License.