Method: Istox::RateLimit#exec_within_threshold

Defined in:
lib/istox/helpers/rate_limit.rb

#exec_within_threshold(subject, options = {}) { ... } ⇒ Object

Execute a block once the rate limit is within bounds WARNING This will block the current thread until the rate limit is within bounds.

Examples:

Send an email as long as we haven’t send 5 in the last 10 minutes

ratelimit.exec_with_threshold(email, [:threshold => 5, :interval => 600]) do
  send_another_email
  ratelimit.add(email)
end

Parameters:

  • subject (String)

    Subject for this rate limit

  • options (Hash) (defaults to: {})

    Options hash

Options Hash (options):

  • :interval (Integer)

    How far back to retrieve activity.

  • :threshold (Integer)

    Maximum number of actions

Yields:

  • The block to be run



130
131
132
133
134
135
# File 'lib/istox/helpers/rate_limit.rb', line 130

def exec_within_threshold(subject, options = {})
  options[:threshold] ||= 30
  options[:interval] ||= 30
  sleep @bucket_interval while exceeded?(subject, options)
  yield(self)
end