Threshold

A gem to control external API calls. Many API server has rate limit.e.g. Recurly: 1k requests/1minute, Shopify: 4 requests/second. To resolve external API call issue, I create this gem.

Ideas are come from rack-attach, redlock. This gem add extension to Net::HTTPRequest. Before you made request, check redis if out request reach the limit. If reach the limit, sleep and wait rack-attack expire the out rate limit count.

How to use

gem install threshold


# currently, throttle is based on host + port
HttpThreshold.redis_url = "redis://localhost:6379/0"
HttpThreshold::Client.set_throttle("your-store.shopify.com", limit: 4, period: 1.second)
HttpThreshold::Client.set_throttle("localhost:3000", limit: 50, period: 1.minute)
HttpThreshold::Client.set_throttle("google.com", limit: 5, period: 10.second)

Then when you run any http call like below

require 'httparty'
HTTParty.get("https://www.google.com")#ok
HTTParty.get("https://www.google.com")#ok
HTTParty.get("https://www.google.com")#ok
HTTParty.get("https://www.google.com")#ok
HTTParty.get("https://www.google.com")#ok
HTTParty.get("https://www.google.com")#will sleep until this 10 seconds past, and then get response

It support distributed system, because we use redis lock.

Note

The best way to use this, is add threshold at background job, set a more strict limit in a period. So that it could leave enough rate resources for website.

Background job sleep is OK. A user's request sleep long would cause problem.

Todo

Add test