Browse Source

HUE-9574 [core] Apply timeout to thrift-over-http calls (#1430)

Ying Chen 5 năm trước cách đây
mục cha
commit
df2fd5862e

+ 17 - 12
desktop/core/src/desktop/lib/rest/http_client.py

@@ -27,7 +27,7 @@ from django.utils.encoding import iri_to_uri, smart_str
 from django.utils.http import urlencode
 
 from requests import exceptions
-from requests.auth import AuthBase ,HTTPBasicAuth, HTTPDigestAuth
+from requests.auth import AuthBase, HTTPBasicAuth, HTTPDigestAuth
 from requests_kerberos import HTTPKerberosAuth, REQUIRED, OPTIONAL, DISABLED
 from urllib3.contrib import pyopenssl
 
@@ -58,7 +58,8 @@ def get_request_session(url, logger):
     with CACHE_SESSION_LOCK:
       CACHE_SESSION[url] = requests.Session()
       logger.debug("Setting request Session")
-      CACHE_SESSION[url].mount(url, requests.adapters.HTTPAdapter(pool_connections=conf.CHERRYPY_SERVER_THREADS.get(), pool_maxsize=conf.CHERRYPY_SERVER_THREADS.get()))
+      CACHE_SESSION[url].mount(url, requests.adapters.HTTPAdapter(pool_connections=conf.CHERRYPY_SERVER_THREADS.get(),
+                                                                  pool_maxsize=conf.CHERRYPY_SERVER_THREADS.get()))
       logger.debug("Setting session adapter for %s" % url)
 
   return CACHE_SESSION
@@ -199,6 +200,10 @@ class HttpClient(object):
         self.logger.warn("GET and DELETE methods do not pass any data. Path '%s'" % path)
         data = None
 
+    if headers and 'timeout' in headers:
+      timeout = int(headers['timeout'])
+      LOG.debug("Overriding timeout xxx via header value %d" % timeout)
+
     request_kwargs = {'allow_redirects': allow_redirects, 'timeout': timeout}
     if headers:
       request_kwargs['headers'] = headers
@@ -239,17 +244,17 @@ class HttpClient(object):
 
 
 class HTTPBearerAuth(AuthBase):
-    """Attaches HTTP Basic Authentication to the given Request object."""
+  """Attaches HTTP Basic Authentication to the given Request object."""
 
-    def __init__(self, token):
-        self.token = token
+  def __init__(self, token):
+    self.token = token
 
-    def __eq__(self, other):
-        return self.token == getattr(other, 'token', None)
+  def __eq__(self, other):
+    return self.token == getattr(other, 'token', None)
 
-    def __ne__(self, other):
-        return not self == other
+  def __ne__(self, other):
+    return not self == other
 
-    def __call__(self, r):
-        r.headers['Authorization'] = 'Bearer %s' % self.token
-        return r
+  def __call__(self, r):
+    r.headers['Authorization'] = 'Bearer %s' % self.token
+    return r

+ 3 - 1
desktop/core/src/desktop/lib/thrift_/http_client.py

@@ -72,7 +72,9 @@ class THttpClient(TTransportBase):
     return self._client is not None
 
   def setTimeout(self, ms):
-    pass
+    if not self._headers:
+      self._headers = {}
+    self._headers.update(timeout=str(int(ms / 1000)))
 
   def setCustomHeaders(self, headers):
     self._headers = headers