From 8155036b1af08dfa6cd5ff9a746f018f79350cef Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Thu, 17 Nov 2022 19:07:31 +0100 Subject: add a timeout to the tile proxy This avoids blocking the whole pipeline --- fietsboek/views/tileproxy.py | 18 ++++++++++++++---- 1 file 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)) -- cgit v1.2.3