Add timeout to all Requests calls

Use a default timeout of 30 seconds for all requests, and add a
REQUESTS_TIMEOUT Anymail setting to override.

(I'm making a judgement call that this is not a breaking change in the
real world, and not bumping the major version. Theoretically, it could
affect you if your network somehow takes >30s to connect to your ESP,
but eventually succeeds. If so, set REQUESTS_TIMEOUT to None to restore
the earlier behavior.)

Fixes #80.
This commit is contained in:
medmunds
2018-01-17 14:36:50 -08:00
parent 5fb46952c6
commit 09def30868
4 changed files with 80 additions and 1 deletions

View File

@@ -4,6 +4,7 @@ import requests
# noinspection PyUnresolvedReferences
from six.moves.urllib.parse import urljoin
from anymail.utils import get_anymail_setting
from .base import AnymailBaseBackend, BasePayload
from ..exceptions import AnymailRequestsAPIError, AnymailSerializationError
from .._version import __version__
@@ -17,6 +18,7 @@ class AnymailRequestsBackend(AnymailBaseBackend):
def __init__(self, api_url, **kwargs):
"""Init options from Django settings"""
self.api_url = api_url
self.timeout = get_anymail_setting('requests_timeout', kwargs=kwargs, default=30)
super(AnymailRequestsBackend, self).__init__(**kwargs)
self.session = None
@@ -65,6 +67,7 @@ class AnymailRequestsBackend(AnymailBaseBackend):
Can raise AnymailRequestsAPIError for HTTP errors in the post
"""
params = payload.get_request_params(self.api_url)
params.setdefault('timeout', self.timeout)
try:
response = self.session.request(**params)
except requests.RequestException as err: