Skip to content

Commit

Permalink
added timeout to middleware requests (#2434)
Browse files Browse the repository at this point in the history
* added timeout to middleware requests

* updated the reference for timeout value in function

---------

Co-authored-by: Vignesh Hari <14056798+vigneshhari@users.noreply.github.com>
  • Loading branch information
DraKen0009 and vigneshhari authored Sep 19, 2024
1 parent 2bb7357 commit d36b5d9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions care/utils/assetintegration/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, meta):
self.host = self.meta["local_ip_address"]
self.middleware_hostname = self.meta["middleware_hostname"]
self.insecure_connection = self.meta.get("insecure_connection", False)
self.timeout = settings.MIDDLEWARE_REQUEST_TIMEOUT

def handle_action(self, action):
pass
Expand All @@ -37,12 +38,17 @@ def api_post(self, url, data=None):
url,
json=data,
headers=self.get_headers(),
timeout=self.timeout,
)
try:
response = req.json()
if req.status_code >= 400:
raise APIException(response, req.status_code)
return response

except requests.Timeout:
raise APIException({"error": "Request Timeout"}, 504)

except json.decoder.JSONDecodeError:
raise APIException({"error": "Invalid Response"}, req.status_code)

Expand All @@ -51,11 +57,16 @@ def api_get(self, url, data=None):
url,
params=data,
headers=self.get_headers(),
timeout=self.timeout,
)
try:
if req.status_code >= 400:
raise APIException(req.text, req.status_code)
response = req.json()
return response

except requests.Timeout:
raise APIException({"error": "Request Timeout"}, 504)

except json.decoder.JSONDecodeError:
raise APIException({"error": "Invalid Response"}, req.status_code)
6 changes: 4 additions & 2 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import json
from datetime import datetime, timedelta
from pathlib import Path
from django.utils.translation import gettext_lazy as _

import environ
from authlib.jose import JsonWebKey
from django.utils.translation import gettext_lazy as _
from healthy_django.healthcheck.celery_queue_length import (
DjangoCeleryQueueLengthHealthCheck,
)
Expand Down Expand Up @@ -55,7 +55,6 @@
# https://docs.djangoproject.com/en/dev/ref/settings/#locale-paths
LOCALE_PATHS = [str(BASE_DIR / "locale")]


LANGUAGES = [
("en-us", _("English")),
("ml", _("Malayalam")),
Expand Down Expand Up @@ -663,3 +662,6 @@
TASK_SUMMARIZE_DISTRICT_PATIENT = env.bool(
"TASK_SUMMARIZE_DISTRICT_PATIENT", default=True
)

# Timeout for middleware request (in seconds)
MIDDLEWARE_REQUEST_TIMEOUT = env.int("MIDDLEWARE_REQUEST_TIMEOUT", 20)

0 comments on commit d36b5d9

Please sign in to comment.