Class: SecApi::RateLimitTracker
- Inherits:
-
Object
- Object
- SecApi::RateLimitTracker
- Defined in:
- lib/sec_api/rate_limit_tracker.rb
Overview
Thread-safe manager for rate limit state and queue tracking.
This class provides thread-safe storage and access to rate limit information using a Mutex for synchronization. Each Client instance owns its own tracker, ensuring rate limit state is isolated per-client.
The tracker receives updates from the RateLimiter middleware and provides read access to the current state via the Client#rate_limit_state method. It also tracks the number of queued requests waiting for rate limit reset.
Instance Method Summary collapse
-
#current_state ⇒ RateLimitState?
Returns the current rate limit state.
-
#decrement_queued ⇒ Integer
Decrements the queued request counter.
-
#increment_queued ⇒ Integer
Increments the queued request counter.
-
#initialize ⇒ RateLimitTracker
constructor
Creates a new RateLimitTracker instance.
-
#queued_count ⇒ Integer
Returns the current count of queued requests.
-
#reset! ⇒ void
Clears the current rate limit state.
-
#update(limit:, remaining:, reset_at:) ⇒ RateLimitState
Updates the rate limit state with new values.
Constructor Details
#initialize ⇒ RateLimitTracker
Creates a new RateLimitTracker instance.
52 53 54 55 56 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 52 def initialize @mutex = Mutex.new @state = nil @queued_count = 0 end |
Instance Method Details
#current_state ⇒ RateLimitState?
Returns the current rate limit state.
Returns nil if no rate limit information has been received yet. The returned RateLimitState is immutable and can be safely used outside the mutex lock.
95 96 97 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 95 def current_state @mutex.synchronize { @state } end |
#decrement_queued ⇒ Integer
Decrements the queued request counter.
Called by the RateLimiter middleware when a request exits the queue.
148 149 150 151 152 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 148 def decrement_queued @mutex.synchronize do @queued_count = [@queued_count - 1, 0].max end end |
#increment_queued ⇒ Integer
Increments the queued request counter.
Called by the RateLimiter middleware when a request enters the queue.
136 137 138 139 140 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 136 def increment_queued @mutex.synchronize do @queued_count += 1 end end |
#queued_count ⇒ Integer
Returns the current count of queued requests.
When the rate limit is exhausted (remaining = 0), requests are queued until the rate limit resets. This method returns the current count of waiting requests.
126 127 128 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 126 def queued_count @mutex.synchronize { @queued_count } end |
#reset! ⇒ void
This method returns an undefined value.
Clears the current rate limit state.
After calling reset!, current_state will return nil until new rate limit headers are received.
111 112 113 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 111 def reset! @mutex.synchronize { @state = nil } end |
#update(limit:, remaining:, reset_at:) ⇒ RateLimitState
Updates the rate limit state with new values.
Creates a new immutable RateLimitState object with the provided values. This method is thread-safe and can be called concurrently.
71 72 73 74 75 76 77 78 79 |
# File 'lib/sec_api/rate_limit_tracker.rb', line 71 def update(limit:, remaining:, reset_at:) @mutex.synchronize do @state = RateLimitState.new( limit: limit, remaining: remaining, reset_at: reset_at ) end end |