Skip to content

Commit

Permalink
Add MTU size to UdpConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
icgood committed Aug 13, 2023
1 parent 7ba009c commit cf4624e
Showing 1 changed file with 3 additions and 15 deletions.
18 changes: 3 additions & 15 deletions swimprotocol/udp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def __init__(self, *, bind_host: Optional[str] = None,
default_host: Optional[str] = None,
default_port: Optional[int] = None,
discovery: bool = False,
mtu_size: int = 1500,
**kwargs: Any) -> None:
address_parser = AddressParser(
default_host=default_host,
Expand All @@ -58,6 +59,7 @@ def __init__(self, *, bind_host: Optional[str] = None,
self.bind_host: Final = bind_host
self.bind_port: Final = bind_port
self.address_parser: Final = address_parser
self.mtu_size: Final = mtu_size

@classmethod
def add_arguments(cls, parser: ArgumentParser, *,
Expand Down Expand Up @@ -213,7 +215,7 @@ async def _run_send(self, thread_pool: ThreadPoolExecutor,
packet_data = await loop.run_in_executor(
thread_pool, self.udp_pack.pack, packet)
address = self.address_parser.parse(member.name)
if len(packet_data) <= 1500:
if len(packet_data) <= self.config.mtu_size:
udp_transport.sendto(packet_data, (address.host, address.port))
else:
asyncio.create_task(self._tcp_send(packet_data, address))
Expand All @@ -225,20 +227,6 @@ async def _tcp_send(self, packet_data: bytes, address: Address) -> None:
with closing(tcp_transport):
tcp_transport.write(packet_data)

@classmethod
def add_arguments(cls, name: str, parser: ArgumentParser, *,
prefix: str = '--udp') -> None:
group = parser.add_argument_group(f'{name} options')
group.add_argument(f'{prefix}-bind', metavar='INTERFACE',
dest='swim_udp_bind',
help='The bind IP address.')
group.add_argument(f'{prefix}-host', metavar='NAME',
dest='swim_udp_host',
help='The default remote hostname.')
group.add_argument(f'{prefix}-port', metavar='NUM', type=int,
dest='swim_udp_port',
help='The default port number.')


class _BaseProtocol(Subtasks):

Expand Down

0 comments on commit cf4624e

Please sign in to comment.