keepalive: add `__repr__()` to the HTTPConnection class to ease debugging stable
authorMatt Harbison <matt_harbison@yahoo.com>
Tue, 18 Oct 2022 12:58:34 -0400
branchstable
changeset 49543 76fbb1b6692a
parent 49542 8251f7cc787d
child 49544 abf471862b8e
keepalive: add `__repr__()` to the HTTPConnection class to ease debugging By default, this just printed the class name and memory address. By displaying the address and ports on both sides of the socket, it makes it easier to figure out what's in the ConnectionManager, and correlate with WireShark traces. It looks like the two connections mentioned in the previous commit come about because the LFS POST request to access the blobs opens connection 1, and gets a 401. Then for some reason, the follow up with credentials opens a new socket, instead of using the existing one in the pool. I have no clue why. This can be seen with something like this in the blobstore: ``` for h in self.urlopener.handlers: if hasattr(h, "close_all"): print('open connections on %s in pid %d' % (type(h), os.getpid())) for host, conns in h._cm.get_all().items(): for c in conns: print('connection: %r' % c) ```
mercurial/keepalive.py
--- a/mercurial/keepalive.py	Tue Oct 18 11:54:58 2022 -0400
+++ b/mercurial/keepalive.py	Tue Oct 18 12:58:34 2022 -0400
@@ -702,6 +702,17 @@
         self.sentbytescount = 0
         self.receivedbytescount = 0
 
+    def __repr__(self):
+        base = super(HTTPConnection, self).__repr__()
+        local = "(unconnected)"
+        s = self.sock
+        if s:
+            try:
+                local = "%s:%d" % s.getsockname()
+            except OSError:
+                pass  # Likely not connected
+        return "<%s: %s <--> %s:%d>" % (base, local, self.host, self.port)
+
 
 #########################################################################
 #####   TEST FUNCTIONS