| OLD | NEW |
| 1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
| 4 | 4 |
| 5 """ | 5 """ |
| 6 Function/method decorators that provide timeout and retry logic. | 6 Function/method decorators that provide timeout and retry logic. |
| 7 """ | 7 """ |
| 8 | 8 |
| 9 import functools | 9 import functools |
| 10 import sys | 10 import sys |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 """ | 109 """ |
| 110 def decorator(f): | 110 def decorator(f): |
| 111 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) | 111 get_timeout = lambda *a, **kw: kw.get('timeout', default_timeout) |
| 112 get_retries = lambda *a, **kw: kw.get('retries', default_retries) | 112 get_retries = lambda *a, **kw: kw.get('retries', default_retries) |
| 113 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 113 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 114 return decorator | 114 return decorator |
| 115 | 115 |
| 116 | 116 |
| 117 def WithTimeoutAndRetriesFromInstance( | 117 def WithTimeoutAndRetriesFromInstance( |
| 118 default_timeout_name=DEFAULT_TIMEOUT_ATTR, | 118 default_timeout_name=DEFAULT_TIMEOUT_ATTR, |
| 119 default_retries_name=DEFAULT_RETRIES_ATTR): | 119 default_retries_name=DEFAULT_RETRIES_ATTR, |
| 120 min_default_timeout=None): |
| 120 """Returns a decorator that handles timeouts and retries. | 121 """Returns a decorator that handles timeouts and retries. |
| 121 | 122 |
| 122 The provided |default_timeout_name| and |default_retries_name| are used to | 123 The provided |default_timeout_name| and |default_retries_name| are used to |
| 123 get the default timeout value and the default retries value from the object | 124 get the default timeout value and the default retries value from the object |
| 124 instance if timeout and retries values are not provided. | 125 instance if timeout and retries values are not provided. |
| 125 | 126 |
| 126 Note that this should only be used to decorate methods, not functions. | 127 Note that this should only be used to decorate methods, not functions. |
| 127 | 128 |
| 128 Args: | 129 Args: |
| 129 default_timeout_name: The name of the default timeout attribute of the | 130 default_timeout_name: The name of the default timeout attribute of the |
| 130 instance. | 131 instance. |
| 131 default_retries_name: The name of the default retries attribute of the | 132 default_retries_name: The name of the default retries attribute of the |
| 132 instance. | 133 instance. |
| 134 min_timeout: Miniumum timeout to be used when using instance timeout. |
| 133 Returns: | 135 Returns: |
| 134 The actual decorator. | 136 The actual decorator. |
| 135 """ | 137 """ |
| 136 def decorator(f): | 138 def decorator(f): |
| 137 def get_timeout(inst, *_args, **kwargs): | 139 def get_timeout(inst, *_args, **kwargs): |
| 138 return kwargs.get('timeout', getattr(inst, default_timeout_name)) | 140 ret = getattr(inst, default_timeout_name) |
| 141 if min_default_timeout is not None: |
| 142 ret = max(min_default_timeout, ret) |
| 143 return kwargs.get('timeout', ret) |
| 139 def get_retries(inst, *_args, **kwargs): | 144 def get_retries(inst, *_args, **kwargs): |
| 140 return kwargs.get('retries', getattr(inst, default_retries_name)) | 145 return kwargs.get('retries', getattr(inst, default_retries_name)) |
| 141 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) | 146 return _TimeoutRetryWrapper(f, get_timeout, get_retries, pass_values=True) |
| 142 return decorator | 147 return decorator |
| 143 | 148 |
| OLD | NEW |