Skip to content

Commit bb08f39

Browse files
committed
Start token validator when a token is actually added; removes a unnecessary async call
1 parent b8fefda commit bb08f39

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

twitchio/authentication/tokens.py

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,14 @@ def __init__(
8282

8383
self._tokens: TokenMapping = {}
8484
self._app_token: str | None = None
85-
self._validate_task: asyncio.Task[None] | None = None
8685
self._nested_key: str | None = None
8786

8887
self._token_lock: asyncio.Lock = asyncio.Lock()
8988
self._has_loaded: bool = False
9089
self._backoff: Backoff = Backoff(base=3, maximum_time=90)
90+
9191
self._validated_event: asyncio.Event = asyncio.Event()
92+
self._validate_task: asyncio.Task[None] | None = None
9293

9394
async def _attempt_refresh_on_add(self, token: str, refresh: str) -> ValidateTokenPayload:
9495
logger.debug("Token was invalid when attempting to add it to the token manager. Attempting to refresh.")
@@ -122,6 +123,9 @@ async def _attempt_refresh_on_add(self, token: str, refresh: str) -> ValidateTok
122123
return valid_resp
123124

124125
async def add_token(self, token: str, refresh: str) -> ValidateTokenPayload:
126+
if not self._validate_task:
127+
self._validate_task = asyncio.create_task(self.__validate_loop())
128+
125129
try:
126130
resp: ValidateTokenPayload = await self.__isolated.validate_token(token)
127131
except HTTPException as e:
@@ -171,9 +175,6 @@ def _find_token(self, route: Route) -> TokenMappingData | None | str:
171175
return token or self._app_token
172176

173177
async def request(self, route: Route) -> RawResponse | str | None:
174-
if not self._session:
175-
await self._init_session()
176-
177178
old: TokenMappingData | None | str = self._find_token(route)
178179
if old:
179180
token: str = old if isinstance(old, str) else old["token"]
@@ -266,9 +267,6 @@ async def _revalidate_all(self) -> None:
266267
async def __validate_loop(self) -> None:
267268
logger.debug("Started the token validation loop on %s.", self.__class__.__qualname__)
268269

269-
if not self._session:
270-
await self._init_session()
271-
272270
while True:
273271
self._validated_event.clear()
274272

@@ -284,12 +282,6 @@ async def __validate_loop(self) -> None:
284282
self._validated_event.set()
285283
await asyncio.sleep(60)
286284

287-
async def _init_session(self) -> None:
288-
await super()._init_session()
289-
290-
if not self._validate_task:
291-
self._validate_task = asyncio.create_task(self.__validate_loop())
292-
293285
def cleanup(self) -> None:
294286
self._tokens.clear()
295287

0 commit comments

Comments
 (0)