Skip to content
Open
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
2 changes: 1 addition & 1 deletion gitlab/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1439,7 +1439,7 @@ async def execute(self, request: str, *args: Any, **kwargs: Any) -> Any:
parsed_document, *args, **kwargs
)
except gql.transport.exceptions.TransportServerError as e:
if retry.handle_retry_on_status(
if await retry.async_handle_retry_on_status(
status_code=e.code, headers=self._transport.response_headers
):
continue
Expand Down
45 changes: 38 additions & 7 deletions gitlab/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,31 +114,62 @@ def _retryable_status_code(self, status_code: int | None, reason: str = "") -> b

return False

def handle_retry_on_status(
def _get_wait_time_on_status(
self,
status_code: int | None,
headers: MutableMapping[str, str] | None = None,
reason: str = "",
) -> bool:
) -> float | None:
"""Return how long to wait before retrying, or None to not retry."""
if not self._retryable_status_code(status_code, reason):
return False
return None

if headers is None:
headers = {}

# Response headers documentation:
# https://docs.gitlab.com/ee/user/admin_area/settings/user_and_ip_rate_limits.html#response-headers
if self.max_retries == -1 or self.cur_retries < self.max_retries:
wait_time = 2**self.cur_retries * 0.1
wait_time: float = 2**self.cur_retries * 0.1
if "Retry-After" in headers:
wait_time = int(headers["Retry-After"])
elif "RateLimit-Reset" in headers:
wait_time = max(0, int(headers["RateLimit-Reset"]) - time.time())
self.cur_retries += 1
time.sleep(wait_time)
return True
return wait_time

return False
return None

def handle_retry_on_status(
self,
status_code: int | None,
headers: MutableMapping[str, str] | None = None,
reason: str = "",
) -> bool:
wait_time = self._get_wait_time_on_status(status_code, headers, reason)
if wait_time is None:
return False

time.sleep(wait_time)
return True

async def async_handle_retry_on_status(
self,
status_code: int | None,
headers: MutableMapping[str, str] | None = None,
reason: str = "",
) -> bool:
"""Like handle_retry_on_status, without blocking the event loop."""
# anyio ships with httpx (the graphql extra) and supports both asyncio
# and trio; imported here as the synchronous clients do not need it.
import anyio

wait_time = self._get_wait_time_on_status(status_code, headers, reason)
if wait_time is None:
return False

await anyio.sleep(wait_time)
return True

def handle_retry(self) -> bool:
if self.retry_transient_errors and (
Expand Down
25 changes: 25 additions & 0 deletions tests/unit/test_graphql.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import time
from unittest import mock

import httpx
import pytest
import respx
Expand Down Expand Up @@ -99,6 +102,28 @@ async def test_async_graphql_raises_when_max_retries_exceeded(
await gl_async_gql.execute("query {currentUser {id}}")


@pytest.mark.anyio
async def test_async_graphql_retries_without_blocking_event_loop(
api_url: str, respx_mock: respx.MockRouter, monkeypatch: pytest.MonkeyPatch
):
mock_sleep = mock.Mock()
monkeypatch.setattr(time, "sleep", mock_sleep)
responses = [
httpx.Response(502),
httpx.Response(
200, json={"data": {"currentUser": {"id": "gid://gitlab/User/1"}}}
),
]
respx_mock.post(api_url).mock(side_effect=responses)

gl_async_gql = gitlab.AsyncGraphQL(
"https://gitlab.example.com", max_retries=1, retry_transient_errors=True
)
await gl_async_gql.execute("query {currentUser {id}}")

mock_sleep.assert_not_called()


def test_graphql_raises_on_401_response(
api_url: str, gl_gql: gitlab.GraphQL, respx_mock: respx.MockRouter
):
Expand Down
29 changes: 29 additions & 0 deletions tests/unit/test_retry.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
from unittest import mock

import anyio
import pytest

from gitlab import utils
Expand Down Expand Up @@ -39,3 +40,31 @@ def test_handle_retry_on_status_accepts_ratelimit_reset_header(
def test_handle_retry_on_status_returns_false_when_max_retries_reached():
retry = utils.Retry(max_retries=0)
assert retry.handle_retry_on_status(429) is False


@pytest.mark.anyio
async def test_async_handle_retry_on_status_ignores_unknown_status_code():
retry = utils.Retry(max_retries=1, retry_transient_errors=True)
assert await retry.async_handle_retry_on_status(418) is False


@pytest.mark.anyio
async def test_async_handle_retry_on_status_awaits_sleep(
monkeypatch: pytest.MonkeyPatch,
):
mock_sleep = mock.Mock()
mock_async_sleep = mock.AsyncMock()
monkeypatch.setattr(time, "sleep", mock_sleep)
monkeypatch.setattr(anyio, "sleep", mock_async_sleep)
retry = utils.Retry(max_retries=1)
headers = {"Retry-After": "1"}

assert await retry.async_handle_retry_on_status(429, headers=headers) is True
mock_async_sleep.assert_awaited_once_with(1)
mock_sleep.assert_not_called()


@pytest.mark.anyio
async def test_async_handle_retry_on_status_returns_false_when_max_retries_reached():
retry = utils.Retry(max_retries=0)
assert await retry.async_handle_retry_on_status(429) is False
Loading