Class: OllamaAgent::Providers::RateWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/ollama_agent/providers/rate_window.rb

Overview

Thread-safe sliding-window counter for rate-limit awareness.

Tracks how many units (requests or tokens) have occurred within a rolling time window. Used by QuotaTracker to compute live RPM and TPM.

Examples:

window = RateWindow.new(window_seconds: 60)
window.record(1)           # one request
window.record(1500)        # 1500 tokens
window.current_rate        # => sum of values within last 60 s

Instance Method Summary collapse

Constructor Details

#initialize(window_seconds: 60) ⇒ RateWindow

Returns a new instance of RateWindow.



16
17
18
19
20
# File 'lib/ollama_agent/providers/rate_window.rb', line 16

def initialize(window_seconds: 60)
  @window  = window_seconds.to_i
  @entries = [] # Array of { at: Time, value: Integer }
  @mutex   = Mutex.new
end

Instance Method Details

#countInteger

Number of entries in the current window (for request-count windows).

Returns:

  • (Integer)


42
43
44
45
46
47
# File 'lib/ollama_agent/providers/rate_window.rb', line 42

def count
  @mutex.synchronize do
    prune!
    @entries.size
  end
end

#current_rateInteger

Sum of all values recorded within the current window.

Returns:

  • (Integer)


33
34
35
36
37
38
# File 'lib/ollama_agent/providers/rate_window.rb', line 33

def current_rate
  @mutex.synchronize do
    prune!
    @entries.sum { |e| e[:value] }
  end
end

#record(value = 1) ⇒ Object

Record a value (default 1 for request counting, N for token counting).

Parameters:

  • value (Integer) (defaults to: 1)


24
25
26
27
28
29
# File 'lib/ollama_agent/providers/rate_window.rb', line 24

def record(value = 1)
  @mutex.synchronize do
    prune!
    @entries << { at: Time.now, value: value.to_i }
  end
end