Skip to content

Commit

Permalink
Change: give TURN more time to connect (from 10 to 20 seconds) (#35)
Browse files Browse the repository at this point in the history
This means that users that get a popup client-side to ask if they
want to use TURN have more time to read the dialog. On average this
should now be ~18 seconds that they have time to read it, up from
~8 seconds.
  • Loading branch information
TrueBrain authored Jul 20, 2021
1 parent e7d7054 commit 6c03c41
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
4 changes: 3 additions & 1 deletion game_coordinator/application/helpers/token_connect.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

log = logging.getLogger(__name__)

TIMEOUT = 20 # After how many seconds we give up on connecting client and server.


class TokenConnect:
def __init__(self, application, source, protocol_version, token, server):
Expand Down Expand Up @@ -70,7 +72,7 @@ async def stun_result(self, prefix, interface_number, peer_type, peer_ip, peer_p

async def _timeout(self):
try:
await asyncio.sleep(10)
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.
self._timeout_task = None
Expand Down
7 changes: 6 additions & 1 deletion game_coordinator/application/turn.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
log = logging.getLogger(__name__)

TTL = 15
TIMEOUT = 20 # After how many seconds we give up on connecting client and server.

_turn_address = None

Expand Down Expand Up @@ -82,8 +83,12 @@ async def receive_PACKET_TURN_SERCLI_CONNECT(self, source, protocol_version, tic
await self._ticket_pair[ticket].protocol.send_PACKET_TURN_TURN_CONNECTED(protocol_version, str(source.ip))
await self.database.stats_turn("two-sided")

del self._ticket_pair[ticket]

async def _expire_ticket(self, ticket):
await asyncio.sleep(10)
await asyncio.sleep(TIMEOUT)

self._ticket_pair[ticket].protocol.transport.close()

# Expire the ticket after 10 seconds if there was no match.
del self._ticket_pair[ticket]
Expand Down
2 changes: 1 addition & 1 deletion game_coordinator/database/redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ async def create_turn_ticket(self):
while True:
ticket_left = secrets.token_hex(8)
ticket_right = secrets.token_hex(8)
if await self._redis.set(f"turn-ticket:{ticket_left}", ticket_right, ex=10, nx=True):
if await self._redis.set(f"turn-ticket:{ticket_left}", ticket_right, ex=30, nx=True):
break

return f"{ticket_left}:{ticket_right}"
Expand Down

0 comments on commit 6c03c41

Please sign in to comment.