Requests infinitely retry on connection now

This commit is contained in:
DevilXD committed 2022-07-23 12:32:08 +02:00
1 parent e7da8a2d13
commit 8018503d4f
1 file changed
+6 -26
+6 -26
View File
@@ -17,9 +17,7 @@ from gui import GUIManager
from channel import Channel from channel import Channel
from websocket import WebsocketPool from websocket import WebsocketPool
from inventory import DropsCampaign from inventory import DropsCampaign
from exceptions import ( from exceptions import MinerException, LoginException, CaptchaRequired, ExitRequest
MinerException, RequestException, LoginException, CaptchaRequired, ExitRequest
)
from utils import task_wrapper, timestamp, AwaitableValue, OrderedSet, ExponentialBackoff from utils import task_wrapper, timestamp, AwaitableValue, OrderedSet, ExponentialBackoff
from constants import ( from constants import (
BASE_URL, BASE_URL,
@@ -851,42 +849,24 @@ class Twitch:
@asynccontextmanager @asynccontextmanager
async def request( async def request(
self, method: str, url: str, *, attempts: int = 5, **kwargs self, method: str, url: str, **kwargs
) -> abc.AsyncIterator[aiohttp.ClientResponse]: ) -> abc.AsyncIterator[aiohttp.ClientResponse]:
session = await self.get_session() session = await self.get_session()
method = method.upper() method = method.upper()
if self.settings.proxy and "proxy" not in kwargs: if self.settings.proxy and "proxy" not in kwargs:
kwargs["proxy"] = self.settings.proxy kwargs["proxy"] = self.settings.proxy
logger.debug(f"Request: ({method=}, {url=}, {attempts=}, {kwargs=})") logger.debug(f"Request: ({method=}, {url=}, {kwargs=})")
attempt = 0 for delay in ExponentialBackoff(maximum=3*60):
cause: Exception | None = None
backoff = ExponentialBackoff(shift=4, maximum=3*60)
while attempt < attempts + 1:
delay: float | None = None
try: try:
async with session.request(method, url, **kwargs) as response: async with session.request(method, url, **kwargs) as response:
logger.debug(f"Response: {response.status}: {response}") logger.debug(f"Response: {response.status}: {response}")
if response.status < 500: if response.status < 500:
yield response yield response
return return
delay = next(backoff)
self.print(f"Twitch is down, retrying in {round(delay)} seconds...") self.print(f"Twitch is down, retrying in {round(delay)} seconds...")
except asyncio.TimeoutError: except (aiohttp.ClientConnectionError, asyncio.TimeoutError):
delay = next(backoff)
self.print(f"Cannot connect to Twitch, retrying in {round(delay)} seconds...") self.print(f"Cannot connect to Twitch, retrying in {round(delay)} seconds...")
except aiohttp.ClientConnectionError as exc: await asyncio.sleep(delay)
cause = exc
if delay is not None:
await asyncio.sleep(delay)
else:
await asyncio.sleep(0.1 * attempt)
attempt += 1
if "json" in kwargs and "password" in kwargs["json"]:
kwargs["json"]["password"] = "..."
raise RequestException(
"Ran out of attempts while handling a request: "
f"({method=}, {url=}, {attempts=}, {kwargs=})"
) from cause
async def gql_request(self, op: GQLOperation) -> JsonType: async def gql_request(self, op: GQLOperation) -> JsonType:
gql_logger.debug(f"GQL Request: {op}") gql_logger.debug(f"GQL Request: {op}")