From ab5aeadd89f91f7359d3c95bd6114b8ef7ad7a89 Mon Sep 17 00:00:00 2001 From: hldh214 Date: Mon, 26 Feb 2024 16:30:11 +0900 Subject: [PATCH] Add retry mechanism to Buff request method(#28) Added a retry mechanism to the Buff class's request method, in the event of a BuffError exception, using the tenacity library. It is set to try a maximum of 5 times, utilizing a random exponential wait strategy with a minimum of 8 seconds and maximum of 60 seconds between attempts, enhancing error tolerance and stability of data fetching operations. Signed-off-by: hldh214 --- buff2steam/provider/buff.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/buff2steam/provider/buff.py b/buff2steam/provider/buff.py index c8223da..a62e86b 100644 --- a/buff2steam/provider/buff.py +++ b/buff2steam/provider/buff.py @@ -3,6 +3,7 @@ import time import httpx +import tenacity from buff2steam import logger from buff2steam.exceptions import BuffError @@ -38,6 +39,11 @@ async def __aenter__(self): async def __aexit__(self, exc_type, exc_val, exc_tb): await self.opener.aclose() + @tenacity.retry( + wait=tenacity.wait_random_exponential(multiplier=1, min=8, max=60), + stop=tenacity.stop_after_attempt(5), + retry=tenacity.retry_if_exception_type(BuffError) + ) async def request(self, *args, **kwargs) -> dict: url = kwargs.get('url', args[1]) if url not in self.request_locks: