@@ -1754,11 +1754,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
1754
1754
1755
1755
statistics_.data_received += nread;
1756
1756
1757
- if (LIKELY (stream_buf_offset_ == 0 )) {
1758
- // Shrink to the actual amount of used data.
1759
- buf.Resize (nread);
1760
- IncrementCurrentSessionMemory (nread);
1761
- } else {
1757
+ if (UNLIKELY (stream_buf_offset_ > 0 )) {
1762
1758
// This is a very unlikely case, and should only happen if the ReadStart()
1763
1759
// call in OnStreamAfterWrite() immediately provides data. If that does
1764
1760
// happen, we concatenate the data we received with the already-stored
@@ -1769,20 +1765,20 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
1769
1765
memcpy (new_buf.data (), stream_buf_.base + stream_buf_offset_, pending_len);
1770
1766
memcpy (new_buf.data () + pending_len, buf.data (), nread);
1771
1767
1772
- // The data in stream_buf_ is already accounted for, add nread received
1773
- // bytes to session memory but remove the already processed
1774
- // stream_buf_offset_ bytes.
1775
- // TODO(@jasnell): There are some cases where nread is < stream_buf_offset_
1776
- // here but things still work. Those need to be investigated.
1777
- // CHECK_GE(nread, stream_buf_offset_);
1778
- IncrementCurrentSessionMemory (nread - stream_buf_offset_);
1779
-
1780
1768
buf = std::move (new_buf);
1781
1769
nread = buf.size ();
1782
1770
stream_buf_offset_ = 0 ;
1783
1771
stream_buf_ab_.Reset ();
1772
+
1773
+ // We have now fully processed the stream_buf_ input chunk (by moving the
1774
+ // remaining part into buf, which will be accounted for below).
1775
+ DecrementCurrentSessionMemory (stream_buf_.len );
1784
1776
}
1785
1777
1778
+ // Shrink to the actual amount of used data.
1779
+ buf.Resize (nread);
1780
+ IncrementCurrentSessionMemory (nread);
1781
+
1786
1782
// Remember the current buffer, so that OnDataChunkReceived knows the
1787
1783
// offset of a DATA frame's data into the socket read buffer.
1788
1784
stream_buf_ = uv_buf_init (buf.data (), static_cast <unsigned int >(nread));
0 commit comments