changeset 40032:dc82ad1b7f77

keepalive: track request count and bytes sent I want wire protocol interactions to report the number of requests made and bytes transferred. This commit teaches the very low-level custom HTTPConnection class to track the number of bytes sent to the socket. This may vary from the number of bytes that go on the wire due to e.g. TLS. That's OK. KeepAliveHandler is taught to track the total number of requests and total number of bytes sent across all requests. Differential Revision: https://phab.mercurial-scm.org/D4856
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 01 Oct 2018 12:02:54 -0700
parents f2dffa1359c6
children 5e5b06087ec5
files mercurial/keepalive.py
diffstat 1 files changed, 19 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/keepalive.py	Mon Oct 01 12:06:36 2018 -0700
+++ b/mercurial/keepalive.py	Mon Oct 01 12:02:54 2018 -0700
@@ -174,6 +174,8 @@
 class KeepAliveHandler(object):
     def __init__(self):
         self._cm = ConnectionManager()
+        self.requestscount = 0
+        self.sentbytescount = 0
 
     #### Connection Management
     def open_connections(self):
@@ -312,6 +314,8 @@
         return r
 
     def _start_transaction(self, h, req):
+        oldbytescount = h.sentbytescount
+
         # What follows mostly reimplements HTTPConnection.request()
         # except it adds self.parent.addheaders in the mix and sends headers
         # in a deterministic order (to make testing easier).
@@ -346,6 +350,16 @@
         if urllibcompat.hasdata(req):
             h.send(data)
 
+        # This will fail to record events in case of I/O failure. That's OK.
+        self.requestscount += 1
+        self.sentbytescount += h.sentbytescount - oldbytescount
+
+        try:
+            self.parent.requestscount += 1
+            self.parent.sentbytescount += h.sentbytescount - oldbytescount
+        except AttributeError:
+            pass
+
 class HTTPHandler(KeepAliveHandler, urlreq.httphandler):
     pass
 
@@ -585,9 +599,11 @@
             data = read(blocksize)
             while data:
                 self.sock.sendall(data)
+                self.sentbytescount += len(data)
                 data = read(blocksize)
         else:
             self.sock.sendall(str)
+            self.sentbytescount += len(str)
     except socket.error as v:
         reraise = True
         if v[0] == errno.EPIPE:      # Broken pipe
@@ -624,6 +640,9 @@
     send = safesend
     getresponse = wrapgetresponse(httplib.HTTPConnection)
 
+    def __init__(self, *args, **kwargs):
+        httplib.HTTPConnection.__init__(self, *args, **kwargs)
+        self.sentbytescount = 0
 
 #########################################################################
 #####   TEST FUNCTIONS