-
Notifications
You must be signed in to change notification settings - Fork 21
Update wbi_helpers.py #834
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
base: master
Are you sure you want to change the base?
Conversation
rely on backoff to manage exceptions and retry
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.
Pull Request Overview
This PR updates the SPARQL query execution function to rely on the @wbi_backoff() decorator for retry logic instead of implementing custom retry loops. The change removes the manual retry loop and max_retries parameter, delegating retry management to the backoff decorator.
Key changes:
- Removed manual retry loop and
max_retriesparameter fromexecute_sparql_query - Simplified exception handling to rely on backoff decorator for retries
- Updated error handling to raise exceptions that can be caught by the backoff mechanism
| try: | ||
| response = helpers_session.post(sparql_endpoint_url, params=params, headers=headers) | ||
| except BaseException as e: | ||
| if config['BACKOFF_MAX_TRIES'] > 1: | ||
| sleep(retry_after) | ||
| continue | ||
| raise e | ||
| else: | ||
| if response.status_code in (500, 502, 503, 504): |
Copilot
AI
Sep 2, 2025
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.
Catching BaseException is overly broad and will catch system exit signals like KeyboardInterrupt and SystemExit. This should be Exception instead to avoid interfering with system-level exceptions.
| except BaseException as e: | ||
| if config['BACKOFF_MAX_TRIES'] > 1: | ||
| sleep(retry_after) | ||
| continue | ||
| raise e |
Copilot
AI
Sep 2, 2025
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 sleep operation defeats the purpose of using the backoff decorator. The backoff decorator should handle all timing delays, so this manual sleep should be removed to let backoff manage retry timing properly.
| raise Exception("Service unavailable (HTTP Code %d)." % (response.status_code)) | ||
| if response.status_code == 429: | ||
| if 'retry-after' in response.headers.keys(): | ||
| retry_after = int(response.headers['retry-after']) | ||
| log.error("Too Many Requests (429). Sleeping for %d seconds", retry_after) | ||
| sleep(retry_after) | ||
| continue | ||
| response.raise_for_status() | ||
| raise Exception("Too Many Requests (429).") |
Copilot
AI
Sep 2, 2025
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.
Using generic Exception reduces error handling precision. Consider using a more specific exception type like requests.HTTPError or creating a custom exception class for HTTP service errors.
| sleep(retry_after) | ||
| continue | ||
| response.raise_for_status() | ||
| raise Exception("Too Many Requests (429).") |
Copilot
AI
Sep 2, 2025
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.
Using generic Exception reduces error handling precision. Consider using a more specific exception type like requests.HTTPError or creating a custom exception class for rate limiting errors.
| if config['BACKOFF_MAX_TRIES'] > 1: | ||
| sleep(retry_after) | ||
| continue | ||
| raise e | ||
| else: | ||
| if response.status_code in (500, 502, 503, 504): | ||
| log.error("Service unavailable (HTTP Code %d). Sleeping for %d seconds.", response.status_code, retry_after) | ||
| sleep(retry_after) | ||
| continue | ||
| raise Exception("Service unavailable (HTTP Code %d)." % (response.status_code)) | ||
| if response.status_code == 429: | ||
| if 'retry-after' in response.headers.keys(): | ||
| retry_after = int(response.headers['retry-after']) | ||
| log.error("Too Many Requests (429). Sleeping for %d seconds", retry_after) | ||
| sleep(retry_after) | ||
| continue | ||
| response.raise_for_status() | ||
| raise Exception("Too Many Requests (429).") |
Copilot
AI
Sep 2, 2025
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.
Manual sleep calls before raising exceptions will cause double delays when combined with the backoff decorator. The backoff decorator should handle all retry timing, so these sleep calls should be removed.
Rely on backoff to manage exceptions and retry.
Adresses #453