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

[#1744] Added timeouts to requests #777

Merged
merged 1 commit into from
Sep 28, 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
1 change: 0 additions & 1 deletion src/open_inwoner/cms/cases/views/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from django.views.generic import FormView, TemplateView

from django_htmx.http import HttpResponseClientRedirect
from glom import glom
from mail_editor.helpers import find_template
from view_breadcrumbs import BaseBreadcrumbMixin
from zgw_consumers.api_models.constants import RolOmschrijving
Expand Down
2 changes: 2 additions & 0 deletions src/open_inwoner/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
("nl", _("Dutch")),
]

# Default (connection timeout, read timeout) for the requests library (in seconds)
DEFAULT_TIMEOUT_REQUESTS = (10, 60)

TIME_ZONE = "Europe/Amsterdam" # note: this *may* affect the output of DRF datetimes

Expand Down
27 changes: 27 additions & 0 deletions src/open_inwoner/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@
do NOT import anything Django related here, as this file needs to be loaded
before Django is initialized.
"""
import logging
import os

from django.conf import settings

from dotenv import load_dotenv
from requests import Session

logger = logging.getLogger(__name__)


def setup_env():
Expand All @@ -20,3 +26,24 @@ def setup_env():
load_dotenv(dotenv_path)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "open_inwoner.conf.dev")

monkeypatch_requests()


def monkeypatch_requests():
"""
Add a default timeout for any requests calls.
"""
if hasattr(Session, "_original_request"):
logger.debug(
"Session is already patched OR has an ``_original_request`` attribute."
)
return

Session._original_request = Session.request

def new_request(self, *args, **kwargs):
kwargs.setdefault("timeout", settings.DEFAULT_TIMEOUT_REQUESTS)
return self._original_request(*args, **kwargs)

Session.request = new_request
Loading