Skip to content
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

cirq-ionq: Retry non-standard cloudflare errors #5237

Merged
merged 2 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cirq-ionq/cirq_ionq/ionq_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,24 @@
import cirq_ionq
from cirq_ionq import ionq_exceptions

RETRIABLE_STATUS_CODES = {
requests.codes.internal_server_error,
requests.codes.service_unavailable,
}


def _is_retriable(code):
# Handle 52x responses from cloudflare.
# See https://support.cloudflare.com/hc/en-us/articles/115003011431/
return code in RETRIABLE_STATUS_CODES or (code >= 520 and code <= 530)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

washing out my eyes after reading the extra proxy service errors



class _IonQClient:
"""Handles calls to IonQ's API.

Users should not instantiate this themselves, but instead should use `cirq_ionq.Service`.
"""

RETRIABLE_STATUS_CODES = {
requests.codes.internal_server_error,
requests.codes.service_unavailable,
}
SUPPORTED_TARGETS = {'qpu', 'simulator'}
SUPPORTED_VERSIONS = {
'v0.1',
Expand Down Expand Up @@ -294,7 +301,7 @@ def _make_request(self, request: Callable[[], requests.Response]) -> requests.Re
raise ionq_exceptions.IonQNotFoundException(
'IonQ could not find requested resource.'
)
if response.status_code not in self.RETRIABLE_STATUS_CODES:
if not _is_retriable(response.status_code):
raise ionq_exceptions.IonQException(
'Non-retry-able error making request to IonQ API. '
f'Status: {response.status_code} '
Expand Down
15 changes: 15 additions & 0 deletions cirq-ionq/cirq_ionq/ionq_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,18 @@ def test_ionq_client_list_calibrations_retry(mock_get):
)
client.list_calibrations()
assert mock_get.call_count == 2


@mock.patch('requests.get')
def test_ionq_client_list_calibrations_retry_nonstandard_error(mock_get):
response1 = mock.MagicMock()
response2 = mock.MagicMock()
mock_get.side_effect = [response1, response2]
response1.ok = False
response1.status_code = 524
response2.ok = True
client = ionq.ionq_client._IonQClient(
remote_host='http://example.com', api_key='to_my_heart', default_target='simulator'
)
client.list_calibrations()
assert mock_get.call_count == 2