Skip to content

Commit

Permalink
Also retry settings fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Aug 1, 2023
1 parent 08a543e commit 4ead577
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions mautrix_twitter/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations

from typing import TYPE_CHECKING, AsyncGenerator, AsyncIterable, Awaitable, cast
from typing import TYPE_CHECKING, AsyncGenerator, AsyncIterable, Awaitable, Callable, TypeVar, cast
import asyncio
import logging

Expand Down Expand Up @@ -76,6 +76,8 @@
}
)

T = TypeVar("T")


class User(DBUser, BaseUser):
by_mxid: dict[UserID, User] = {}
Expand Down Expand Up @@ -192,6 +194,23 @@ async def locked_connect(self, auth_token: str, csrf_token: str) -> None:
finally:
self._connect_task = None

async def _hacky_retry_loop(
self, fn: Callable[[], Awaitable[T]], action: str, max_retry_count: int = 5
) -> T:
retry_count = 0
while True:
try:
return await fn()
except aiohttp.ClientResponseError as e:
if e.status == 403 and retry_count < max_retry_count:
retry_count += 1
self.log.warning(
f"Unexpected 403 in {action}, retrying in 5 seconds", exc_info=True
)
await asyncio.sleep(5)
else:
raise

async def _connect(self, auth_token: str | None = None, csrf_token: str | None = None) -> None:
client = TwitterAPI(
log=logging.getLogger("mau.twitter.api").getChild(self.mxid),
Expand All @@ -202,20 +221,7 @@ async def _connect(self, auth_token: str | None = None, csrf_token: str | None =
client.set_tokens(auth_token or self.auth_token, csrf_token or self.csrf_token)

# Initial ping to make sure auth works
initial_ping_retry = 0
while True:
try:
await client.get_user_identifier()
break
except aiohttp.ClientResponseError as e:
if e.status == 403 and initial_ping_retry < 5:
initial_ping_retry += 1
self.log.warning(
"Unexpected 403 in initial ping, retrying in 5 seconds", exc_info=True
)
await asyncio.sleep(5)
else:
raise
await self._hacky_retry_loop(client.get_user_identifier, action="initial ping")

self.client = client
self.client.add_handler(Conversation, self.handle_conversation_update)
Expand All @@ -231,7 +237,7 @@ async def _connect(self, auth_token: str | None = None, csrf_token: str | None =
self.client.add_handler(PollingErrored, self.on_error)
self.client.add_handler(PollingErrorResolved, self.on_error_resolved)

user_info = await self.get_info()
user_info = await self._hacky_retry_loop(self.get_info, action="settings fetch")
self.twid = user_info.id
self._track_metric(METRIC_LOGGED_IN, True)
self.by_twid[self.twid] = self
Expand Down

0 comments on commit 4ead577

Please sign in to comment.