| 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 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 | 35 |
| 36 class NetworkTimeout(Exception): | 36 class NetworkTimeout(Exception): |
| 37 | 37 |
| 38 def __str__(self): | 38 def __str__(self): |
| 39 return 'NetworkTimeout' | 39 return 'NetworkTimeout' |
| 40 | 40 |
| 41 | 41 |
| 42 class NetworkTransaction(object): | 42 class NetworkTransaction(object): |
| 43 | 43 |
| 44 # TODO(qyearsley): Rename convert_404_to_None | 44 # TODO(qyearsley): Rename return_none_on_404 |
| 45 # pylint: disable=invalid-name | 45 # pylint: disable=invalid-name |
| 46 | 46 |
| 47 def __init__(self, initial_backoff_seconds=10, grown_factor=1.5, timeout_sec
onds=(10 * 60), convert_404_to_None=False): | 47 def __init__(self, initial_backoff_seconds=10, grown_factor=1.5, timeout_sec
onds=(10 * 60), return_none_on_404=False): |
| 48 self._initial_backoff_seconds = initial_backoff_seconds | 48 self._initial_backoff_seconds = initial_backoff_seconds |
| 49 self._grown_factor = grown_factor | 49 self._grown_factor = grown_factor |
| 50 self._timeout_seconds = timeout_seconds | 50 self._timeout_seconds = timeout_seconds |
| 51 self._convert_404_to_None = convert_404_to_None | 51 self._return_none_on_404 = return_none_on_404 |
| 52 self._total_sleep = 0 | 52 self._total_sleep = 0 |
| 53 self._backoff_seconds = 0 | 53 self._backoff_seconds = 0 |
| 54 | 54 |
| 55 def run(self, request): | 55 def run(self, request): |
| 56 self._total_sleep = 0 | 56 self._total_sleep = 0 |
| 57 self._backoff_seconds = self._initial_backoff_seconds | 57 self._backoff_seconds = self._initial_backoff_seconds |
| 58 while True: | 58 while True: |
| 59 try: | 59 try: |
| 60 return request() | 60 return request() |
| 61 except urllib2.HTTPError as error: | 61 except urllib2.HTTPError as error: |
| 62 if self._convert_404_to_None and error.code == 404: | 62 if self._return_none_on_404 and error.code == 404: |
| 63 return None | 63 return None |
| 64 self._check_for_timeout() | 64 self._check_for_timeout() |
| 65 _log.warning("Received HTTP status %s loading \"%s\". Retrying
in %s seconds...", | 65 _log.warning("Received HTTP status %s loading \"%s\". Retrying
in %s seconds...", |
| 66 error.code, error.filename, self._backoff_seconds) | 66 error.code, error.filename, self._backoff_seconds) |
| 67 self._sleep() | 67 self._sleep() |
| 68 | 68 |
| 69 def _check_for_timeout(self): | 69 def _check_for_timeout(self): |
| 70 if self._total_sleep + self._backoff_seconds > self._timeout_seconds: | 70 if self._total_sleep + self._backoff_seconds > self._timeout_seconds: |
| 71 raise NetworkTimeout() | 71 raise NetworkTimeout() |
| 72 | 72 |
| 73 def _sleep(self): | 73 def _sleep(self): |
| 74 time.sleep(self._backoff_seconds) | 74 time.sleep(self._backoff_seconds) |
| 75 self._total_sleep += self._backoff_seconds | 75 self._total_sleep += self._backoff_seconds |
| 76 self._backoff_seconds *= self._grown_factor | 76 self._backoff_seconds *= self._grown_factor |
| OLD | NEW |