Skip to content

Commit

Permalink
Fix: track better why a connection (method) failed (#47)
Browse files Browse the repository at this point in the history
Also make it more obvious what is a final call, and what is a
"this method failed".
  • Loading branch information
TrueBrain committed Aug 23, 2021
1 parent 86f287c commit 2db6577
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
16 changes: 8 additions & 8 deletions game_coordinator/application/helpers/token_connect.py
Expand Up @@ -80,9 +80,9 @@ async def _timeout(self):
try:
await asyncio.sleep(TIMEOUT)

# If we reach here, we haven't managed to get a connection within 10 seconds. Time to call it a day.
# If we reach here, we haven't managed to get a connection within TIMEOUT seconds. Time to call it a day.
self._timeout_task = None
await self._connect_failed()
await self._connect_failed("timeout")
except Exception:
log.exception("Exception during _timeout()")

Expand Down Expand Up @@ -111,13 +111,13 @@ async def _connect(self):
# If TURN failed, we have no other method left, so fail the attempt.

self._connect_task = None
asyncio.create_task(self._connect_failed())
asyncio.create_task(self._connect_failed("out-of-methods"))
break
except SocketClosed:
# Either of the two sides closed the Game Coordinator
# connection. So cancel the connection attempt.
self._connect_task = None
asyncio.create_task(self._connect_failed(True))
asyncio.create_task(self._connect_failed("closed"))
break
except Exception:
log.exception("Exception during _connect_next_wait()")
Expand All @@ -132,7 +132,7 @@ async def _connect_next_wait(self):
async def connect_failed(self, tracking_number):
if tracking_number == 0:
# Client requested we stop with this connection attempt. So clean it up!
asyncio.create_task(self._connect_failed(True))
asyncio.create_task(self._connect_failed("stop"))
return

# Check if this is our current attempt. Server and client send
Expand All @@ -141,7 +141,7 @@ async def connect_failed(self, tracking_number):
if tracking_number != self._tracking_number:
return

await self._application.database.stats_connect(self._connect_method, False)
await self._application.database.stats_connect(self._connect_method, False, False)

# Try the next attempt now.
self._tracking_number += 1
Expand Down Expand Up @@ -197,7 +197,7 @@ async def _connect_turn_connect(self, connection_string):
connection_string,
)

async def _connect_failed(self, on_request=False):
async def _connect_failed(self, stats_type):
if self._connect_task:
self._connect_task.cancel()
self._connect_task = None
Expand All @@ -219,6 +219,6 @@ async def _connect_failed(self, on_request=False):
# If the server already left, that is fine.
pass

await self._application.database.stats_connect("closed" if on_request else "failed", False)
await self._application.database.stats_connect(stats_type, False)

self._application.delete_token(self.token)
10 changes: 8 additions & 2 deletions game_coordinator/database/redis.py
Expand Up @@ -327,8 +327,14 @@ async def server_offline(self, server_id):
async def stats_verify(self, connection_type_name):
await self._stats("verify", connection_type_name)

async def stats_connect(self, method_name, result):
key = "connect" if result else "connect-failed"
async def stats_connect(self, method_name, result, final=True):
if result:
# Successful connections are always final.
key = "connect"
elif final:
key = "connect-failed"
else:
key = "connect-method-failed"

await self._stats(key, method_name)

Expand Down
1 change: 1 addition & 0 deletions game_coordinator/web.py
Expand Up @@ -22,6 +22,7 @@ async def stats_handler(request):
"listing": await DB_INSTANCE.get_stats("listing"),
"connect": await DB_INSTANCE.get_stats("connect"),
"connect-failed": await DB_INSTANCE.get_stats("connect-failed"),
"connect-method-failed": await DB_INSTANCE.get_stats("connect-method-failed"),
"turn": await DB_INSTANCE.get_stats("turn"),
}
)
Expand Down

0 comments on commit 2db6577

Please sign in to comment.