You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2013/08/26 21:11:18 UTC

[7/7] git commit: [#6531] Added timeout param to urlopen and fixed retries count logging

[#6531] Added timeout param to urlopen and fixed retries count logging

Signed-off-by: Cory Johns <cj...@slashdotmedia.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/8c6605d3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/8c6605d3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/8c6605d3

Branch: refs/heads/master
Commit: 8c6605d3f32a10b6bd2bd5c19834064c0cdbae34
Parents: 0688afc
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Mon Aug 26 19:09:10 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Mon Aug 26 19:09:10 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/helpers.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8c6605d3/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 8095f79..e545289 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -932,7 +932,7 @@ class exceptionless(object):
         return inner
 
 
-def urlopen(url, retries=3, codes=(408,)):
+def urlopen(url, retries=3, codes=(408,), timeout=None):
     """Open url, optionally retrying if an error is encountered.
 
     Socket timeouts will always be retried if retries > 0.
@@ -942,20 +942,23 @@ def urlopen(url, retries=3, codes=(408,)):
     :param codes: HTTP error codes that should be retried.
 
     """
+    attempts = 0
     while True:
         try:
-            return urllib2.urlopen(url)
+            return urllib2.urlopen(url, timeout=timeout)
         except (urllib2.HTTPError, socket.timeout) as e:
-            if retries and (isinstance(e, socket.timeout) or
+            attempts += 1
+            if attempts < retries and (isinstance(e, socket.timeout) or
                     e.code in codes):
-                retries -= 1
                 continue
             else:
                 try:
                     url_string = url.get_full_url()  # if url is Request obj
                 except Exception:
                     url_string = url
-                log.exception('Failed after %s retries on url: %s: %s', retries, url_string, e)
+                if timeout is None:
+                    timeout = socket.getdefaulttimeout()
+                log.exception('Failed after %s retries on url with a timeout of %s: %s: %s', attempts, timeout, url_string, e)
                 raise e