Skip to content
This repository was archived by the owner on Nov 23, 2017. It is now read-only.

Commit f6117ee

Browse files
committed
Tighten up the SO_REUSEPORT code
1 parent 610c03f commit f6117ee

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

asyncio/base_events.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ def _format_pipe(fd):
7676
return repr(fd)
7777

7878

79+
def _set_reuseport(sock):
80+
if not hasattr(socket, 'SO_REUSEPORT'):
81+
raise ValueError('reuse_port not supported by socket module')
82+
else:
83+
try:
84+
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
85+
except OSError:
86+
raise ValueError('reuse_port not supported by socket module, '
87+
'SO_REUSEPORT defined but not implemented.')
88+
89+
7990
# Linux's sock.type is a bitmask that can include extra info about socket.
8091
_SOCKET_TYPE_MASK = 0
8192
if hasattr(socket, 'SOCK_NONBLOCK'):
@@ -873,17 +884,7 @@ def create_datagram_endpoint(self, protocol_factory,
873884
sock.setsockopt(
874885
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
875886
if reuse_port:
876-
if not hasattr(socket, 'SO_REUSEPORT'):
877-
raise ValueError(
878-
'reuse_port not supported by socket module')
879-
else:
880-
try:
881-
sock.setsockopt(
882-
socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
883-
except OSError:
884-
raise ValueError((
885-
'reuse_port not supported by socket module, '
886-
'SO_REUSEPORT defined but not implemented.'))
887+
_set_reuseport(sock)
887888
if allow_broadcast:
888889
sock.setsockopt(
889890
socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
@@ -1006,17 +1007,7 @@ def create_server(self, protocol_factory, host=None, port=None,
10061007
sock.setsockopt(
10071008
socket.SOL_SOCKET, socket.SO_REUSEADDR, True)
10081009
if reuse_port:
1009-
if not hasattr(socket, 'SO_REUSEPORT'):
1010-
raise ValueError(
1011-
'reuse_port not supported by socket module')
1012-
else:
1013-
try:
1014-
sock.setsockopt(
1015-
socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
1016-
except OSError:
1017-
raise ValueError((
1018-
'reuse_port not supported by socket module, '
1019-
'SO_REUSEPORT defined but not implemented.'))
1010+
_set_reuseport(sock)
10201011
# Disable IPv4/IPv6 dual stack support (enabled by
10211012
# default on Linux) which makes a single socket
10221013
# listen on both address families.

0 commit comments

Comments
 (0)