@@ -70,55 +70,57 @@ if __name__ == "__main__":
7070```
7171
7272# ## Retrier
73+
7374```python
7475import asyncio
7576import logging
7677
7778import httpx
7879import tenacity
7980
80- from circuit_breaker_box.retryer import Retrier
81-
81+ from circuit_breaker_box.retrier import Retrier
8282
8383MAX_RETRIES = 4
8484SOME_HOST = " http://example.com/"
8585
8686
8787async def main() -> None :
88- """
89- Use Retrier with tenacity adjustments to automatically retry failed operations raising specific exceptions like:
90- stop_rule
91- retry_cause
92- wait_strategy
88+ """
89+ Use Retrier with tenacity adjustments to automatically retry failed operations raising specific exceptions like:
90+ stop_rule
91+ retry_cause
92+ wait_strategy
9393
94- `foo` as example function will be retried immediately (no wait) when it raises ZeroDivisionError up to MAX_RETRIES
95- After exceeding MAX_RETRIES attempts, the exception will propagate.
96- """
97- logging.basicConfig(level = logging.DEBUG )
98- retryer = Retrier[httpx.Response](
99- stop_rule = tenacity.stop.stop_after_attempt(MAX_RETRIES ),
100- retry_cause = tenacity.retry_if_exception_type(ZeroDivisionError ),
101- wait_strategy = tenacity.wait_none(),
102- )
103- example_request = httpx.Request(" GET" , httpx.URL(SOME_HOST ))
94+ `foo` as example function will be retried immediately (no wait) when it raises ZeroDivisionError up to MAX_RETRIES
95+ After exceeding MAX_RETRIES attempts, the exception will propagate.
96+ """
97+ logging.basicConfig(level = logging.DEBUG )
98+ retryer = Retrier[httpx.Response](
99+ stop_rule = tenacity.stop.stop_after_attempt(MAX_RETRIES ),
100+ retry_cause = tenacity.retry_if_exception_type(ZeroDivisionError ),
101+ wait_strategy = tenacity.wait_none(),
102+ )
103+ example_request = httpx.Request(" GET" , httpx.URL(SOME_HOST ))
104104
105- async def foo(request: httpx.Request) -> httpx.Response:
106- raise ZeroDivisionError (request)
105+ async def foo(request: httpx.Request) -> httpx.Response:
106+ raise ZeroDivisionError (request)
107107
108- await retryer.retry(foo, request = example_request)
108+ await retryer.retry(foo, request = example_request)
109109
110110
111111if __name__ == " __main__" :
112- asyncio.run(main())
113-
114-
115- >> > INFO :circuit_breaker_box.retryer:Attempt: attempt_number: 1 , outcome_timestamp: None
116- >> > INFO :circuit_breaker_box.retryer:Attempt: attempt_number: 2 , outcome_timestamp: None
117- >> > INFO :circuit_breaker_box.retryer:Attempt: attempt_number: 3 , outcome_timestamp: None
118- >> > INFO :circuit_breaker_box.retryer:Attempt: attempt_number: 4 , outcome_timestamp: None
119- >> > Traceback (most recent call last):
120- >> > ...
121- >> > ZeroDivisionError : < Request(' GET' , ' http://example.com/' )>
112+ asyncio.run(main())
113+
114+ >> > INFO : circuit_breaker_box.retryer:Attempt: attempt_number: 1 , outcome_timestamp: None
115+ >> > INFO : circuit_breaker_box.retryer:Attempt: attempt_number: 2 , outcome_timestamp: None
116+ >> > INFO : circuit_breaker_box.retryer:Attempt: attempt_number: 3 , outcome_timestamp: None
117+ >> > INFO : circuit_breaker_box.retryer:Attempt: attempt_number: 4 , outcome_timestamp: None
118+ >> > Traceback(most
119+ recent
120+ call
121+ last):
122+ >> > ...
123+ >> > ZeroDivisionError : < Request(' GET' , ' http://example.com/' ) >
122124```
123125
124126# ## Retrier with CircuitBreaker
0 commit comments