Class: Discorb::RatelimitHandler
- Inherits:
-
Object
- Object
- Discorb::RatelimitHandler
- Defined in:
- lib/discorb/rate_limit.rb
Overview
Class to handle rate limiting.
Instance Method Summary collapse
-
#initialize(client) -> RatelimitHandler
constructor
A new instance of RatelimitHandler.
- #save(method, path, resp) -> Object
- #wait(method, path) -> Object
Constructor Details
#initialize(client) -> RatelimitHandler
Returns a new instance of RatelimitHandler.
8 9 10 11 12 |
# File 'lib/discorb/rate_limit.rb', line 8 def initialize(client) @client = client @ratelimit_hash = {} @path_ratelimit_hash = {} end |
Instance Method Details
#save(method, path, resp) -> Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/discorb/rate_limit.rb', line 31 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) -> Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/discorb/rate_limit.rb', line 14 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 |