Skip to content

Commit

Permalink
feat: add max number of retries for socket binding
Browse files Browse the repository at this point in the history
  • Loading branch information
santiagosalamandri committed Jan 25, 2023
1 parent 437fc77 commit 6cb7a5e
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions iso15118/secc/transport/tcp_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ async def server_factory(self, ready_event: asyncio.Event, tls: bool) -> None:
port = self.port_tls
ssl_context = get_ssl_context(True)
server_type = "TLS"
# Initialise socket for IPv6 TCP packets
# Address family (determines network layer protocol, here IPv6)
# Socket type (stream, determines transport layer protocol TCP)
while True:

MAX_RETRIES: int = 3
BACK_OFF_SECONDS: float = 0.5

for i in range(MAX_RETRIES):
# Initialise socket for IPv6 TCP packets
# Address family (determines network layer protocol, here IPv6)
# Socket type (stream, determines transport layer protocol TCP)
sock = socket.socket(family=socket.AF_INET6, type=socket.SOCK_STREAM)

# Allows address to be reused
Expand All @@ -97,11 +101,16 @@ async def server_factory(self, ready_event: asyncio.Event, tls: bool) -> None:
# TCP packets
try:
sock.bind(self.full_ipv6_address)
break
except OSError as e:
logger.info(f"{e} on {server_type} server. Retrying...")
await asyncio.sleep(0.5)
continue
break
# Once the max amount of retries has been reached, reraise the exception
if i == MAX_RETRIES - 1:
raise e
else:
logger.info(f"{e} on {server_type} server. Retrying...")
await asyncio.sleep(BACK_OFF_SECONDS)
continue

server = await asyncio.start_server(
# The client_connected_cb callback, which is the __call__ method of
# this class) is called whenever a new client connection is
Expand Down

0 comments on commit 6cb7a5e

Please sign in to comment.