Skip to content

Commit

Permalink
Configurable HTTP timeout and retry
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel committed Jan 31, 2025
1 parent 53fe0f0 commit 8b493ca
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ on:
schedule:
- cron: "0 8 * * 2"

env:
HTTP_REQUEST_CONNECT_TIMEOUT: 10
HTTP_REQUEST_READ_TIMEOUT: 20
HTTP_RETRY_COUNT: 3

jobs:
tests-linux:
name: "Tests on ${{ matrix.python-version }} on ${{ matrix.os }}"
Expand Down
28 changes: 21 additions & 7 deletions pydov/util/net.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

import pydov

request_timeout = (30, 600)


def proxy_autoconfiguration():
"""Try proxy autoconfiguration via PAC.
Expand Down Expand Up @@ -112,7 +110,21 @@ class TimeoutHTTPAdapter(HTTPAdapter):

def __init__(self, *args, **kwargs):
"""Initialisation."""
self.timeout = request_timeout

request_timeout = os.environ.get('HTTP_REQUEST_TIMEOUT', None)
request_connect_timeout = os.environ.get(
'HTTP_REQUEST_CONNECT_TIMEOUT', None)
request_read_timeout = os.environ.get(
'HTTP_REQUEST_READ_TIMEOUT', None)

if request_connect_timeout is not None and request_read_timeout is not None:
self.timeout = (int(request_connect_timeout),
int(request_read_timeout))
elif request_timeout is not None:
self.timeout = int(request_timeout)
else:
self.timeout = 600

if "timeout" in kwargs:
self.timeout = kwargs["timeout"]
del kwargs["timeout"]
Expand Down Expand Up @@ -156,25 +168,27 @@ def get_session():
"""
session = requests.Session()

retry_count = int(os.environ.get('HTTP_RETRY_COUNT', 10))

session.headers.update(
{'user-agent': 'pydov/{}'.format(pydov.__version__)})

try:
retry = urllib3.util.Retry(
total=10, connect=10, read=10, redirect=5, backoff_factor=1,
total=retry_count, connect=retry_count, read=retry_count, redirect=5, backoff_factor=1,
allowed_methods=set(
['HEAD', 'GET', 'POST', 'PUT', 'OPTIONS']))
except TypeError:
# urllib3 < 1.26.0 used method_whitelist instead
retry = urllib3.util.Retry(
total=10, connect=10, read=10, redirect=5, backoff_factor=1,
total=retry_count, connect=retry_count, read=retry_count, redirect=5, backoff_factor=1,
method_whitelist=set(
['HEAD', 'GET', 'POST', 'PUT', 'OPTIONS']))

adapter = TimeoutHTTPAdapter(timeout=request_timeout,
max_retries=retry)
adapter = TimeoutHTTPAdapter(max_retries=retry)
session.mount('http://', adapter)
session.mount('https://', adapter)
session.timeout = adapter.timeout
return session


Expand Down
2 changes: 1 addition & 1 deletion pydov/util/owsutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def get_remote_metadata(contentmetadata):
"""
with warnings.catch_warnings():
warnings.filterwarnings("ignore", category=FutureWarning)
contentmetadata.parse_remote_metadata(pydov.util.net.request_timeout)
contentmetadata.parse_remote_metadata(pydov.session.timeout)

for remote_md in contentmetadata.metadataUrls:
if 'metadata' in remote_md and remote_md['metadata'] is not None:
Expand Down

0 comments on commit 8b493ca

Please sign in to comment.