Class: OllamaAgent::Providers::RateWindow
- Inherits:
-
Object
- Object
- OllamaAgent::Providers::RateWindow
- 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.
Instance Method Summary collapse
-
#count ⇒ Integer
Number of entries in the current window (for request-count windows).
-
#current_rate ⇒ Integer
Sum of all values recorded within the current window.
-
#initialize(window_seconds: 60) ⇒ RateWindow
constructor
A new instance of RateWindow.
-
#record(value = 1) ⇒ Object
Record a value (default 1 for request counting, N for token counting).
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
#count ⇒ Integer
Number of entries in the current window (for request-count windows).
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_rate ⇒ Integer
Sum of all values recorded within the current window.
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).
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 |