Skip to content

Commit

Permalink
Set timeout when connecting to the CGW (#945)
Browse files Browse the repository at this point in the history
- Sets a timeout when connecting to the Safe Client Gateway.
- The timeout is configurable with the environment variable `CGW_SESSION_TIMEOUT_SECONDS`. If it's not set, 2 seconds will be used.
- The maximum number of retries is now configurable under `CGW_SESSION_MAX_RETRIES`. If it's not set, no retries will be made (0).
  • Loading branch information
fmrsabino authored Sep 8, 2023
1 parent 5b95fde commit 4581796
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
6 changes: 6 additions & 0 deletions .dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ GUNICORN_WEB_RELOAD=false
# The Client Gateway /flush token.
#CGW_FLUSH_TOKEN=example-flush-token

# The maximum number of retries to be done when connecting to the Safe Client Gateway
#CGW_SESSION_MAX_RETRIES=0

# The request timeout in seconds for requests done to the Safe Client Gateway
#CGW_SESSION_TIMEOUT_SECONDS=2

# What CPU and memory constraints will be added to your services? When left at
# 0, they will happily use as much as needed.
#DOCKER_POSTGRES_CPUS=0
Expand Down
6 changes: 5 additions & 1 deletion src/clients/safe_client_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
from urllib.parse import urljoin

import requests
from django.conf import settings

logger = logging.getLogger(__name__)


@cache
def setup_session() -> requests.Session:
session = requests.Session()
adapter = requests.adapters.HTTPAdapter(max_retries=3)
adapter = requests.adapters.HTTPAdapter(
max_retries=settings.CGW_SESSION_MAX_RETRIES
)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
Expand All @@ -33,6 +36,7 @@ def flush(
url,
json=json,
headers={"Authorization": f"Basic {cgw_flush_token}"},
timeout=settings.CGW_SESSION_TIMEOUT_SECONDS,
)
post.raise_for_status()
except Exception as error:
Expand Down
2 changes: 2 additions & 0 deletions src/config/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,8 @@

CGW_URL = os.environ.get("CGW_URL")
CGW_FLUSH_TOKEN = os.environ.get("CGW_FLUSH_TOKEN")
CGW_SESSION_MAX_RETRIES = int(os.environ.get("CGW_SESSION_MAX_RETRIES", "0"))
CGW_SESSION_TIMEOUT_SECONDS = int(os.environ.get("CGW_SESSION_TIMEOUT_SECONDS", "2"))

# By default, Django stores files locally, using the MEDIA_ROOT and MEDIA_URL settings.
# (using the default the default FileSystemStorage)
Expand Down

0 comments on commit 4581796

Please sign in to comment.