You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@openwhisk.apache.org by GitBox <gi...@apache.org> on 2018/02/20 05:29:15 UTC

[GitHub] csantanapr closed pull request #3301: Add HTTP request timeout.

csantanapr closed pull request #3301: Add HTTP request timeout.
URL: https://github.com/apache/incubator-openwhisk/pull/3301
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/tools/admin/wskutil.py b/tools/admin/wskutil.py
index 06b00d1fbc..59320d1121 100644
--- a/tools/admin/wskutil.py
+++ b/tools/admin/wskutil.py
@@ -24,21 +24,22 @@
 import httplib
 import ssl
 import base64
+import socket
 from urlparse import urlparse
 
 # global configurations, can control whether to allow untrusted certificates
 # on HTTPS connections
 httpRequestProps = {'secure': True}
 
-def request(method, urlString, body = '', headers = {}, auth = None, verbose = False, https_proxy = os.getenv('https_proxy', None)):
+def request(method, urlString, body = '', headers = {}, auth = None, verbose = False, https_proxy = os.getenv('https_proxy', None), timeout = 10):
     url = urlparse(urlString)
     if url.scheme == 'http':
-        conn = httplib.HTTPConnection(url.netloc)
+        conn = httplib.HTTPConnection(url.netloc, timeout = timeout)
     else:
         if httpRequestProps['secure'] or not hasattr(ssl, '_create_unverified_context'):
-            conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy)
+            conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy, timeout = timeout)
         else:
-            conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy, context=ssl._create_unverified_context())
+            conn = httplib.HTTPSConnection(url.netloc if https_proxy is None else https_proxy, context=ssl._create_unverified_context(), timeout = timeout)
         if https_proxy:
             conn.set_tunnel(url.netloc)
 
@@ -77,28 +78,21 @@ def request(method, urlString, body = '', headers = {}, auth = None, verbose = F
             print(res.read())
             print('========')
         return res
+    except socket.timeout:
+        return ErrorResponse(status = 500, error = 'request timed out at %d seconds' % timeout)
     except Exception as e:
-        res = dict2obj({ 'status' : 500, 'error': str(e) })
-        return res
+        return ErrorResponse(status = 500, error = str(e))
 
 
 def getPrettyJson(obj):
     return json.dumps(obj, sort_keys=True, indent=4, separators=(',', ': '))
 
 
-# class to convert dictionary to objects
-class dict2obj(dict):
-    def __getattr__(self, name):
-        if name in self:
-            return self[name]
-        else:
-            raise AttributeError('object has no attribute "%s"' % name)
-
-    def __setattr__(self, name, value):
-        self[name] = value
+# class to normalize responses for exceptions with no HTTP response for canonical error handling
+class ErrorResponse:
+    def __init__(self, status, error):
+        self.status = status
+        self.error = error
 
-    def __delattr__(self, name):
-        if name in self:
-            del self[name]
-        else:
-            raise AttributeError('object has no attribute "%s"' % name)
+    def read(self):
+        return self.error


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services