Skip to content

Commit 69f8b54

Browse files
authored
Allow multiport with ephemeral & unix socket support (#1078)
* Allow multiport with ephemeral & unix socket support * Fix unix tests
1 parent de36c60 commit 69f8b54

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

proxy/core/listener/base.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11-
import os
1211
import socket
1312
import logging
1413
import argparse
@@ -60,5 +59,3 @@ def setup(self) -> None:
6059
def shutdown(self) -> None:
6160
assert self._socket
6261
self._socket.close()
63-
if self.flags.unix_socket_path:
64-
os.remove(self.flags.unix_socket_path)

proxy/core/listener/tcp.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,10 @@ def __init__(self, *args: Any, port: Optional[int] = None, **kwargs: Any) -> Non
6565
super().__init__(*args, **kwargs)
6666

6767
def listen(self) -> socket.socket:
68-
sock = socket.socket(self.flags.family, socket.SOCK_STREAM)
68+
sock = socket.socket(
69+
socket.AF_INET6 if self.flags.hostname.version == 6 else socket.AF_INET,
70+
socket.SOCK_STREAM,
71+
)
6972
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
7073
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
7174
# s.setsockopt(socket.SOL_TCP, socket.TCP_FASTOPEN, 5)

proxy/core/listener/unix.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
:copyright: (c) 2013-present by Abhinav Singh and contributors.
99
:license: BSD, see LICENSE for more details.
1010
"""
11+
import os
1112
import socket
1213
import logging
1314

@@ -30,9 +31,8 @@ class UnixSocketListener(BaseListener):
3031
"""Unix socket domain listener."""
3132

3233
def listen(self) -> socket.socket:
33-
sock = socket.socket(self.flags.family, socket.SOCK_STREAM)
34+
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
3435
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
35-
sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
3636
sock.bind(self.flags.unix_socket_path)
3737
sock.listen(self.flags.backlog)
3838
sock.setblocking(False)
@@ -41,3 +41,8 @@ def listen(self) -> socket.socket:
4141
self.flags.unix_socket_path,
4242
)
4343
return sock
44+
45+
def shutdown(self) -> None:
46+
super().shutdown()
47+
if self.flags.unix_socket_path:
48+
os.remove(self.flags.unix_socket_path)

proxy/proxy.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,24 @@ def setup(self) -> None:
195195
# Override flags.port to match the actual port
196196
# we are listening upon. This is necessary to preserve
197197
# the server port when `--port=0` is used.
198-
self.flags.port = cast(
199-
'TcpSocketListener',
200-
self.listeners.pool[0],
201-
)._port
198+
if not self.flags.unix_socket_path:
199+
self.flags.port = cast(
200+
'TcpSocketListener',
201+
self.listeners.pool[0],
202+
)._port
203+
# --ports flag can also use 0 as value for ephemeral port selection.
204+
# Here, we override flags.ports to reflect actual listening ports.
205+
ports = []
206+
offset = 1 if self.flags.unix_socket_path or self.flags.port else 0
207+
for index in range(offset, offset + len(self.flags.ports)):
208+
ports.append(
209+
cast(
210+
'TcpSocketListener',
211+
self.listeners.pool[index],
212+
)._port,
213+
)
214+
self.flags.ports = ports
215+
# Write ports to port file
202216
self._write_port_file()
203217
# Setup EventManager
204218
if self.flags.enable_events:
@@ -278,7 +292,12 @@ def _delete_pid_file(self) -> None:
278292
def _write_port_file(self) -> None:
279293
if self.flags.port_file:
280294
with open(self.flags.port_file, 'wb') as port_file:
281-
port_file.write(bytes_(self.flags.port))
295+
if not self.flags.unix_socket_path:
296+
port_file.write(bytes_(self.flags.port))
297+
port_file.write(b'\n')
298+
for port in self.flags.ports:
299+
port_file.write(bytes_(port))
300+
port_file.write(b'\n')
282301

283302
def _delete_port_file(self) -> None:
284303
if self.flags.port_file \

tests/core/test_listener.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,11 @@ def test_unix_path_listener(self, mock_socket: mock.Mock, mock_remove: mock.Mock
6969
socket.AF_UNIX,
7070
socket.SOCK_STREAM,
7171
)
72-
self.assertEqual(sock.setsockopt.call_count, 2)
72+
self.assertEqual(sock.setsockopt.call_count, 1)
7373
self.assertEqual(
7474
sock.setsockopt.call_args_list[0][0],
7575
(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1),
7676
)
77-
self.assertEqual(
78-
sock.setsockopt.call_args_list[1][0],
79-
(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1),
80-
)
8177
sock.bind.assert_called_with(sock_path)
8278
sock.listen.assert_called_with(flags.backlog)
8379
sock.setblocking.assert_called_with(False)

0 commit comments

Comments
 (0)