| OLD | NEW |
| 1 # Copyright (C) 2010 Google Inc. All rights reserved. | 1 # Copyright (C) 2010 Google Inc. All rights reserved. |
| 2 # | 2 # |
| 3 # Redistribution and use in source and binary forms, with or without | 3 # Redistribution and use in source and binary forms, with or without |
| 4 # modification, are permitted provided that the following conditions are | 4 # modification, are permitted provided that the following conditions are |
| 5 # met: | 5 # met: |
| 6 # | 6 # |
| 7 # * Redistributions of source code must retain the above copyright | 7 # * Redistributions of source code must retain the above copyright |
| 8 # notice, this list of conditions and the following disclaimer. | 8 # notice, this list of conditions and the following disclaimer. |
| 9 # * Redistributions in binary form must reproduce the above | 9 # * Redistributions in binary form must reproduce the above |
| 10 # copyright notice, this list of conditions and the following disclaimer | 10 # copyright notice, this list of conditions and the following disclaimer |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | 28 |
| 29 import logging | 29 import logging |
| 30 import time | 30 import time |
| 31 import urllib2 | 31 import urllib2 |
| 32 | 32 |
| 33 _log = logging.getLogger(__name__) | 33 _log = logging.getLogger(__name__) |
| 34 | 34 |
| 35 | 35 |
| 36 class NetworkTimeout(Exception): | 36 class NetworkTimeout(Exception): |
| 37 |
| 37 def __str__(self): | 38 def __str__(self): |
| 38 return 'NetworkTimeout' | 39 return 'NetworkTimeout' |
| 39 | 40 |
| 40 | 41 |
| 41 class NetworkTransaction(object): | 42 class NetworkTransaction(object): |
| 43 |
| 42 def __init__(self, initial_backoff_seconds=10, grown_factor=1.5, timeout_sec
onds=(10 * 60), convert_404_to_None=False): | 44 def __init__(self, initial_backoff_seconds=10, grown_factor=1.5, timeout_sec
onds=(10 * 60), convert_404_to_None=False): |
| 43 self._initial_backoff_seconds = initial_backoff_seconds | 45 self._initial_backoff_seconds = initial_backoff_seconds |
| 44 self._grown_factor = grown_factor | 46 self._grown_factor = grown_factor |
| 45 self._timeout_seconds = timeout_seconds | 47 self._timeout_seconds = timeout_seconds |
| 46 self._convert_404_to_None = convert_404_to_None | 48 self._convert_404_to_None = convert_404_to_None |
| 47 | 49 |
| 48 def run(self, request): | 50 def run(self, request): |
| 49 self._total_sleep = 0 | 51 self._total_sleep = 0 |
| 50 self._backoff_seconds = self._initial_backoff_seconds | 52 self._backoff_seconds = self._initial_backoff_seconds |
| 51 while True: | 53 while True: |
| 52 try: | 54 try: |
| 53 return request() | 55 return request() |
| 54 except urllib2.HTTPError, e: | 56 except urllib2.HTTPError, e: |
| 55 if self._convert_404_to_None and e.code == 404: | 57 if self._convert_404_to_None and e.code == 404: |
| 56 return None | 58 return None |
| 57 self._check_for_timeout() | 59 self._check_for_timeout() |
| 58 _log.warn("Received HTTP status %s loading \"%s\". Retrying in
%s seconds..." % (e.code, e.filename, self._backoff_seconds)) | 60 _log.warn("Received HTTP status %s loading \"%s\". Retrying in
%s seconds..." % |
| 61 (e.code, e.filename, self._backoff_seconds)) |
| 59 self._sleep() | 62 self._sleep() |
| 60 | 63 |
| 61 def _check_for_timeout(self): | 64 def _check_for_timeout(self): |
| 62 if self._total_sleep + self._backoff_seconds > self._timeout_seconds: | 65 if self._total_sleep + self._backoff_seconds > self._timeout_seconds: |
| 63 raise NetworkTimeout() | 66 raise NetworkTimeout() |
| 64 | 67 |
| 65 def _sleep(self): | 68 def _sleep(self): |
| 66 time.sleep(self._backoff_seconds) | 69 time.sleep(self._backoff_seconds) |
| 67 self._total_sleep += self._backoff_seconds | 70 self._total_sleep += self._backoff_seconds |
| 68 self._backoff_seconds *= self._grown_factor | 71 self._backoff_seconds *= self._grown_factor |
| OLD | NEW |