retry.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright 2016–2021 Julien Danjou
  2. # Copyright 2016 Joshua Harlow
  3. # Copyright 2013-2014 Ray Holder
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. import abc
  17. import typing
  18. from tenacity import _utils
  19. from tenacity import retry_base
  20. if typing.TYPE_CHECKING:
  21. from tenacity import RetryCallState
  22. class async_retry_base(retry_base):
  23. """Abstract base class for async retry strategies."""
  24. @abc.abstractmethod
  25. async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
  26. pass
  27. def __and__( # type: ignore[override]
  28. self, other: "typing.Union[retry_base, async_retry_base]"
  29. ) -> "retry_all":
  30. return retry_all(self, other)
  31. def __rand__( # type: ignore[misc,override]
  32. self, other: "typing.Union[retry_base, async_retry_base]"
  33. ) -> "retry_all":
  34. return retry_all(other, self)
  35. def __or__( # type: ignore[override]
  36. self, other: "typing.Union[retry_base, async_retry_base]"
  37. ) -> "retry_any":
  38. return retry_any(self, other)
  39. def __ror__( # type: ignore[misc,override]
  40. self, other: "typing.Union[retry_base, async_retry_base]"
  41. ) -> "retry_any":
  42. return retry_any(other, self)
  43. RetryBaseT = typing.Union[
  44. async_retry_base, typing.Callable[["RetryCallState"], typing.Awaitable[bool]]
  45. ]
  46. class retry_if_exception(async_retry_base):
  47. """Retry strategy that retries if an exception verifies a predicate."""
  48. def __init__(
  49. self, predicate: typing.Callable[[BaseException], typing.Awaitable[bool]]
  50. ) -> None:
  51. self.predicate = predicate
  52. async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
  53. if retry_state.outcome is None:
  54. raise RuntimeError("__call__() called before outcome was set")
  55. if retry_state.outcome.failed:
  56. exception = retry_state.outcome.exception()
  57. if exception is None:
  58. raise RuntimeError("outcome failed but the exception is None")
  59. return await self.predicate(exception)
  60. else:
  61. return False
  62. class retry_if_result(async_retry_base):
  63. """Retries if the result verifies a predicate."""
  64. def __init__(
  65. self, predicate: typing.Callable[[typing.Any], typing.Awaitable[bool]]
  66. ) -> None:
  67. self.predicate = predicate
  68. async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
  69. if retry_state.outcome is None:
  70. raise RuntimeError("__call__() called before outcome was set")
  71. if not retry_state.outcome.failed:
  72. return await self.predicate(retry_state.outcome.result())
  73. else:
  74. return False
  75. class retry_any(async_retry_base):
  76. """Retries if any of the retries condition is valid."""
  77. def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None:
  78. self.retries = retries
  79. async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
  80. result = False
  81. for r in self.retries:
  82. result = result or await _utils.wrap_to_async_func(r)(retry_state)
  83. if result:
  84. break
  85. return result
  86. class retry_all(async_retry_base):
  87. """Retries if all the retries condition are valid."""
  88. def __init__(self, *retries: typing.Union[retry_base, async_retry_base]) -> None:
  89. self.retries = retries
  90. async def __call__(self, retry_state: "RetryCallState") -> bool: # type: ignore[override]
  91. result = True
  92. for r in self.retries:
  93. result = result and await _utils.wrap_to_async_func(r)(retry_state)
  94. if not result:
  95. break
  96. return result