Class: Prefab::RateLimitClient
- Inherits:
-
Object
- Object
- Prefab::RateLimitClient
- Defined in:
- lib/prefab/ratelimit_client.rb
Instance Method Summary collapse
- #acquire(groups, acquire_amount, allow_partial_response: false, on_error: :log_and_pass) ⇒ Object
-
#initialize(ratelimit_service, client) ⇒ RateLimitClient
constructor
A new instance of RateLimitClient.
- #pass?(group) ⇒ Boolean
Constructor Details
#initialize(ratelimit_service, client) ⇒ RateLimitClient
Returns a new instance of RateLimitClient.
3 4 5 6 |
# File 'lib/prefab/ratelimit_client.rb', line 3 def initialize(ratelimit_service, client) @ratelimit_service = ratelimit_service @client = client end |
Instance Method Details
#acquire(groups, acquire_amount, allow_partial_response: false, on_error: :log_and_pass) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/prefab/ratelimit_client.rb', line 13 def acquire(groups, acquire_amount, allow_partial_response: false, on_error: :log_and_pass) expiry_cache_key = "prefab.ratelimit.expiry:#{groups.join(".")}" expiry = @client.shared_cache.read(expiry_cache_key) if !expiry.nil? && Integer(expiry) > Time.now.utc.to_f * 1000 @client.stats.increment("prefab.ratelimit.limitcheck.expirycache.hit", tags: []) return It::Ratelim::Data::LimitResponse.new(passed: false, amount: 0) end req = It::Ratelim::Data::LimitRequest.new( account_id: @client.account_id, acquire_amount: acquire_amount, groups: groups, allow_partial_response: allow_partial_response ) result = @ratelimit_service.limit_check(req) reset = result.limit_reset_at @client.shared_cache.write(expiry_cache_key, reset) unless reset < 1 @client.stats.increment("prefab.ratelimit.limitcheck", tags: ["policy_group:#{result.policy_group}", "pass:#{result.passed}"]) result rescue => e handle_error(e, on_error) end |
#pass?(group) ⇒ Boolean
8 9 10 11 |
# File 'lib/prefab/ratelimit_client.rb', line 8 def pass?(group) result = acquire([group], 1) return result.passed end |