Class: AgentHarness::Orchestration::RateLimiter

Inherits:
Object
  • Object
show all
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.

Examples:

limiter = RateLimiter.new
limiter.mark_limited(reset_at: Time.now + 3600)
limiter.limited? # => true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = nil, default_reset_time: nil) ⇒ RateLimiter

Create a new rate limiter

Parameters:

  • config (RateLimitConfig, nil) (defaults to: nil)

    configuration object

  • default_reset_time (Integer) (defaults to: nil)

    default seconds until reset



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_countObject (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_untilObject (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_limitvoid

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

Returns:

  • (Boolean)

    true if 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

Parameters:

  • reset_at (Time, nil) (defaults to: nil)

    when the limit resets

  • reset_in (Integer, nil) (defaults to: nil)

    seconds until reset



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

#statusHash

Get rate limiter status

Returns:

  • (Hash)

    status information



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_resetInteger?

Get time until limit resets

Returns:

  • (Integer, nil)

    seconds until reset, or nil if not limited



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