Skip to content

Commit c1e38f9

Browse files
committed
Surface actual listening port via flags.port
1 parent ddc9439 commit c1e38f9

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

proxy/core/acceptor/pool.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,10 @@ def listen(self) -> None:
111111
self.socket.bind((str(self.flags.hostname), self.flags.port))
112112
self.socket.listen(self.flags.backlog)
113113
self.socket.setblocking(False)
114-
logger.info(
115-
'Listening on %s:%d' %
116-
(self.flags.hostname, self.flags.port))
114+
# Override flags.port to match the actual port
115+
# we are listening upon. This is necessary to preserve
116+
# the server port when `--port=0` is used.
117+
self.flags.port = self.socket.getsockname()[1]
117118

118119
def start_workers(self) -> None:
119120
"""Start worker processes."""

proxy/proxy.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ class Proxy:
132132

133133
def __init__(self, input_args: Optional[List[str]], **opts: Any) -> None:
134134
self.flags = Proxy.initialize(input_args, **opts)
135-
self.acceptors: Optional[AcceptorPool] = None
135+
self.pool: Optional[AcceptorPool] = None
136136
# TODO(abhinavsingh): Allow users to override the worker class itself
137137
# e.g. A clear text protocol. Or imagine a TelnetProtocolHandler instead
138138
# of default HttpProtocolHandler.
@@ -150,11 +150,11 @@ def delete_pid_file(self) -> None:
150150
os.remove(self.flags.pid_file)
151151

152152
def __enter__(self) -> 'Proxy':
153-
self.acceptors = AcceptorPool(
153+
self.pool = AcceptorPool(
154154
flags=self.flags,
155155
work_klass=self.work_klass
156156
)
157-
self.acceptors.setup()
157+
self.pool.setup()
158158
self.write_pid_file()
159159
return self
160160

@@ -163,8 +163,8 @@ def __exit__(
163163
exc_type: Optional[Type[BaseException]],
164164
exc_val: Optional[BaseException],
165165
exc_tb: Optional[TracebackType]) -> None:
166-
assert self.acceptors
167-
self.acceptors.shutdown()
166+
assert self.pool
167+
self.pool.shutdown()
168168
self.delete_pid_file()
169169

170170
@staticmethod
@@ -449,7 +449,10 @@ def main(
449449
input_args: Optional[List[str]] = None,
450450
**opts: Any) -> None:
451451
try:
452-
with Proxy(input_args=input_args, **opts):
452+
with Proxy(input_args=input_args, **opts) as proxy:
453+
assert proxy.pool is not None
454+
logger.info('Listening on %s:%d' %
455+
(proxy.pool.flags.hostname, proxy.pool.flags.port))
453456
# TODO: Introduce cron feature
454457
# https://github.com/abhinavsingh/proxy.py/issues/392
455458
while True:

tests/core/test_acceptor_pool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_setup_and_shutdown(
4949
sock.setsockopt.assert_called_with(
5050
socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
5151
sock.bind.assert_called_with(
52-
(str(pool.flags.hostname), pool.flags.port))
52+
(str(pool.flags.hostname), 8899))
5353
sock.listen.assert_called_with(pool.flags.backlog)
5454
sock.setblocking.assert_called_with(False)
5555

0 commit comments

Comments
 (0)