class Discorb::RatelimitHandler

Class to handle rate limiting. @private

Public Class Methods

new(client) click to toggle source

@!visibility private

# File lib/discorb/rate_limit.rb, line 10
def initialize(client)
  @client = client
  @ratelimit_hash = {}
  @path_ratelimit_hash = {}
end

Public Instance Methods

save(method, path, resp) click to toggle source

Save the rate limit.

@param [String] method The HTTP method. @param [String] path The path. @param [Net::HTTPResponse] resp The response.

# File lib/discorb/rate_limit.rb, line 46
def save(method, path, resp)
  return unless resp["X-RateLimit-Remaining"]

  @path_ratelimit_hash[method + path] = resp["X-RateLimit-Bucket"]
  @ratelimit_hash[resp["X-RateLimit-Bucket"]] = {
    remaining: resp["X-RateLimit-Remaining"].to_i,
    reset_at: resp["X-RateLimit-Reset"].to_i,
  }
end
wait(method, path) click to toggle source

Wait for the rate limit to reset.

@param [String] method The HTTP method. @param [String] path The path.

# File lib/discorb/rate_limit.rb, line 22
def wait(method, path)
  return if path.start_with?("https://")

  return unless hash = @path_ratelimit_hash[method + path]

  return unless b = @ratelimit_hash[hash]

  if b[:reset_at] < Time.now.to_i
    @ratelimit_hash.delete(hash)
    return
  end
  return if b[:remaining] > 0

  @client.log.info("Ratelimit reached, waiting for #{b[:reset_at] - Time.now.to_i} seconds")
  sleep(b[:reset_at] - Time.now.to_i)
end