From bb754e09028a010e88195a666da40aff4dd65ef6 Mon Sep 17 00:00:00 2001 From: Alex Vandiver Date: Mon, 10 Aug 2020 12:09:38 -0700 Subject: [PATCH] tornado: Add a retry with backoff to django-to-tornado requests. This better hides errors from users during the moments when Tornado is being restarted. --- zerver/tornado/django_api.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/zerver/tornado/django_api.py b/zerver/tornado/django_api.py index 1e563797d1..60a1cd41c2 100644 --- a/zerver/tornado/django_api.py +++ b/zerver/tornado/django_api.py @@ -5,6 +5,8 @@ from typing import Any, Dict, Iterable, List, Mapping, Optional, Sequence, Union import requests import ujson from django.conf import settings +from requests.adapters import HTTPAdapter +from requests.packages.urllib3.util.retry import Retry from zerver.lib.queue import queue_json_publish from zerver.models import Client, Realm, UserProfile @@ -20,6 +22,15 @@ def requests_client() -> requests.Session: # Disable trusting the environment, so requests don't # go through any env-configured external proxy. c.trust_env = False + + # During restarts, the tornado server may be down briefly + retry = Retry( + total=3, + backoff_factor=1, + ) + adapter = HTTPAdapter(max_retries=retry) + for scheme in ("https://", "http://"): + c.mount(scheme, adapter) return c def request_event_queue(user_profile: UserProfile, user_client: Client, apply_markdown: bool,