changeset 40033:5e5b06087ec5

keepalive: track number of bytes received from an HTTP response We also bubble the byte count up to the HTTPConnection instance and its parent opener at read time. Unlike sending, there isn't a clear "end of response" signal we can intercept to defer updating the accounting. Differential Revision: https://phab.mercurial-scm.org/D4857
author Gregory Szorc <gregory.szorc@gmail.com>
date Mon, 01 Oct 2018 12:30:32 -0700
parents dc82ad1b7f77
children 393e44324037
files mercurial/keepalive.py
diffstat 1 files changed, 27 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/keepalive.py	Mon Oct 01 12:02:54 2018 -0700
+++ b/mercurial/keepalive.py	Mon Oct 01 12:30:32 2018 -0700
@@ -392,6 +392,7 @@
                                       method=method, **extrakw)
         self.fileno = sock.fileno
         self.code = None
+        self.receivedbytescount = 0
         self._rbuf = ''
         self._rbufsize = 8096
         self._handler = None # inserted by the handler later
@@ -436,7 +437,16 @@
         # if it's not empty.
         s = self._rbuf
         self._rbuf = ''
-        s += self._raw_read(amt)
+        data = self._raw_read(amt)
+
+        self.receivedbytescount += len(data)
+        self._connection.receivedbytescount += len(data)
+        try:
+            self._handler.parent.receivedbytescount += len(data)
+        except AttributeError:
+            pass
+
+        s += data
         return s
 
     # stolen from Python SVN #68532 to fix issue1088
@@ -512,6 +522,13 @@
             if not new:
                 break
 
+            self.receivedbytescount += len(new)
+            self._connection.receivedbytescount += len(new)
+            try:
+                self._handler.parent.receivedbytescount += len(new)
+            except AttributeError:
+                pass
+
             chunks.append(new)
             i = new.find('\n')
             if i >= 0:
@@ -557,6 +574,14 @@
             return total
         mv = memoryview(dest)
         got = self._raw_readinto(mv[have:total])
+
+        self.receivedbytescount += got
+        self._connection.receivedbytescount += got
+        try:
+            self._handler.receivedbytescount += got
+        except AttributeError:
+            pass
+
         dest[0:have] = self._rbuf
         got += len(self._rbuf)
         self._rbuf = ''
@@ -643,6 +668,7 @@
     def __init__(self, *args, **kwargs):
         httplib.HTTPConnection.__init__(self, *args, **kwargs)
         self.sentbytescount = 0
+        self.receivedbytescount = 0
 
 #########################################################################
 #####   TEST FUNCTIONS