@@ -1840,11 +1840,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
1840
1840
1841
1841
statistics_.data_received += nread;
1842
1842
1843
- if (LIKELY (stream_buf_offset_ == 0 )) {
1844
- // Shrink to the actual amount of used data.
1845
- buf.Resize (nread);
1846
- IncrementCurrentSessionMemory (nread);
1847
- } else {
1843
+ if (UNLIKELY (stream_buf_offset_ > 0 )) {
1848
1844
// This is a very unlikely case, and should only happen if the ReadStart()
1849
1845
// call in OnStreamAfterWrite() immediately provides data. If that does
1850
1846
// happen, we concatenate the data we received with the already-stored
@@ -1854,17 +1850,20 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
1854
1850
memcpy (new_buf.data (), stream_buf_.base + stream_buf_offset_, pending_len);
1855
1851
memcpy (new_buf.data () + pending_len, buf.data (), nread);
1856
1852
1857
- // The data in stream_buf_ is already accounted for, add nread received
1858
- // bytes to session memory but remove the already processed
1859
- // stream_buf_offset_ bytes.
1860
- IncrementCurrentSessionMemory (nread - stream_buf_offset_);
1861
-
1862
1853
buf = std::move (new_buf);
1863
1854
nread = buf.size ();
1864
1855
stream_buf_offset_ = 0 ;
1865
1856
stream_buf_ab_.Reset ();
1857
+
1858
+ // We have now fully processed the stream_buf_ input chunk (by moving the
1859
+ // remaining part into buf, which will be accounted for below).
1860
+ DecrementCurrentSessionMemory (stream_buf_.len );
1866
1861
}
1867
1862
1863
+ // Shrink to the actual amount of used data.
1864
+ buf.Resize (nread);
1865
+ IncrementCurrentSessionMemory (nread);
1866
+
1868
1867
// Remember the current buffer, so that OnDataChunkReceived knows the
1869
1868
// offset of a DATA frame's data into the socket read buffer.
1870
1869
stream_buf_ = uv_buf_init (buf.data (), nread);
0 commit comments