All Projects → viafintech → gcra-ruby

viafintech / gcra-ruby

Licence: MIT license
Generic cell rate algorithm (leaky bucket) implementation for rate limiting

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to gcra-ruby

Dalli Rate limiter
Arbitrary Memcached-backed rate limiting for Ruby
Stars: ✭ 38 (-22.45%)
Mutual labels:  ruby-gem, rate-limiting
Filestack Rails
Official Ruby on Rails plugin for Filestack File Picker that makes it easy to add powerful file uploading and transformation capabilities to any web or mobile application.
Stars: ✭ 220 (+348.98%)
Mutual labels:  ruby-gem
Agent orange
Parse and process User Agents like a secret one
Stars: ✭ 127 (+159.18%)
Mutual labels:  ruby-gem
Oscailte
Oscailte — A powerful light, clean, and responsive Jekyll theme.
Stars: ✭ 178 (+263.27%)
Mutual labels:  ruby-gem
Screen Recorder
A Ruby gem to video record and take screenshots of your desktop or specific application window. Works on Windows, Linux, and macOS.
Stars: ✭ 135 (+175.51%)
Mutual labels:  ruby-gem
Discogs
A Ruby wrapper of the Discogs.com API
Stars: ✭ 195 (+297.96%)
Mutual labels:  ruby-gem
Machine Learning With Ruby
Curated list: Resources for machine learning in Ruby
Stars: ✭ 1,693 (+3355.1%)
Mutual labels:  ruby-gem
Api Fuzzer
API Fuzzer which allows to fuzz request attributes using common pentesting techniques and lists vulnerabilities
Stars: ✭ 238 (+385.71%)
Mutual labels:  ruby-gem
Jekyll Minifier
Jekyll HTML/XML/CSS/JS Minifier utilising yui-compressor, and htmlcompressor
Stars: ✭ 215 (+338.78%)
Mutual labels:  ruby-gem
Tty Table
A flexible and intuitive table generator
Stars: ✭ 161 (+228.57%)
Mutual labels:  ruby-gem
Draftsman
Ruby gem that lets you create draft versions of your database records.
Stars: ✭ 159 (+224.49%)
Mutual labels:  ruby-gem
Necromancer
Conversion from one object type to another with a bit of black magic.
Stars: ✭ 136 (+177.55%)
Mutual labels:  ruby-gem
Tty
Toolkit for developing sleek command line apps.
Stars: ✭ 2,329 (+4653.06%)
Mutual labels:  ruby-gem
Pages Gem
A simple Ruby Gem to bootstrap dependencies for setting up and maintaining a local Jekyll environment in sync with GitHub Pages
Stars: ✭ 1,670 (+3308.16%)
Mutual labels:  ruby-gem
Caxlsx
xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine.
Stars: ✭ 221 (+351.02%)
Mutual labels:  ruby-gem
So Simple Theme
A simple Jekyll theme for words and pictures.
Stars: ✭ 1,701 (+3371.43%)
Mutual labels:  ruby-gem
Rorvswild
Ruby on Rails app monitoring: performances & exceptions insights for rails developers.
Stars: ✭ 159 (+224.49%)
Mutual labels:  ruby-gem
No Style Please
A (nearly) no-CSS, fast, minimalist Jekyll theme.
Stars: ✭ 192 (+291.84%)
Mutual labels:  ruby-gem
Dry Schema
Coercion and validation for data structures
Stars: ✭ 249 (+408.16%)
Mutual labels:  ruby-gem
Unimidi
Realtime MIDI IO for Ruby
Stars: ✭ 229 (+367.35%)
Mutual labels:  ruby-gem

GCRA for Ruby

Build Status Code Climate RubyDoc

gcra is a Ruby implementation of a generic cell rate algorithm (GCRA), ported from and data-format compatible with the Go implementation throttled. It's useful for rate limiting (e.g. for HTTP requests) and allows weights specified per request.

Getting Started

gcra currently uses Redis (>= 2.6.12 for EVALSHA, SCRIPT LOAD, SEX with NX and PX) as a data store, although it supports other store implementations.

Add to your Gemfile:

gem 'gcra'
gem 'redis'

Create Redis, RedisStore and RateLimiter instances:

require 'redis'
require 'gcra/rate_limiter'
require 'gcra/redis_store'

redis = Redis.new(host: 'localhost', port: 6379, timeout: 0.1)
key_prefix = 'rate-limit-app1:'
store = GCRA::RedisStore.new(
          redis,
          key_prefix,
          { reconnect_on_readonly: false },
        )

rate_period = 0.5  # Two requests per second
max_burst = 10     # Allow 10 additional requests as a burst
limiter = GCRA::RateLimiter.new(store, rate_period, max_burst)
  • rate_period: Period between two requests, allowed as a sustained rate. Example: 0.1 for 10 requests per second
  • max_burst: Number of requests allowed as a burst in addition to the sustained rate. If the burst is used up, one additional request allowed as burst 'comes back' after each rate_period where no request was made.

Rate limit a request (call this before each request):

key = '123'  # e.g. an account identifier
quantity = 1  # the number of requests 'used up' by this request, useful e.g. for batch requests

exceeded, info = limiter.limit(key, quantity)
# => [false, #<struct GCRA::RateLimitInfo limit=11, remaining=10, reset_after=0.5, retry_after=nil>]
  • exceeded: false means the request should be allowed, true means the request would exceed the limit and should be blocked.
  • info: GCRA::RateLimitInfo contains information that might be useful for your API users. It's a Struct with the following fields:
    • limit: Contains the number of requests that can be made if no previous requests have been made (or they were long enough ago). That's max_burst plus one. The latter is necessary so requests are allowed at all when max_burst is set to zero.
    • remaining: The number of remaining requests that can be made immediately, i.e. the remaining burst.
    • reset_after: The time in seconds until the full burst will be available again.
    • retry_after: Set to nil if a request is allowed or it otherwise doesn't make sense to retry a request (if quantity is larger than max_burst). For a blocked request that can be retried later, set to the duration in seconds until the next request with the given quantity will be allowed.

RateLimiter#limit only tells you whether to limit a request or not. You'll have to react to its response yourself and e.g. return an error message and stop processing a request if the limit was exceeded.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].