Class: Fathom::RateLimiter
- Inherits:
-
Object
- Object
- Fathom::RateLimiter
- Defined in:
- lib/fathom/rate_limiter.rb
Instance Attribute Summary collapse
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#remaining ⇒ Object
readonly
Returns the value of attribute remaining.
-
#reset ⇒ Object
readonly
Returns the value of attribute reset.
Instance Method Summary collapse
-
#initialize ⇒ RateLimiter
constructor
A new instance of RateLimiter.
- #rate_limited? ⇒ Boolean
- #should_retry? ⇒ Boolean
- #to_h ⇒ Object
- #update_from_headers(headers) ⇒ Object
- #wait_time ⇒ Object
Constructor Details
#initialize ⇒ RateLimiter
Returns a new instance of RateLimiter.
7 8 9 10 11 |
# File 'lib/fathom/rate_limiter.rb', line 7 def initialize @limit = nil @remaining = nil @reset = nil end |
Instance Attribute Details
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
5 6 7 |
# File 'lib/fathom/rate_limiter.rb', line 5 def limit @limit end |
#remaining ⇒ Object (readonly)
Returns the value of attribute remaining.
5 6 7 |
# File 'lib/fathom/rate_limiter.rb', line 5 def remaining @remaining end |
#reset ⇒ Object (readonly)
Returns the value of attribute reset.
5 6 7 |
# File 'lib/fathom/rate_limiter.rb', line 5 def reset @reset end |
Instance Method Details
#rate_limited? ⇒ Boolean
30 |
# File 'lib/fathom/rate_limiter.rb', line 30 def rate_limited? = @remaining&.zero? |
#should_retry? ⇒ Boolean
21 |
# File 'lib/fathom/rate_limiter.rb', line 21 def should_retry? = Fathom.auto_retry && @remaining&.zero? |
#to_h ⇒ Object
32 |
# File 'lib/fathom/rate_limiter.rb', line 32 def to_h = { limit: @limit, remaining: @remaining, reset: @reset } |
#update_from_headers(headers) ⇒ Object
13 14 15 16 17 18 19 |
# File 'lib/fathom/rate_limiter.rb', line 13 def update_from_headers(headers) @limit = extract_header_value(headers, "RateLimit-Limit")&.to_i @remaining = extract_header_value(headers, "RateLimit-Remaining")&.to_i @reset = extract_header_value(headers, "RateLimit-Reset")&.to_i Fathom.log("Rate limit: #{@remaining}/#{@limit}, resets in #{@reset}s") end |
#wait_time ⇒ Object
23 24 25 26 27 28 |
# File 'lib/fathom/rate_limiter.rb', line 23 def wait_time return 0 unless @reset # Add a small buffer @reset + 1 end |