Skip to content

Commit

Permalink
Make HttpxBackoffClient backoff on Name or service not known Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Filip Čáp committed Oct 31, 2023
1 parent dbff688 commit 3dca13b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
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

0 comments on commit 3dca13b

Please sign in to comment.