0.0
The project is in a healthy, maintained state
Limiter enables building rate limits for your application with ease. It provides a simple interface to define rate limits and track usage.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 5.0
 Project Readme

Limiter

Limiter.dev enables building rate limits for your application with ease. It provides a simple interface to define rate limits and track usage.

Usage

Generate API Token

Before using the gem you must register at https://limiter.dev and generate an token to interact with the api. Thereafter, add the token to app's environment variable

LIMITER_TOKEN: [GENERATED TOKEN]

Install the gem

In the Gemfile add the following

gem "limiter-ruby"

Simple Rate Limit Example

Assuming this is a Ruby on Rails app within ActiveJob

class ExpensiveJob < ApplicationJob

  def perform
    rate_limit = limiter.check(user.id) # A unique identifier

    if rate_limit.exhausted? # Rate limit got hit
      self.class.set(wait: rate_limit.resets_in).perform_later(..args)
      return
    end

    # Good to continue expensive operation
    # ...
  end

  private
  def limiter
    Limiter::Client.new(namespace: "openai", limit: 60, period: 1.minute)
  end
end

Point Based Rate Limit Example

class ExpensiveJob < ApplicationJob

  def perform
    rate_limit = limiter.check(shop.id) # A unique identifier

    if rate_limit.exhausted? # Rate limit got hit
      self.class.set(wait: rate_limit.resets_in).perform_later(..args)
      return
    else
      # Good to continue expensive operation
      # ...
      # Perform query
      limiter.used(250) # Points deducted for last query
    end
  end

  private
  def limiter
    Limiter::Points.new(namespace: "shopify", limit: 1000, period: 1.minute)
  end
end