@@ -1862,7 +1862,11 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
18621862
18631863 statistics_.data_received += nread;
18641864
1865- if (UNLIKELY (stream_buf_offset_ > 0 )) {
1865+ if (LIKELY (stream_buf_offset_ == 0 )) {
1866+ // Shrink to the actual amount of used data.
1867+ buf.Resize (nread);
1868+ IncrementCurrentSessionMemory (nread);
1869+ } else {
18661870 // This is a very unlikely case, and should only happen if the ReadStart()
18671871 // call in OnStreamAfterWrite() immediately provides data. If that does
18681872 // happen, we concatenate the data we received with the already-stored
@@ -1871,20 +1875,18 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
18711875 AllocatedBuffer new_buf = env ()->AllocateManaged (pending_len + nread);
18721876 memcpy (new_buf.data (), stream_buf_.base + stream_buf_offset_, pending_len);
18731877 memcpy (new_buf.data () + pending_len, buf.data (), nread);
1878+
1879+ // The data in stream_buf_ is already accounted for, add nread received
1880+ // bytes to session memory but remove the already processed
1881+ // stream_buf_offset_ bytes.
1882+ IncrementCurrentSessionMemory (nread - stream_buf_offset_);
1883+
18741884 buf = std::move (new_buf);
18751885 nread = buf.size ();
18761886 stream_buf_offset_ = 0 ;
18771887 stream_buf_ab_.Reset ();
1878-
1879- // We have now fully processed the stream_buf_ input chunk (by moving the
1880- // remaining part into buf, which will be accounted for below).
1881- DecrementCurrentSessionMemory (stream_buf_.len );
18821888 }
18831889
1884- // Shrink to the actual amount of used data.
1885- buf.Resize (nread);
1886- IncrementCurrentSessionMemory (nread);
1887-
18881890 // Remember the current buffer, so that OnDataChunkReceived knows the
18891891 // offset of a DATA frame's data into the socket read buffer.
18901892 stream_buf_ = uv_buf_init (buf.data (), nread);
0 commit comments