GithubHelp home page GithubHelp logo

gsto / active_record_rate_limiter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ardell/active_record_rate_limiter

0.0 1.0 0.0 28 KB

A simple rate limiter backed by ActiveRecord.

License: MIT License

Ruby 100.00%

active_record_rate_limiter's Introduction

active_record_rate_limiter

Installation

  1. Add gem 'active_record_rate_limiter', git: 'git://github.com/ardell/active_record_rate_limiter.git' to your Gemfile
  2. Run bundle install
  3. Run rails g active_record_rate_limiter:install
  4. Run rake db:migrate

Add a Limiter

A simple rate limiter:

# Limits actions to 10 per second.
class TenTimesPerSecondLimiter < ActiveRecordRateLimiter::Limiter
  event_type 'TenTimesPerSecond'
  limit 10, since: proc { 1.second.ago }
end

A more complex rate limiter with multiple rules for a single event type:

# Limits actions to 10 per second AND 100 per day. E.g. if there have already
# been 100 actions in the past 24 hours, `limited?` will return true no matter
# how few actions have occurred in the past second.
class ComplexLimiter < ActiveRecordRateLimiter::Limiter
  event_type 'Complex'
  limit 10,  since: proc { 1.second.ago }
  limit 100, since: proc { 1.day.ago }
end

A custom handler when the limit is hit (default is to sleep for 0.1 seconds)...

class ComplexLimiter < ActiveRecordRateLimiter::Limiter
  # Custom handler for on_limit
  # NOTEs:
  # - it's a class method
  # - it's defined before calling `limit`
  def self.sleep_longer
    sleep(10)
  end

  event_type 'CustomHandler'
  limit 1, since: proc { 10.seconds.ago }, on_limit: :sleep_longer
end

Record an Event

TenTimesPerSecondLimiter.track

Check Rate Limit

TenTimesPerSecondRateLimiter.limited?

Get Events

To get a raw relation of all the events as an ActiveRecord relation:

TenTimesPerSecondRateLimiter.events

To simply get the total number of events matching the given event type:

TenTimesPerSecondRateLimiter.events.count

active_record_rate_limiter's People

Contributors

ardell avatar gsto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.