Class: Rubord::RateLimiter::Bucket
- Inherits:
-
Object
- Object
- Rubord::RateLimiter::Bucket
- Defined in:
- lib/rubord/structs/rate_limiter.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#last_request_at ⇒ Object
readonly
Returns the value of attribute last_request_at.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#reset_at ⇒ Object
readonly
Returns the value of attribute reset_at.
Instance Method Summary collapse
- #available? ⇒ Boolean
-
#initialize(id) ⇒ Bucket
constructor
A new instance of Bucket.
- #reset_in ⇒ Object
- #update_from_headers(headers) ⇒ Object
- #wait ⇒ Object
Constructor Details
#initialize(id) ⇒ Bucket
Returns a new instance of Bucket.
6 7 8 9 10 11 12 13 14 |
# File 'lib/rubord/structs/rate_limiter.rb', line 6 def initialize(id) @id = id @limit = 1 @remaining = 1 @reset_at = Time.now @queue = [] @last_request_at = nil @mutex = Mutex.new end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def id @id end |
#last_request_at ⇒ Object (readonly)
Returns the value of attribute last_request_at.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def last_request_at @last_request_at end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def limit @limit end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def queue @queue end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def remaining @remaining end |
#reset_at ⇒ Object (readonly)
Returns the value of attribute reset_at.
4 5 6 |
# File 'lib/rubord/structs/rate_limiter.rb', line 4 def reset_at @reset_at end |
Instance Method Details
#available? ⇒ Boolean
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rubord/structs/rate_limiter.rb', line 16 def available? @mutex.synchronize do now = Time.now if now >= @reset_at @remaining = @limit @reset_at = now + 1 end @remaining > 0 end end |
#reset_in ⇒ Object
66 67 68 |
# File 'lib/rubord/structs/rate_limiter.rb', line 66 def reset_in [0, @reset_at - Time.now].max end |
#update_from_headers(headers) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rubord/structs/rate_limiter.rb', line 41 def update_from_headers(headers) @mutex.synchronize do limit_header = headers["x-ratelimit-limit"] remaining_header = headers["x-ratelimit-remaining"] reset_after_header = headers["x-ratelimit-reset-after"] reset_header = headers["x-ratelimit-reset"] limit_value = Array(limit_header).first remaining_value = Array(remaining_header).first reset_after_value = Array(reset_after_header).first reset_value = Array(reset_header).first @limit = limit_value&.to_i || @limit @remaining = remaining_value&.to_i || @remaining if reset_after_value @reset_at = Time.now + reset_after_value.to_f elsif reset_value @reset_at = Time.at(reset_value.to_i) end @last_request_at = Time.now end end |
#wait ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rubord/structs/rate_limiter.rb', line 29 def wait @mutex.synchronize do now = Time.now if now < @reset_at && @remaining <= 0 sleep_time = @reset_at - now sleep(sleep_time) if sleep_time > 0 @remaining = @limit end end end |