{"id":22427207,"url":"https://github.com/shopify/limiter","last_synced_at":"2025-05-15T12:05:29.606Z","repository":{"id":41220787,"uuid":"139990301","full_name":"Shopify/limiter","owner":"Shopify","description":"Simple Ruby rate limiting mechanism.","archived":false,"fork":false,"pushed_at":"2024-06-06T17:33:56.000Z","size":93,"stargazers_count":416,"open_issues_count":1,"forks_count":22,"subscribers_count":383,"default_branch":"main","last_synced_at":"2025-05-11T09:16:48.103Z","etag":null,"topics":["gem","rate-limiting","ruby"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Shopify.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-07-06T13:43:20.000Z","updated_at":"2025-05-05T04:49:43.000Z","dependencies_parsed_at":"2022-07-12T18:17:50.370Z","dependency_job_id":"7871e4aa-cfd3-4f52-ba10-88dd4106b879","html_url":"https://github.com/Shopify/limiter","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Flimiter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Flimiter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Flimiter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Shopify%2Flimiter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Shopify","download_url":"https://codeload.github.com/Shopify/limiter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254337613,"owners_count":22054253,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gem","rate-limiting","ruby"],"created_at":"2024-12-05T20:10:30.698Z","updated_at":"2025-05-15T12:05:24.588Z","avatar_url":"https://github.com/Shopify.png","language":"Ruby","readme":"# Limiter [![Build Status](https://github.com/Shopify/limiter/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/Shopify/limiter/actions/workflows/ci.yml)\n\nThis gem implements a simple mechanism to throttle or rate-limit operations in Ruby.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'ruby-limiter'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install ruby-limiter\n\n## Usage\n\n### Basic Usage\n\nTo rate limit calling an instance method, a mixin is provided. Simply specify the method to be limited, and the maximum\nrate that the method can be called. This rate is (by default) a number of requests per minute.\n\n``` ruby\nclass Widget\n  extend Limiter::Mixin\n\n  # limit the rate we can call tick to 300 times per minute\n  # when the rate has been exceeded, a call to tick will block until the rate limit would not be exceeded\n  limit_method :tick, rate: 300\n\n  ...\nend\n```\n\nTo specify the rate in terms of an interval shorter (or longer) than 1 minute, an optional `interval` parameter can be\nprovided to specify the throttling period in seconds.\n\n``` ruby\nclass Widget\n  extend Limiter::Mixin\n\n  # limit the rate we can call tick to 5 times per second\n  # when the rate has been exceeded, a call to tick will block until the rate limit would not be exceeded\n  # and the provided block will be executed\n  limit_method(:tick, rate: 5, interval: 1) do\n    puts 'Limit reached'\n  end\n\n  ...\nend\n```\n\n#### Load balancing\n\nBy default all calls to the `limit_method` will be bursted, e.g. as quick as possible, until the rate is exceeded. Then\nwe wait for the remainder of the interval to continue. To even out the burst, an optional `balanced` parameter can be\nprovided to enable interleaving between the method calls, e.g: `interleave = interval / size`.\n\n``` ruby\n  ...\n  limit_method :tick, rate: 60, balanced: true\n  ...\n```\n\nFor example: with an interval of 60 seconds and a rate of 60:\n\n`balanced: false`\n: As quickly as possible we call the method 60 times, then we wait for the remainder of the time.\n\n`balanced: true`\n: We interleave each call with 1 second so we call this method every second.\n\n#### Resetting a rate-limited method\n\nThere are times when it may be necessary to reset the rate limiter for a method, for example during testing.\n\nThis can be done by calling `reset_method_limit!` on the class, where \"method\" is replaced with the name of the method\nbeing limited.\n\nGiven the example above, the following would reset the rate limit for the `tick` method during test setup:\n\n``` ruby\nclass WidgetTest \u003c Minitest::Test\n  def setup\n    Widget.reset_tick_limit!\n  end\n\n  ...\nend\n```\n\n### Advanced Usage\n\nIn cases where the mixin is not appropriate the `RateQueue` class can be used directly. As in the mixin examples above,\nthe `interval` parameter is optional (and defaults to 1 minute). It is also possible to provide the block to\n`RateQueue`, which will be executed on each limit hit (useful for metrics).\n\n``` ruby\nclass Widget\n  def initialize\n    # create a rate-limited queue which allows 10000 operations per hour\n    @queue = Limiter::RateQueue.new(10000, interval: 3600) do\n      puts \"Hit the limit, waiting\"\n    end\n  end\n\n  def tick\n    # this operation will block until less than 10000 shift calls have been made within the last hour\n    @queue.shift\n    # do something\n  end\nend\n```\n\n#### Resetting a RateQueue\n\nIn some circumstances it may be desirable to reset a rate queue, for example after invoking an API that resets an\nexternal rate limit.\n\nThis can be done by calling `reset` on the queue.\n\n``` ruby\n  ...\n  @queue.reset\n  ...\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can\nalso run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the\nversion number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,\npush git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/Shopify/limiter.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopify%2Flimiter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshopify%2Flimiter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshopify%2Flimiter/lists"}