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

fix: consistently retry on error codes and respect Retry-After header #7072

Merged
merged 2 commits into from
Apr 4, 2023
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
4 changes: 3 additions & 1 deletion src/poetry/publishing/uploader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from poetry.__version__ import __version__
from poetry.utils.constants import REQUESTS_TIMEOUT
from poetry.utils.constants import STATUS_FORCELIST
from poetry.utils.patterns import wheel_file_re


Expand Down Expand Up @@ -68,7 +69,8 @@ def adapter(self) -> adapters.HTTPAdapter:
connect=5,
total=10,
allowed_methods=["GET"],
status_forcelist=[500, 501, 502, 503],
respect_retry_after_header=True,
status_forcelist=STATUS_FORCELIST,
)

return adapters.HTTPAdapter(max_retries=retry)
Expand Down
15 changes: 13 additions & 2 deletions src/poetry/utils/authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
from poetry.config.config import Config
from poetry.exceptions import PoetryException
from poetry.utils.constants import REQUESTS_TIMEOUT
from poetry.utils.constants import RETRY_AFTER_HEADER
from poetry.utils.constants import STATUS_FORCELIST
from poetry.utils.password_manager import HTTPAuthCredential
from poetry.utils.password_manager import PasswordManager

Expand Down Expand Up @@ -250,6 +252,7 @@ def request(
send_kwargs.update(settings)

attempt = 0
resp = None

while True:
is_last_attempt = attempt >= 5
Expand All @@ -259,21 +262,29 @@ def request(
if is_last_attempt:
raise e
else:
if resp.status_code not in [502, 503, 504] or is_last_attempt:
if resp.status_code not in STATUS_FORCELIST or is_last_attempt:
if raise_for_status:
resp.raise_for_status()
return resp

if not is_last_attempt:
attempt += 1
delay = 0.5 * attempt
delay = self._get_backoff(resp, attempt)
logger.debug("Retrying HTTP request in %s seconds.", delay)
time.sleep(delay)
continue

# this should never really be hit under any sane circumstance
raise PoetryException("Failed HTTP {} request", method.upper())

def _get_backoff(self, response: requests.Response | None, attempt: int) -> float:
if response is not None:
retry_after = response.headers.get(RETRY_AFTER_HEADER, "")
if retry_after:
return float(retry_after)

return 0.5 * attempt
radoering marked this conversation as resolved.
Show resolved Hide resolved

def get(self, url: str, **kwargs: Any) -> requests.Response:
return self.request("get", url, **kwargs)

Expand Down
5 changes: 5 additions & 0 deletions src/poetry/utils/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@

# Timeout for HTTP requests using the requests library.
REQUESTS_TIMEOUT = 15

RETRY_AFTER_HEADER = "retry-after"

# Server response codes to retry requests on.
STATUS_FORCELIST = [429, 500, 501, 502, 503, 504]
nejch marked this conversation as resolved.
Show resolved Hide resolved
31 changes: 30 additions & 1 deletion tests/utils/test_authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,43 @@ def callback(*_: Any, **___: Any) -> None:
assert sleep.call_count == 5


def test_authenticator_request_respects_retry_header(
mocker: MockerFixture,
config: Config,
http: type[httpretty.httpretty],
):
sleep = mocker.patch("time.sleep")
sdist_uri = f"https://foo.bar/files/{uuid.uuid4()!s}/foo-0.1.0.tar.gz"
content = str(uuid.uuid4())
seen = []

def callback(
request: requests.Request, uri: str, response_headers: dict
) -> list[int | dict | str]:
if not seen.count(uri):
seen.append(uri)
return [429, {"Retry-After": "42"}, "Retry later"]

return [200, response_headers, content]

http.register_uri(httpretty.GET, sdist_uri, body=callback)
authenticator = Authenticator(config, NullIO())

response = authenticator.request("get", sdist_uri)
assert sleep.call_args[0] == (42.0,)
assert response.text == content


@pytest.mark.parametrize(
["status", "attempts"],
[
(400, 0),
(401, 0),
(403, 0),
(404, 0),
(500, 0),
(429, 5),
(500, 5),
(501, 5),
(502, 5),
(503, 5),
(504, 5),
Expand Down