add REQUESTS_TIMEOUT settings for utils.request (#20839)

This commit is contained in:
Thomas NOËL 2017-12-21 01:42:05 +01:00 committed by Frédéric Péters
parent b4a7305ba9
commit b40c0f894f
5 changed files with 12 additions and 5 deletions

View File

@ -14,6 +14,6 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='jsoncell',
name='timeout',
field=models.PositiveIntegerField(default=28, verbose_name='Request timeout'),
field=models.PositiveIntegerField(default=0, verbose_name='Request timeout'),
),
]

View File

@ -876,7 +876,7 @@ class JsonCellBase(CellBase):
varnames = None
force_async = False
log_errors = True
timeout = 28 # combo wsgi service timeout - 2 seconds
timeout = None
actions = {}
additional_data = None
# [
@ -1050,7 +1050,8 @@ class JsonCell(JsonCellBase):
varnames_str = models.CharField(_('Variable names'), max_length=200, blank=True,
help_text=_('Comma separated list of query-string variables '
'to be copied in template context'))
timeout = models.PositiveIntegerField(_('Request timeout'), default=28)
timeout = models.PositiveIntegerField(_('Request timeout'), default=0,
help_text=_('In seconds. Use 0 for default system timeout'))
class Meta:
verbose_name = _('JSON Feed')

View File

@ -279,6 +279,10 @@ COMBO_MAP_TILE_URLTEMPLATE = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png
# default combo map attribution
COMBO_MAP_ATTRIBUTION = 'Map data &copy; <a href="https://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
# timeout used in python-requests call, in seconds
# we use 28s by default: timeout just before web server, which is usually 30s
REQUESTS_TIMEOUT = 28
local_settings_file = os.environ.get('COMBO_SETTINGS_FILE',
os.path.join(os.path.dirname(__file__), 'local_settings.py'))
if os.path.exists(local_settings_file):

View File

@ -164,6 +164,8 @@ class Requests(RequestsSession):
if remote_service: # sign
url = sign_url(url, remote_service.get('secret'))
kwargs['timeout'] = kwargs.get('timeout') or settings.REQUESTS_TIMEOUT
response = super(Requests, self).request(method, url, **kwargs)
if log_errors and (response.status_code // 100 != 2):
logging.error('failed to %s %s (%s)', method, url, response.status_code)

View File

@ -475,7 +475,7 @@ def test_config_json_cell_additional_url(app):
assert len(requests_get.mock_calls) == 2
assert requests_get.mock_calls[0][1][0] == 'http://foo'
assert requests_get.mock_calls[0][-1]['log_errors'] == True
assert requests_get.mock_calls[0][-1]['timeout'] == 28
assert requests_get.mock_calls[0][-1]['timeout'] == None
assert requests_get.mock_calls[1][1][0] == 'http://bar'
assert requests_get.mock_calls[1][-1]['log_errors'] == False
assert requests_get.mock_calls[1][-1]['timeout'] == 42
@ -544,7 +544,7 @@ def test_config_json_cell_additional_url(app):
assert len(requests_get.mock_calls) == 3
assert requests_get.mock_calls[0][1][0] == 'http://foo'
assert requests_get.mock_calls[0][-1]['log_errors'] == True
assert requests_get.mock_calls[0][-1]['timeout'] == 28
assert requests_get.mock_calls[0][-1]['timeout'] == None
assert requests_get.mock_calls[1][1][0] == 'http://bar'
assert requests_get.mock_calls[1][-1]['log_errors'] == False
assert requests_get.mock_calls[1][-1]['timeout'] == 42