Simplify pinging & properly close aiohttp session

This commit is contained in:
miruka 2020-08-22 13:19:38 -04:00
parent f327c99a99
commit 9cc9229fdb
2 changed files with 9 additions and 11 deletions

View File

@ -114,8 +114,6 @@ class Backend:
self._sso_server: Optional[SSOServer] = None self._sso_server: Optional[SSOServer] = None
self._sso_server_task: Optional[asyncio.Future] = None self._sso_server_task: Optional[asyncio.Future] = None
self._ping_tasks: Dict[str, asyncio.Future] = {}
self.profile_cache: Dict[str, nio.ProfileGetResponse] = {} self.profile_cache: Dict[str, nio.ProfileGetResponse] = {}
self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \ self.get_profile_locks: DefaultDict[str, asyncio.Lock] = \
DefaultDict(asyncio.Lock) # {user_id: lock} DefaultDict(asyncio.Lock) # {user_id: lock}
@ -565,6 +563,7 @@ class Backend:
tmout = aiohttp.ClientTimeout(total=20) tmout = aiohttp.ClientTimeout(total=20)
session = aiohttp.ClientSession(raise_for_status=True, timeout=tmout) session = aiohttp.ClientSession(raise_for_status=True, timeout=tmout)
response = await session.get(api_list) response = await session.get(api_list)
coros = []
for server in (await response.json()): for server in (await response.json()):
homeserver_url = server["homeserver"] homeserver_url = server["homeserver"]
@ -578,9 +577,6 @@ class Backend:
if server["country"] == "USA": if server["country"] == "USA":
server["country"] = "United States" server["country"] = "United States"
if homeserver_url in self._ping_tasks:
self._ping_tasks[homeserver_url].cancel()
self.models["homeservers"][homeserver_url] = Homeserver( self.models["homeservers"][homeserver_url] = Homeserver(
id = homeserver_url, id = homeserver_url,
name = server["name"], name = server["name"],
@ -590,6 +586,7 @@ class Backend:
self._get_homeserver_stability(server["monitor"]["logs"]), self._get_homeserver_stability(server["monitor"]["logs"]),
) )
self._ping_tasks[homeserver_url] = asyncio.ensure_future( coros.append(self._ping_homeserver(session, homeserver_url))
self._ping_homeserver(session, homeserver_url),
) await asyncio.gather(*coros)
await session.close()

View File

@ -27,12 +27,13 @@ HBox {
function takeFocus() { serverField.item.field.forceActiveFocus() } function takeFocus() { serverField.item.field.forceActiveFocus() }
function fetchServers() { function fetchServers() {
if (fetchServersFuture) fetchServersFuture.cancel()
fetchServersFuture = py.callCoro("fetch_homeservers", [], () => { fetchServersFuture = py.callCoro("fetch_homeservers", [], () => {
fetchServersFuture = null fetchServersFuture = null
}, (type, args, error, traceback) => { }, (type, args, error, traceback) => {
fetchServersFuture = null fetchServersFuture = null
// TODO print( traceback) // TODO: display error graphically
print( traceback)
}) })
} }
@ -283,7 +284,7 @@ HBox {
height: width height: width
source: "../../Base/HBusyIndicator.qml" source: "../../Base/HBusyIndicator.qml"
active: box.fetchServersFuture active: box.fetchServersFuture && ! serverList.count
opacity: active ? 1 : 0 opacity: active ? 1 : 0
Behavior on opacity { HNumberAnimation { factor: 2 } } Behavior on opacity { HNumberAnimation { factor: 2 } }