-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Flickr rate limit retry with backoff #84
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,29 @@ | ||
| name: Tests | ||
|
|
||
| on: | ||
| pull_request: | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| tests: | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v6 | ||
|
|
||
| - name: Install poetry | ||
| run: pipx install poetry | ||
|
|
||
| - name: Set up Python 3.13 | ||
| uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: '3.13' | ||
| cache: 'poetry' | ||
|
|
||
| - name: Install dependencies | ||
| run: poetry install --with dev | ||
|
|
||
| - name: Run tests | ||
| run: poetry run pytest |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,6 @@ | ||
| [pytest] | ||
| testpaths = tests | ||
| python_files = test_*.py | ||
| python_functions = test_* | ||
| pythonpath = src | ||
| timeout = 0.25 |
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 |
|---|---|---|
|
|
@@ -4,18 +4,21 @@ | |
| from datetime import datetime | ||
| from time import perf_counter | ||
|
|
||
| import httpx | ||
| from flickr_api import FlickrApi | ||
| from flickr_api.exceptions import PermissionDenied, ResourceNotFound | ||
| from flickr_api.models import SinglePhotoInfo | ||
| from flickr_api.models.photo import Location | ||
| from flickr_url_parser import parse_flickr_url | ||
|
|
||
| from wikibots.lib.bot import BaseBot | ||
| from wikibots.lib.bot import BaseBot, RateLimitExhausted | ||
| from wikibots.lib.claim import WBTIME_PRECISION, Claim | ||
| from wikibots.lib.wikidata import WikidataEntity, WikidataProperty | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| RATE_LIMIT_DELAYS = (60, 180, 300) | ||
|
|
||
|
|
||
| class FlickrBot(BaseBot): | ||
| redis_prefix = "xQ6cz5J84Viw/K6FIcOH1kxJjfiS8jO56AoSmhBgO/A=" | ||
|
|
@@ -137,18 +140,32 @@ def get_flickr_photo(self, flickr_photo_id: str) -> None: | |
| logger.warning("Flickr photo skipped due to Redis cache") | ||
| return | ||
|
|
||
| try: | ||
| start = perf_counter() | ||
| self.photo = self.flickr_api.get_single_photo_info(photo_id=flickr_photo_id) | ||
| logger.info( | ||
| f"Retrieved Flickr photo in {(perf_counter() - start) * 1000:.0f} ms" | ||
| ) | ||
| except (ResourceNotFound, PermissionDenied) as e: | ||
| logger.warning(f"[{flickr_photo_id}] {e}") | ||
| self.redis.set(redis_key_photo, 1) | ||
| except Exception as e: | ||
| logger.error(f"[{flickr_photo_id}] {e}") | ||
| time.sleep(60) | ||
| delays = iter(RATE_LIMIT_DELAYS) | ||
| while True: | ||
| try: | ||
| start = perf_counter() | ||
| self.photo = self.flickr_api.get_single_photo_info(photo_id=flickr_photo_id) | ||
| logger.info( | ||
| f"Retrieved Flickr photo in {(perf_counter() - start) * 1000:.0f} ms" | ||
| ) | ||
| return | ||
| except (ResourceNotFound, PermissionDenied) as e: | ||
| logger.warning(f"[{flickr_photo_id}] {e}") | ||
| self.redis.set(redis_key_photo, 1) | ||
| return | ||
| except httpx.HTTPStatusError as e: | ||
| if e.response.status_code != 429: | ||
| logger.error(f"[{flickr_photo_id}] {e}") | ||
| return | ||
| delay = next(delays, None) | ||
| if delay is None: | ||
| logger.critical(f"[{flickr_photo_id}] Rate limit exhausted after all retries") | ||
| raise RateLimitExhausted | ||
| logger.warning(f"[{flickr_photo_id}] Rate limited, retrying in {delay}s") | ||
| time.sleep(delay) | ||
| except Exception as e: | ||
| logger.error(f"[{flickr_photo_id}] {e}") | ||
| return | ||
|
Comment on lines
+166
to
+168
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| def hook_creator_claim(self, claim: Claim) -> None: | ||
| if not self.photo: | ||
|
|
||
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
Empty file.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The retry logic for HTTP 429 (Too Many Requests) uses hardcoded delays. While this is a good start, the Flickr API typically returns a
Retry-Afterheader indicating how long the client should wait. It would be more efficient to respect this header if present, falling back to the hardcoded delays only if the header is missing.