From 196dfd09d5a44dd432fd13ab87e93b87af7a8f64 Mon Sep 17 00:00:00 2001 From: Brian Korty Date: Mon, 7 Aug 2017 13:08:20 -0400 Subject: [PATCH] Add HTTP_MAX_CLIENTS setting. Add in support for the HTTP_MAX_CLIENTS environment variable to override the default tornado settings. --- README.rst | 9 +++++++++ sprockets/mixins/http/__init__.py | 8 ++++++++ tests.py | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index bad252f..d9b0fd4 100644 --- a/README.rst +++ b/README.rst @@ -84,6 +84,15 @@ As with Tornado, to use the curl client which has numerous benefits: app.listen(8000) ioloop.IOLoop.current().start() +Environment Variables +--------------------- + ++------------------+----------------------------------------------------------+ +| HTTP_MAX_CLIENTS | An optional setting that specifies the maximum number of | +| | simultaneous asynchronous HTTP requests. If not | +| | specified, the default Tornado value of 10 will be used. | ++------------------+----------------------------------------------------------+ + License ------- ``sprockets.mixins.http`` is released under the `3-Clause BSD license `_. diff --git a/sprockets/mixins/http/__init__.py b/sprockets/mixins/http/__init__.py index 7b23a4c..e7a6628 100644 --- a/sprockets/mixins/http/__init__.py +++ b/sprockets/mixins/http/__init__.py @@ -8,6 +8,7 @@ requests. import collections import json import logging +import os import socket import time @@ -22,6 +23,7 @@ LOGGER = logging.getLogger(__name__) CONTENT_TYPE_JSON = headers.parse_content_type('application/json') CONTENT_TYPE_MSGPACK = headers.parse_content_type('application/msgpack') DEFAULT_USER_AGENT = 'sprockets.mixins.http/{}'.format(__version__) +DEFAULT_MAX_CLIENTS = 10 HTTPResponse = collections.namedtuple( @@ -99,7 +101,13 @@ class HTTPClientMixin(object): if body: body = self._http_req_body_serialize( body, request_headers['Content-Type']) + client = httpclient.AsyncHTTPClient() + + # Workaround for Tornado defect. + if hasattr(client, 'max_clients') and os.getenv('HTTP_MAX_CLIENTS'): + client.max_clients = int(os.getenv('HTTP_MAX_CLIENTS')) + response, start_time = None, time.time() for attempt in range(0, self.MAX_HTTP_RETRIES): LOGGER.debug('%s %s (Attempt %i of %i) %r', diff --git a/tests.py b/tests.py index abbadc3..2d2ddd8 100644 --- a/tests.py +++ b/tests.py @@ -1,8 +1,9 @@ import json import logging +import os import uuid -from tornado import httputil, testing, web +from tornado import httpclient, httputil, testing, web import mock import umsgpack @@ -400,6 +401,15 @@ class MixinTestCase(testing.AsyncHTTPTestCase): method='DELETE', body={'foo': 'bar', 'status_code': 200}, allow_nonstandard_methods=True) - self.assertTrue(response.ok) - self.assertEqual(response.code, 200) + + @testing.gen_test() + def test_max_clients_settings_supported(self): + os.environ['HTTP_MAX_CLIENTS'] = '25' + response = yield self.mixin.http_fetch( + self.get_url('/test?foo=bar&status_code=200')) + self.assertTrue(response.ok) + del os.environ['HTTP_MAX_CLIENTS'] + client = httpclient.AsyncHTTPClient() + self.assertEqual(client.max_clients, 25) +