diff options
author | Daniel Schadt <kingdread@gmx.de> | 2022-11-17 19:07:31 +0100 |
---|---|---|
committer | Daniel Schadt <kingdread@gmx.de> | 2022-11-17 19:07:31 +0100 |
commit | 8155036b1af08dfa6cd5ff9a746f018f79350cef (patch) | |
tree | 5cd79ad31633cd042df1e9e6400c83a46cfdc4e3 | |
parent | 55aad97b75351b671e7c3ab3a05c43d2decc05c5 (diff) | |
download | fietsboek-8155036b1af08dfa6cd5ff9a746f018f79350cef.tar.gz fietsboek-8155036b1af08dfa6cd5ff9a746f018f79350cef.tar.bz2 fietsboek-8155036b1af08dfa6cd5ff9a746f018f79350cef.zip |
add a timeout to the tile proxy
This avoids blocking the whole pipeline
-rw-r--r-- | fietsboek/views/tileproxy.py | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/fietsboek/views/tileproxy.py b/fietsboek/views/tileproxy.py index 16fd4c5..01dc9a9 100644 --- a/fietsboek/views/tileproxy.py +++ b/fietsboek/views/tileproxy.py @@ -8,16 +8,20 @@ Additionally, this protects the users' IP, as only fietsboek can see it. """ import datetime import random +import logging from pyramid.view import view_config from pyramid.response import Response -from pyramid.httpexceptions import HTTPBadRequest +from pyramid.httpexceptions import HTTPBadRequest, HTTPGatewayTimeout import requests +from requests.exceptions import ReadTimeout from .. import __VERSION__ +LOGGER = logging.getLogger(__name__) + TILE_SOURCES = { # Main base layers 'osm': 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', @@ -35,6 +39,7 @@ TILE_SOURCES = { } TTL = datetime.timedelta(days=7) +TIMEOUT = datetime.timedelta(seconds=1.5) @view_config(route_name='tile-proxy', http_cache=3600) @@ -67,6 +72,11 @@ def tile_proxy(request): if from_mail: headers["from"] = from_mail - resp = requests.get(url, headers=headers) - request.redis.set(cache_key, resp.content, ex=TTL) - return Response(resp.content, content_type=resp.headers.get("Content-type", content_type)) + try: + resp = requests.get(url, headers=headers, timeout=TIMEOUT.total_seconds()) + except ReadTimeout: + LOGGER.debug("Proxy timeout when accessing %r", url) + return HTTPGatewayTimeout(f"No response in time from {url}") + else: + request.redis.set(cache_key, resp.content, ex=TTL) + return Response(resp.content, content_type=resp.headers.get("Content-type", content_type)) |