D4856: keepalive: track request count and bytes sent

indygreg (Gregory Szorc) phabricator at mercurial-scm.org
Wed Oct 3 16:52:01 UTC 2018


indygreg created this revision.
Herald added a subscriber: mercurial-devel.
Herald added a reviewer: hg-reviewers.

REVISION SUMMARY
  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.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D4856

AFFECTED FILES
  mercurial/keepalive.py

CHANGE DETAILS

diff --git a/mercurial/keepalive.py b/mercurial/keepalive.py
--- a/mercurial/keepalive.py
+++ b/mercurial/keepalive.py
@@ -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



To: indygreg, #hg-reviewers
Cc: mercurial-devel


More information about the Mercurial-devel mailing list