-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/indexing rate limit hardening #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import time | ||
| from typing import Callable, Iterable, Sequence, TypeVar | ||
|
|
||
| from tenacity import RetryCallState, retry, retry_if_exception, stop_after_attempt, wait_exponential | ||
|
|
||
| from src.core.config import load_settings | ||
|
|
||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| def batched(items: Sequence[T], batch_size: int) -> list[list[T]]: | ||
| if batch_size <= 0: | ||
| raise ValueError("batch_size must be positive") | ||
| return [list(items[index : index + batch_size]) for index in range(0, len(items), batch_size)] | ||
|
|
||
|
|
||
| def is_rate_limit_exception(exc: BaseException) -> bool: | ||
| message = str(exc).lower() | ||
| return "429" in message or "rate limit" in message or "resource exhausted" in message | ||
|
|
||
|
|
||
| def _before_sleep(retry_state: RetryCallState) -> None: | ||
| exception = retry_state.outcome.exception() if retry_state.outcome else None | ||
| print( | ||
| "Rate limited during indexing write; retrying " | ||
| f"(attempt {retry_state.attempt_number}) after error: {exception}" | ||
| ) | ||
|
|
||
|
|
||
| def run_rate_limited_write(write_operation: Callable[[], None]) -> None: | ||
| settings = load_settings() | ||
|
|
||
| @retry( | ||
| retry=retry_if_exception(is_rate_limit_exception), | ||
| stop=stop_after_attempt(settings.indexing_max_retries + 1), | ||
| wait=wait_exponential(multiplier=settings.indexing_backoff_base_seconds, min=settings.indexing_backoff_base_seconds), | ||
| before_sleep=_before_sleep, | ||
| reraise=True, | ||
| ) | ||
| def _wrapped_write() -> None: | ||
| write_operation() | ||
|
|
||
| _wrapped_write() | ||
|
|
||
|
|
||
| def sleep_between_batches() -> None: | ||
| settings = load_settings() | ||
| if settings.indexing_batch_sleep_ms > 0: | ||
| time.sleep(settings.indexing_batch_sleep_ms / 1000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import unittest | ||
|
|
||
| from src.rag.indexing import batched, is_rate_limit_exception | ||
|
|
||
|
|
||
| class IndexingHelperTests(unittest.TestCase): | ||
| def test_batched_splits_items_into_configurable_chunks(self): | ||
| items = list(range(7)) | ||
|
|
||
| result = batched(items, 3) | ||
|
|
||
| self.assertEqual([[0, 1, 2], [3, 4, 5], [6]], result) | ||
|
|
||
| def test_batched_requires_positive_batch_size(self): | ||
| with self.assertRaises(ValueError): | ||
| batched([1, 2], 0) | ||
|
|
||
| def test_is_rate_limit_exception_detects_common_provider_messages(self): | ||
| self.assertTrue(is_rate_limit_exception(Exception("429 Too Many Requests"))) | ||
| self.assertTrue(is_rate_limit_exception(Exception("Resource exhausted for quota"))) | ||
| self.assertTrue(is_rate_limit_exception(Exception("Rate limit exceeded"))) | ||
| self.assertFalse(is_rate_limit_exception(Exception("connection reset by peer"))) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.