rate_limits
rate_limits
¶
Rate limit event mixin for the global learning store.
This module contains the RateLimitMixin class that provides rate limit event recording and querying functionality. Rate limits enable cross-workspace coordination so that parallel jobs can avoid hitting the same API limits.
Evolution #8: Cross-Workspace Circuit Breaker - enables jobs running in different workspaces to share rate limit awareness.
Extracted from global_store.py as part of the modularization effort.
Classes¶
RateLimitMixin
¶
Mixin providing rate limit event functionality.
This mixin provides methods for recording and querying rate limit events across workspaces. When one job hits a rate limit, it records the event so that other parallel jobs can check and avoid hitting the same limit.
Requires the following from the composed class
- _get_connection() -> context manager yielding sqlite3.Connection
- hash_job(job_id: str) -> str (static method)
Functions¶
record_rate_limit_event
¶
Record a rate limit event for cross-workspace coordination.
When one job hits a rate limit, it records the event so that other parallel jobs can check and avoid hitting the same limit.
Evolution #8: Cross-Workspace Circuit Breaker - enables jobs running in different workspaces to share rate limit awareness.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_code
|
str
|
The error code (e.g., 'E101', 'E102'). |
required |
duration_seconds
|
float
|
Expected rate limit duration in seconds. |
required |
job_id
|
str
|
ID of the job that encountered the rate limit. |
required |
model
|
str | None
|
Optional model name that triggered the limit. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The rate limit event record ID. |
Source code in src/marianne/learning/store/rate_limits.py
is_rate_limited
¶
Check if there's an active rate limit from another job.
Queries the rate_limit_events table to see if any unexpired rate limit events exist that would affect this job.
Evolution #8: Cross-Workspace Circuit Breaker - allows jobs to check if another job has already hit a rate limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error_code
|
str | None
|
Optional error code to filter by. If None, checks any. |
None
|
model
|
str | None
|
Optional model to filter by. If None, checks any. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
Tuple of (is_limited: bool, seconds_until_expiry: float | None). |
float | None
|
If is_limited is True, seconds_until_expiry indicates when it clears. |
Source code in src/marianne/learning/store/rate_limits.py
get_active_rate_limits
¶
Get all active (unexpired) rate limit events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
Optional model to filter by. |
None
|
Returns:
| Type | Description |
|---|---|
list[RateLimitEvent]
|
List of RateLimitEvent objects that haven't expired yet. |
Source code in src/marianne/learning/store/rate_limits.py
cleanup_expired_rate_limits
¶
Remove expired rate limit events from the database.
This is a housekeeping method that can be called periodically to prevent the rate_limit_events table from growing unbounded.
Returns:
| Type | Description |
|---|---|
int
|
Number of expired records deleted. |