Simplify Backend.get_profile

This commit is contained in:
miruka 2019-11-12 08:34:08 -04:00
parent 3ebb11aa69
commit 290d3e9f6b

View File

@ -1,6 +1,5 @@
import asyncio import asyncio
import logging as log import logging as log
import random
from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union from typing import Any, DefaultDict, Dict, List, Optional, Tuple, Union
import hsluv import hsluv
@ -111,14 +110,6 @@ class Backend:
await self.saved_accounts.delete(user_id) await self.saved_accounts.delete(user_id)
async def wait_until_any_client_exists(self) -> None:
while True:
if self.clients:
return
await asyncio.sleep(0.1)
async def wait_until_client_exists(self, user_id: str) -> None: async def wait_until_client_exists(self, user_id: str) -> None:
loops = 0 loops = 0
while True: while True:
@ -183,13 +174,13 @@ class Backend:
return self.profile_cache[user_id] return self.profile_cache[user_id]
async with self.get_profile_locks[user_id]: async with self.get_profile_locks[user_id]:
if not self.clients: while True:
await self.wait_until_any_client_exists() try:
client = next(c for c in self.clients.values())
client = self.clients.get( break
user_id, except StopIteration:
random.choice(tuple(self.clients.values())), # Retry after a bit if no client was present yet
) await asyncio.sleep(0.1)
response = await client.get_profile(user_id) response = await client.get_profile(user_id)