Mercurial > hg
comparison mercurial/keepalive.py @ 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 | 6509fcec830c |
comparison
equal
deleted
inserted
replaced
40032:dc82ad1b7f77 | 40033:5e5b06087ec5 |
---|---|
390 extrakw[r'buffering'] = True | 390 extrakw[r'buffering'] = True |
391 httplib.HTTPResponse.__init__(self, sock, debuglevel=debuglevel, | 391 httplib.HTTPResponse.__init__(self, sock, debuglevel=debuglevel, |
392 method=method, **extrakw) | 392 method=method, **extrakw) |
393 self.fileno = sock.fileno | 393 self.fileno = sock.fileno |
394 self.code = None | 394 self.code = None |
395 self.receivedbytescount = 0 | |
395 self._rbuf = '' | 396 self._rbuf = '' |
396 self._rbufsize = 8096 | 397 self._rbufsize = 8096 |
397 self._handler = None # inserted by the handler later | 398 self._handler = None # inserted by the handler later |
398 self._host = None # (same) | 399 self._host = None # (same) |
399 self._url = None # (same) | 400 self._url = None # (same) |
434 # Careful! http.client.HTTPResponse.read() on Python 3 is | 435 # Careful! http.client.HTTPResponse.read() on Python 3 is |
435 # implemented using readinto(), which can duplicate self._rbuf | 436 # implemented using readinto(), which can duplicate self._rbuf |
436 # if it's not empty. | 437 # if it's not empty. |
437 s = self._rbuf | 438 s = self._rbuf |
438 self._rbuf = '' | 439 self._rbuf = '' |
439 s += self._raw_read(amt) | 440 data = self._raw_read(amt) |
441 | |
442 self.receivedbytescount += len(data) | |
443 self._connection.receivedbytescount += len(data) | |
444 try: | |
445 self._handler.parent.receivedbytescount += len(data) | |
446 except AttributeError: | |
447 pass | |
448 | |
449 s += data | |
440 return s | 450 return s |
441 | 451 |
442 # stolen from Python SVN #68532 to fix issue1088 | 452 # stolen from Python SVN #68532 to fix issue1088 |
443 def _read_chunked(self, amt): | 453 def _read_chunked(self, amt): |
444 chunk_left = self.chunk_left | 454 chunk_left = self.chunk_left |
510 while True: | 520 while True: |
511 new = self._raw_read(readsize) | 521 new = self._raw_read(readsize) |
512 if not new: | 522 if not new: |
513 break | 523 break |
514 | 524 |
525 self.receivedbytescount += len(new) | |
526 self._connection.receivedbytescount += len(new) | |
527 try: | |
528 self._handler.parent.receivedbytescount += len(new) | |
529 except AttributeError: | |
530 pass | |
531 | |
515 chunks.append(new) | 532 chunks.append(new) |
516 i = new.find('\n') | 533 i = new.find('\n') |
517 if i >= 0: | 534 if i >= 0: |
518 break | 535 break |
519 | 536 |
555 dest[0:total] = self._rbuf[:total] | 572 dest[0:total] = self._rbuf[:total] |
556 self._rbuf = self._rbuf[total:] | 573 self._rbuf = self._rbuf[total:] |
557 return total | 574 return total |
558 mv = memoryview(dest) | 575 mv = memoryview(dest) |
559 got = self._raw_readinto(mv[have:total]) | 576 got = self._raw_readinto(mv[have:total]) |
577 | |
578 self.receivedbytescount += got | |
579 self._connection.receivedbytescount += got | |
580 try: | |
581 self._handler.receivedbytescount += got | |
582 except AttributeError: | |
583 pass | |
584 | |
560 dest[0:have] = self._rbuf | 585 dest[0:have] = self._rbuf |
561 got += len(self._rbuf) | 586 got += len(self._rbuf) |
562 self._rbuf = '' | 587 self._rbuf = '' |
563 return got | 588 return got |
564 | 589 |
641 getresponse = wrapgetresponse(httplib.HTTPConnection) | 666 getresponse = wrapgetresponse(httplib.HTTPConnection) |
642 | 667 |
643 def __init__(self, *args, **kwargs): | 668 def __init__(self, *args, **kwargs): |
644 httplib.HTTPConnection.__init__(self, *args, **kwargs) | 669 httplib.HTTPConnection.__init__(self, *args, **kwargs) |
645 self.sentbytescount = 0 | 670 self.sentbytescount = 0 |
671 self.receivedbytescount = 0 | |
646 | 672 |
647 ######################################################################### | 673 ######################################################################### |
648 ##### TEST FUNCTIONS | 674 ##### TEST FUNCTIONS |
649 ######################################################################### | 675 ######################################################################### |
650 | 676 |