Class: RubyCord::Client::HTTP::RateLimitHandler
- Inherits:
-
Object
- Object
- RubyCord::Client::HTTP::RateLimitHandler
- Defined in:
- lib/rubycord/client/http/rate_limit_handler.rb
Instance Method Summary collapse
- #inspect ⇒ Object
-
#save(path, resp) ⇒ Object
Save the rate limit.
-
#wait(path) ⇒ Object
Wait for the rate limit to reset.
Instance Method Details
#inspect ⇒ Object
25 26 27 |
# File 'lib/rubycord/client/http/rate_limit_handler.rb', line 25 def inspect "#<#{self.class}>" end |
#save(path, resp) ⇒ Object
Save the rate limit.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rubycord/client/http/rate_limit_handler.rb', line 69 def save(path, resp) if resp["X-Ratelimit-Global"] == "true" @global = Time.now.to_f + JSON.parse(resp.body, symbolize_names: true)[:retry_after] end return unless resp["X-RateLimit-Remaining"] @path_ratelimit_hash[path.identifier] = resp["X-Ratelimit-Bucket"] @path_ratelimit_bucket[resp["X-Ratelimit-Bucket"] + path.major_param] = { remaining: resp["X-RateLimit-Remaining"].to_i, reset_at: Time.now.to_f + resp["X-RateLimit-Reset-After"].to_f } end |
#wait(path) ⇒ Object
Wait for the rate limit to reset.
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rubycord/client/http/rate_limit_handler.rb', line 34 def wait(path) # return if path.url.start_with?("https://") if @global && @global > Time.now.to_f time = @global - Time.now.to_f @client.logger.info( "global rate limit reached, waiting #{time} seconds" ) sleep(time) @global = false end return unless hash = @path_ratelimit_hash[path.identifier] return unless bucket = @path_ratelimit_bucket[hash + path.major_param] if bucket[:reset_at] < Time.now.to_f @path_ratelimit_bucket.delete(path.identifier + path.major_param) return end return if (bucket[:remaining]).positive? time = bucket[:reset_at] - Time.now.to_f @client.logger.info( "rate limit for #{path.identifier} with #{path.major_param} reached, " \ "waiting #{time.round(4)} seconds" ) sleep(time) end |