Class: AgentHarness::Orchestration::RateLimiter
- Inherits:
-
Object
- Object
- AgentHarness::Orchestration::RateLimiter
- Defined in:
- lib/agent_harness/orchestration/rate_limiter.rb
Overview
Rate limiter for tracking and managing provider rate limits
Tracks rate limit events and provides information about when providers are expected to be available again.
Instance Attribute Summary collapse
-
#limit_count ⇒ Object
readonly
Returns the value of attribute limit_count.
-
#limited_until ⇒ Object
readonly
Returns the value of attribute limited_until.
Instance Method Summary collapse
-
#clear_limit ⇒ void
Clear rate limit status.
-
#initialize(config = nil, default_reset_time: nil) ⇒ RateLimiter
constructor
Create a new rate limiter.
-
#limited? ⇒ Boolean
Check if currently rate limited.
-
#mark_limited(reset_at: nil, reset_in: nil) ⇒ void
Mark as rate limited.
-
#reset! ⇒ void
Reset the rate limiter.
-
#status ⇒ Hash
Get rate limiter status.
-
#time_until_reset ⇒ Integer?
Get time until limit resets.
Constructor Details
#initialize(config = nil, default_reset_time: nil) ⇒ RateLimiter
Create a new rate limiter
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 21 def initialize(config = nil, default_reset_time: nil) if config @enabled = config.enabled @default_reset_time = config.default_reset_time else @enabled = true @default_reset_time = default_reset_time || 3600 end reset! end |
Instance Attribute Details
#limit_count ⇒ Object (readonly)
Returns the value of attribute limit_count.
15 16 17 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 15 def limit_count @limit_count end |
#limited_until ⇒ Object (readonly)
Returns the value of attribute limited_until.
15 16 17 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 15 def limited_until @limited_until end |
Instance Method Details
#clear_limit ⇒ void
This method returns an undefined value.
Clear rate limit status
74 75 76 77 78 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 74 def clear_limit @mutex.synchronize do @limited_until = nil end end |
#limited? ⇒ Boolean
Check if currently rate limited
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 36 def limited? return false unless @enabled return false unless @limited_until if Time.now >= @limited_until clear_limit false else true end end |
#mark_limited(reset_at: nil, reset_in: nil) ⇒ void
This method returns an undefined value.
Mark as rate limited
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 53 def mark_limited(reset_at: nil, reset_in: nil) @mutex.synchronize do @limit_count += 1 @limited_until = if reset_at reset_at elsif reset_in Time.now + reset_in else Time.now + @default_reset_time end AgentHarness.logger&.warn( "[AgentHarness::RateLimiter] Rate limited until #{@limited_until}" ) end end |
#reset! ⇒ void
This method returns an undefined value.
Reset the rate limiter
93 94 95 96 97 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 93 def reset! @mutex = Mutex.new @limited_until = nil @limit_count = 0 end |
#status ⇒ Hash
Get rate limiter status
102 103 104 105 106 107 108 109 110 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 102 def status { limited: limited?, limited_until: @limited_until, time_until_reset: time_until_reset, limit_count: @limit_count, enabled: @enabled } end |
#time_until_reset ⇒ Integer?
Get time until limit resets
83 84 85 86 87 88 |
# File 'lib/agent_harness/orchestration/rate_limiter.rb', line 83 def time_until_reset return nil unless @limited_until remaining = @limited_until - Time.now remaining.positive? ? remaining.to_i : 0 end |