Skip to content

Commit

Permalink
ENH: add reuse_port option in create_socket function
Browse files Browse the repository at this point in the history
  • Loading branch information
ninousf authored and eerimoq committed Jun 14, 2023
1 parent 8bb923a commit 0c53883
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 3 additions & 2 deletions asyncudp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def __aexit__(self, *exc_info):
self.close()


async def create_socket(local_addr=None, remote_addr=None, packets_queue_max_size=0):
async def create_socket(local_addr=None, remote_addr=None, packets_queue_max_size=0, reuse_port=None):
"""Create a UDP socket with given local and remote addresses.
>>> sock = await asyncudp.create_socket(local_addr=('127.0.0.1', 9999))
Expand All @@ -116,6 +116,7 @@ async def create_socket(local_addr=None, remote_addr=None, packets_queue_max_siz
transport, protocol = await loop.create_datagram_endpoint(
lambda: _SocketProtocol(packets_queue_max_size),
local_addr=local_addr,
remote_addr=remote_addr)
remote_addr=remote_addr,
reuse_port=reuse_port)

return Socket(transport, protocol)
10 changes: 10 additions & 0 deletions tests/test_asyncudp.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,13 @@ async def packets_queue_max_size(self):

server.close()
client.close()

def test_create_socket_reuse_port(self):
asyncio.run(self.create_socket_reuse_port())

async def create_socket_reuse_port(self):
sock = await asyncudp.create_socket(local_addr=('127.0.0.1', 13003), reuse_port=True)
sock.close()
sock2 = await asyncudp.create_socket(local_addr=('127.0.0.1', 13003), reuse_port=True)
sock2.close()

0 comments on commit 0c53883

Please sign in to comment.