@@ -468,15 +468,13 @@ def _adjust_receive_window(self, frame_len):
468468
469469 return
470470
471- def _recv_cb (self ):
471+ def _consume_single_frame (self ):
472472 """
473- This is the callback used by streams to read data from the connection .
473+ Consumes a single frame from the TCP stream .
474474
475- It expects to read a single frame, and then to deserialize that frame
476- and pass it to the relevant stream. This is generally called by a
477- stream, not by the connection itself, and it's likely that streams will
478- read a frame that doesn't belong to them. That's ok: streams need to
479- make a decision to spin around again.
475+ Right now this method really does a bit too much: it shouldn't be
476+ responsible for determining if a frame is valid or to increase the
477+ flow control window.
480478 """
481479 # Begin by reading 9 bytes from the socket.
482480 header = self ._sock .recv (9 )
@@ -522,6 +520,28 @@ def _recv_cb(self):
522520 else :
523521 self .receive_frame (frame )
524522
523+ def _recv_cb (self ):
524+ """
525+ This is the callback used by streams to read data from the connection.
526+
527+ It expects to read a single frame, and then to deserialize that frame
528+ and pass it to the relevant stream. It then attempts to optimistically
529+ read further frames (in an attempt to ensure that we see control frames
530+ as early as possible).
531+
532+ This is generally called by a stream, not by the connection itself, and
533+ it's likely that streams will read a frame that doesn't belong to them.
534+ """
535+ self ._consume_single_frame ()
536+ count = 9
537+
538+ while count and self ._sock is not None and self ._sock .can_read :
539+ # If the connection has been closed, bail out.
540+ try :
541+ self ._consume_single_frame ()
542+ except ConnectionResetError :
543+ break
544+
525545
526546 # The following two methods are the implementation of the context manager
527547 # protocol.
0 commit comments