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

Make HttpxBackoffClient backoff on Name or service not known Error #253

Merged
merged 1 commit into from
Oct 31, 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
49 changes: 29 additions & 20 deletions testsuite/httpx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import Union

import backoff
from httpx import Client, Response
from httpx import Client, Response, ConnectError

from testsuite.certificates import Certificate

Expand All @@ -19,7 +19,7 @@ def create_tmp_file(content: str):


class UnexpectedResponse(Exception):
"""Slightly different response attributes were expected"""
"""Slightly different response attributes were expected or no response was given"""

def __init__(self, msg, response):
super().__init__(msg)
Expand Down Expand Up @@ -58,6 +58,7 @@ def add_retry_code(self, code):
"""Add a new retry code to"""
self.retry_codes.add(code)

# pylint: disable=too-many-locals
@backoff.on_exception(backoff.fibo, UnexpectedResponse, max_tries=8, jitter=None)
def request(
self,
Expand All @@ -76,21 +77,29 @@ def request(
timeout=None,
extensions=None,
) -> Response:
response = super().request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
if response.status_code in self.retry_codes:
raise UnexpectedResponse(f"Didn't expect '{response.status_code}' status code", response)
return response
try:
response = super().request(
method,
url,
content=content,
data=data,
files=files,
json=json,
params=params,
headers=headers,
cookies=cookies,
auth=auth,
follow_redirects=follow_redirects,
timeout=timeout,
extensions=extensions,
)
if response.status_code in self.retry_codes:
raise UnexpectedResponse(f"Didn't expect '{response.status_code}' status code", response)
return response
except ConnectError as e:
# note: when the code reaches this point, negative caching might have been triggered,
# negative caching TTL of SOA record of the zone must be set accordingly,
# otherwise retry will fail if the value is too high
if len(e.args) > 0 and any("Name or service not known" in arg for arg in e.args):
raise UnexpectedResponse("Didn't expect 'Name or service not known' error", None) from e
raise
4 changes: 0 additions & 4 deletions testsuite/tests/mgc/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
* dnspolicies are created and bound to gateways automatically by mgc operator
* dnspolicies leak at this moment
"""
from time import sleep

import pytest

from testsuite.httpx import HttpxBackoffClient
Expand All @@ -38,7 +36,5 @@ def test_smoke(route, upstream_gateway):
# assert that tls_cert is used by the server
backend_client = HttpxBackoffClient(base_url=f"https://{route.hostnames[0]}", verify=tls_cert)

sleep(30) # wait for DNS record to propagate correctly; TBD

response = backend_client.get("get")
assert response.status_code == 200