Browse Source

HUE-7567 [all] Add default timeout to REST calls

jdesjean 8 năm trước cách đây
mục cha
commit
01c6d38

+ 2 - 1
apps/jobbrowser/src/jobbrowser/models.py

@@ -24,6 +24,7 @@ import urllib2
 from urlparse import urlparse, urlunparse
 
 from django.core.urlresolvers import reverse
+from desktop.conf import REST_CONN_TIMEOUT
 from desktop.lib.view_util import format_duration_in_millis
 from desktop.lib import i18n
 from django.utils.html import escape
@@ -477,7 +478,7 @@ class TaskAttempt(object):
                       None))
     LOG.info('Retrieving %s' % (url,))
     try:
-      data = urllib2.urlopen(url)
+      data = urllib2.urlopen(url, timeout=REST_CONN_TIMEOUT.get())
     except urllib2.URLError:
       raise urllib2.URLError(_("Cannot retrieve logs from TaskTracker %(id)s.") % {'id': self.taskTrackerId})
 

+ 5 - 4
apps/zookeeper/src/zookeeper/rest.py

@@ -20,6 +20,7 @@ import urllib
 import urllib2
 
 from contextlib import contextmanager
+from desktop.conf import REST_CONN_TIMEOUT
 
 
 class RequestWithMethod(urllib2.Request):
@@ -173,7 +174,7 @@ class ZooKeeper(object):
         try:
             req = urllib2.Request(uri)
             req.add_header("Accept", "application/json");
-            r = urllib2.urlopen(req)
+            r = urllib2.urlopen(req, timeout=REST_CONN_TIMEOUT.get())
             resp = json.load(r)
 
             if 'Error' in resp:
@@ -193,7 +194,7 @@ class ZooKeeper(object):
             if data is not None:
                 req.add_data(data)
 
-            resp = json.load(urllib2.urlopen(req))
+            resp = json.load(urllib2.urlopen(req, timeout=REST_CONN_TIMEOUT.get()))
             if 'Error' in resp:
                 raise ZooKeeper.Error(resp['Error'])
             return resp
@@ -212,7 +213,7 @@ class ZooKeeper(object):
         req = RequestWithMethod(uri)
         req.set_method('DELETE')
         req.add_header('Content-Type', 'application/octet-stream')
-        return urllib2.urlopen(req).read()
+        return urllib2.urlopen(req, timeout=REST_CONN_TIMEOUT.get()).read()
 
     def _do_put(self, uri, data):
         """ Send a PUT request """
@@ -223,7 +224,7 @@ class ZooKeeper(object):
             if data is not None:
                 req.add_data(data)
 
-            return urllib2.urlopen(req).read()
+            return urllib2.urlopen(req, timeout=REST_CONN_TIMEOUT.get()).read()
         except urllib2.HTTPError, e:
             if e.code == 412: # precondition failed
                 raise ZooKeeper.WrongVersion(uri)

+ 3 - 0
desktop/conf.dist/hue.ini

@@ -209,6 +209,9 @@
   # Size in KB/MB/GB for audit log to rollover.
   ## audit_log_max_file_size=100MB
 
+  # Timeout in seconds for REST calls.
+  ## rest_conn_timeout=120
+
   # A json file containing a list of log redaction rules for cleaning sensitive data
   # from log files. It is defined as:
   #

+ 3 - 0
desktop/conf/pseudo-distributed.ini.tmpl

@@ -213,6 +213,9 @@
   # Size in KB/MB/GB for audit log to rollover.
   ## audit_log_max_file_size=100MB
 
+  # Timeout in seconds for REST calls.
+  ## rest_conn_timeout=120
+
   # A json file containing a list of log redaction rules for cleaning sensitive data
   # from log files. It is defined as:
   #

+ 6 - 0
desktop/core/src/desktop/conf.py

@@ -471,6 +471,12 @@ ALLOWED_HOSTS = Config(
   help=_('Comma separated list of strings representing the host/domain names that the Hue server can serve.')
 )
 
+REST_CONN_TIMEOUT = Config(
+  key='rest_conn_timeout',
+  default=120,
+  type=int,
+  help=_('Timeout in seconds for REST calls.'))
+
 VCS = UnspecifiedConfigSection(
   "vcs",
   help="One entry for each Version Control",

+ 2 - 2
desktop/core/src/desktop/lib/rest/http_client.py

@@ -151,7 +151,7 @@ class HttpClient(object):
     return self._session.headers.copy()
 
   def execute(self, http_method, path, params=None, data=None, headers=None, allow_redirects=False, urlencode=True,
-              files=None, clear_cookies=False):
+              files=None, clear_cookies=False, timeout=conf.REST_CONN_TIMEOUT.get()):
     """
     Submit an HTTP request.
     @param http_method: GET, POST, PUT, DELETE
@@ -175,7 +175,7 @@ class HttpClient(object):
         self.logger.warn("GET and DELETE methods do not pass any data. Path '%s'" % path)
         data = None
 
-    request_kwargs = {'allow_redirects': allow_redirects}
+    request_kwargs = {'allow_redirects': allow_redirects, 'timeout': timeout}
     if headers:
       request_kwargs['headers'] = headers
     if data: