Class: Fathom::RateLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/fathom/rate_limiter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRateLimiter

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

#limitObject (readonly)

Returns the value of attribute limit.



5
6
7
# File 'lib/fathom/rate_limiter.rb', line 5

def limit
  @limit
end

#remainingObject (readonly)

Returns the value of attribute remaining.



5
6
7
# File 'lib/fathom/rate_limiter.rb', line 5

def remaining
  @remaining
end

#resetObject (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

Returns:

  • (Boolean)


30
# File 'lib/fathom/rate_limiter.rb', line 30

def rate_limited? = @remaining&.zero?

#should_retry?Boolean

Returns:

  • (Boolean)


21
# File 'lib/fathom/rate_limiter.rb', line 21

def should_retry? = Fathom.auto_retry && @remaining&.zero?

#to_hObject



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_timeObject



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