Skip to content

Commit 50705ab

Browse files
committed
Log local address when connecting/disconnecting in RPC
1 parent afb0f70 commit 50705ab

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

cms/io/rpc.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ def __init__(self, remote_address):
9393
connection (origin or target, depending on its direction).
9494
9595
"""
96+
self._local_address = None
9697
self.remote_address = remote_address
9798
self._connection_event = gevent.event.Event()
9899

@@ -160,8 +161,13 @@ def initialize(self, sock, plus):
160161
self._reader = self._socket.makefile('rb')
161162
self._writer = self._socket.makefile('wb')
162163
self._connection_event.set()
164+
# IPv4 addresses have two elements (host and port), IPv6 ones
165+
# have 4 elements (host, port, flowinfo and scopeid). We will
166+
# discard the last two ones, losing information, to simplify.
167+
self._local_address = "%s:%d" % self._socket.getsockname()[:2]
163168

164-
logger.info("Established connection with %s.", self._repr_remote())
169+
logger.info("Established connection with %s (local address: %s).",
170+
self._repr_remote(), self._local_address)
165171

166172
for handler in self._on_connect_handlers:
167173
gevent.spawn(handler, plus)
@@ -179,14 +185,15 @@ def finalize(self, reason=""):
179185
if not self.connected:
180186
return
181187

188+
logger.info("Terminated connection with %s (local address: %s): %s",
189+
self._repr_remote(), self._local_address, reason)
190+
182191
self._socket = None
183192
self._reader = None
184193
self._writer = None
194+
self._local_address = None
185195
self._connection_event.clear()
186196

187-
logger.info("Terminated connection with %s: %s", self._repr_remote(),
188-
reason)
189-
190197
for handler in self._on_disconnect_handlers:
191198
gevent.spawn(handler)
192199

0 commit comments

Comments
 (0)