Module: Philiprehberger::RateLimiter::StatsTracking
- Included in:
- SlidingWindow, TokenBucket
- Defined in:
- lib/philiprehberger/rate_limiter/stats_tracking.rb
Instance Method Summary collapse
-
#allow!(key, weight: 1) ⇒ true
Like allow? but raises RateLimitExceeded when rejected.
-
#keys ⇒ Array<String>
Return all currently tracked keys.
- #on_reject(&block) ⇒ Object
- #stats(key) ⇒ Object
-
#throttle(key, weight: 1) { ... } ⇒ Hash
Execute a block if allowed, returning the result in a hash.
Instance Method Details
#allow!(key, weight: 1) ⇒ true
Like allow? but raises RateLimitExceeded when rejected.
35 36 37 38 39 |
# File 'lib/philiprehberger/rate_limiter/stats_tracking.rb', line 35 def allow!(key, weight: 1) return true if allow?(key, weight: weight) raise RateLimitExceeded, key end |
#keys ⇒ Array<String>
Return all currently tracked keys.
44 45 46 |
# File 'lib/philiprehberger/rate_limiter/stats_tracking.rb', line 44 def keys @mutex.synchronize { @store.keys } end |
#on_reject(&block) ⇒ Object
10 11 12 13 |
# File 'lib/philiprehberger/rate_limiter/stats_tracking.rb', line 10 def on_reject(&block) @mutex.synchronize { @on_reject_callback = block } self end |
#stats(key) ⇒ Object
6 7 8 |
# File 'lib/philiprehberger/rate_limiter/stats_tracking.rb', line 6 def stats(key) @mutex.synchronize { fetch_stats(key).dup } end |
#throttle(key, weight: 1) { ... } ⇒ Hash
Execute a block if allowed, returning the result in a hash.
21 22 23 24 25 26 27 |
# File 'lib/philiprehberger/rate_limiter/stats_tracking.rb', line 21 def throttle(key, weight: 1, &block) if allow?(key, weight: weight) { allowed: true, value: block.call } else { allowed: false, value: nil } end end |