Skip to content

Commit 7dec6b9

Browse files
Cynocracytonybruguier
authored andcommitted
cirq-ionq: Retry non-standard cloudflare errors (quantumlib#5237)
1 parent 4c6344e commit 7dec6b9

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

cirq-ionq/cirq_ionq/ionq_client.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,24 @@
2424
import cirq_ionq
2525
from cirq_ionq import ionq_exceptions
2626

27+
RETRIABLE_STATUS_CODES = {
28+
requests.codes.internal_server_error,
29+
requests.codes.service_unavailable,
30+
}
31+
32+
33+
def _is_retriable(code):
34+
# Handle 52x responses from cloudflare.
35+
# See https://support.cloudflare.com/hc/en-us/articles/115003011431/
36+
return code in RETRIABLE_STATUS_CODES or (code >= 520 and code <= 530)
37+
2738

2839
class _IonQClient:
2940
"""Handles calls to IonQ's API.
3041
3142
Users should not instantiate this themselves, but instead should use `cirq_ionq.Service`.
3243
"""
3344

34-
RETRIABLE_STATUS_CODES = {
35-
requests.codes.internal_server_error,
36-
requests.codes.service_unavailable,
37-
}
3845
SUPPORTED_TARGETS = {'qpu', 'simulator'}
3946
SUPPORTED_VERSIONS = {
4047
'v0.1',
@@ -294,7 +301,7 @@ def _make_request(self, request: Callable[[], requests.Response]) -> requests.Re
294301
raise ionq_exceptions.IonQNotFoundException(
295302
'IonQ could not find requested resource.'
296303
)
297-
if response.status_code not in self.RETRIABLE_STATUS_CODES:
304+
if not _is_retriable(response.status_code):
298305
raise ionq_exceptions.IonQException(
299306
'Non-retry-able error making request to IonQ API. '
300307
f'Status: {response.status_code} '

cirq-ionq/cirq_ionq/ionq_client_test.py

+15
Original file line numberDiff line numberDiff line change
@@ -757,3 +757,18 @@ def test_ionq_client_list_calibrations_retry(mock_get):
757757
)
758758
client.list_calibrations()
759759
assert mock_get.call_count == 2
760+
761+
762+
@mock.patch('requests.get')
763+
def test_ionq_client_list_calibrations_retry_nonstandard_error(mock_get):
764+
response1 = mock.MagicMock()
765+
response2 = mock.MagicMock()
766+
mock_get.side_effect = [response1, response2]
767+
response1.ok = False
768+
response1.status_code = 524
769+
response2.ok = True
770+
client = ionq.ionq_client._IonQClient(
771+
remote_host='http://example.com', api_key='to_my_heart', default_target='simulator'
772+
)
773+
client.list_calibrations()
774+
assert mock_get.call_count == 2

0 commit comments

Comments
 (0)