Module: Philiprehberger::RateLimiter::StatsTracking

Included in:
SlidingWindow, TokenBucket
Defined in:
lib/philiprehberger/rate_limiter/stats_tracking.rb

Instance Method Summary collapse

Instance Method Details

#allow!(key, weight: 1) ⇒ true

Like allow? but raises RateLimitExceeded when rejected.

Parameters:

  • key (Symbol, String)

    the rate limit key

  • weight (Integer) (defaults to: 1)

    tokens to consume

Returns:

  • (true)

Raises:



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

#keysArray<String>

Return all currently tracked keys.

Returns:

  • (Array<String>)


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.

Parameters:

  • key (Symbol, String)

    the rate limit key

  • weight (Integer) (defaults to: 1)

    tokens to consume

Yields:

  • the block to execute when allowed

Returns:

  • (Hash)

    { allowed: true, value: result } or { allowed: false, value: nil }



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