@@ -981,45 +981,24 @@ ssize_t Http2Session::OnMaxFrameSizePadding(size_t frameLen,
981981// quite expensive. This is a potential performance optimization target later.
982982void Http2Session::ConsumeHTTP2Data () {
983983 CHECK_NOT_NULL (stream_buf_.base );
984- CHECK_LE (stream_buf_offset_, stream_buf_.len );
985- size_t read_len = stream_buf_.len - stream_buf_offset_;
986984
987985 // multiple side effects.
988- Debug (this , " receiving %d bytes [wants data? %d]" ,
989- read_len,
986+ Debug (this ,
987+ " receiving %d bytes [wants data? %d]" ,
988+ stream_buf_.len ,
990989 nghttp2_session_want_read (session_.get ()));
991- set_receive_paused (false );
992990 custom_recv_error_code_ = nullptr ;
993991 set_receiving ();
994992 ssize_t ret =
995- nghttp2_session_mem_recv (session_.get (),
996- reinterpret_cast <uint8_t *>(stream_buf_.base ) +
997- stream_buf_offset_,
998- read_len);
993+ nghttp2_session_mem_recv (session_.get (),
994+ reinterpret_cast <uint8_t *>(stream_buf_.base ),
995+ stream_buf_.len );
999996 set_receiving (false );
1000997 CHECK_NE (ret, NGHTTP2_ERR_NOMEM );
1001998 CHECK_IMPLIES (custom_recv_error_code_ != nullptr , ret < 0 );
1002999
1003- if (is_receive_paused ()) {
1004- CHECK (is_reading_stopped ());
1005-
1006- CHECK_GT (ret, 0 );
1007- CHECK_LE (static_cast <size_t >(ret), read_len);
1008-
1009- // Mark the remainder of the data as available for later consumption.
1010- // Even if all bytes were received, a paused stream may delay the
1011- // nghttp2_on_frame_recv_callback which may have an END_STREAM flag.
1012- stream_buf_offset_ += ret;
1013- // Still complete a Close() deferred during mem_recv; do not fall through
1014- // to SendPendingData() here (paused receives historically skip that flush
1015- // because a write may already be in progress).
1016- MaybeFinishPendingClose ();
1017- goto done;
1018- }
1019-
10201000 // We are done processing the current input chunk.
10211001 DecrementCurrentSessionMemory (stream_buf_.len );
1022- stream_buf_offset_ = 0 ;
10231002 stream_buf_ab_.Reset ();
10241003 stream_buf_allocation_.reset ();
10251004 stream_buf_ = uv_buf_init (nullptr , 0 );
@@ -1028,14 +1007,6 @@ void Http2Session::ConsumeHTTP2Data() {
10281007 // not written after pending RST_STREAM frames.
10291008 MaybeFinishPendingClose ();
10301009
1031- done:
1032- // Finish a Close() deferred above before flushing, so GOAWAY is not written
1033- // after pending RST_STREAM frames.
1034- if (is_close_pending () && !is_destroyed ()) {
1035- set_close_pending (false );
1036- FinishClose (pending_close_code_, pending_close_socket_closed_);
1037- }
1038-
10391010 // Send any data that was queued up while processing the received data.
10401011 if (ret >= 0 && !is_destroyed ()) {
10411012 SendPendingData ();
@@ -1485,15 +1456,6 @@ int Http2Session::OnDataChunkReceived(nghttp2_session* handle,
14851456 }
14861457 } while (len != 0 );
14871458
1488- // If we are currently waiting for a write operation to finish, we should
1489- // tell nghttp2 that we want to wait before we process more input data.
1490- if (session->is_write_in_progress ()) {
1491- CHECK (session->is_reading_stopped ());
1492- session->set_receive_paused ();
1493- Debug (session, " receive paused" );
1494- return NGHTTP2_ERR_PAUSE ;
1495- }
1496-
14971459 return 0 ;
14981460}
14991461
@@ -1576,7 +1538,6 @@ void Http2StreamListener::OnStreamRead(ssize_t nread, const uv_buf_t& buf) {
15761538 size_t offset = buf.base - session->stream_buf_ .base ;
15771539
15781540 // Verify that the data offset is inside the current read buffer.
1579- CHECK_GE (offset, session->stream_buf_offset_ );
15801541 CHECK_LE (offset, session->stream_buf_ .len );
15811542 CHECK_LE (offset + buf.len , session->stream_buf_ .len );
15821543
@@ -1891,11 +1852,6 @@ void Http2Session::OnStreamAfterWrite(WriteWrap* w, int status) {
18911852 return ;
18921853 }
18931854
1894- // If there is more incoming data queued up, consume it.
1895- if (stream_buf_offset_ > 0 ) {
1896- ConsumeHTTP2Data ();
1897- }
1898-
18991855 if (!is_write_scheduled () && !is_destroyed ()) {
19001856 // Schedule a new write if nghttp2 wants to send data.
19011857 MaybeScheduleWrite ();
@@ -1942,7 +1898,7 @@ void Http2Session::MaybeStopReading() {
19421898 if (is_reading_stopped () || is_closing ()) return ;
19431899 int want_read = nghttp2_session_want_read (session_.get ());
19441900 Debug (this , " wants read? %d" , want_read);
1945- if (want_read == 0 || is_write_in_progress () ) {
1901+ if (want_read == 0 ) {
19461902 set_reading_stopped ();
19471903 stream_->ReadStop ();
19481904 }
@@ -2207,7 +2163,7 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
22072163 Context::Scope context_scope (env ()->context ());
22082164 Http2Scope h2scope (this );
22092165 CHECK_NOT_NULL (stream_);
2210- Debug (this , " receiving %d bytes, offset %d " , nread, stream_buf_offset_ );
2166+ Debug (this , " receiving %d bytes" , nread);
22112167 std::unique_ptr<BackingStore> bs = env ()->release_managed_buffer (buf_);
22122168
22132169 // Only pass data on if nread > 0
@@ -2222,40 +2178,18 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {
22222178
22232179 statistics_.data_received += nread;
22242180
2225- if (stream_buf_offset_ == 0 && static_cast <size_t >(nread) != bs->ByteLength ())
2226- [[likely]] {
2181+ // ConsumeHTTP2Data() always consumes the whole chunk, so there is never a
2182+ // partially processed buffer left over from a previous read.
2183+ DCHECK_NULL (stream_buf_.base );
2184+
2185+ if (static_cast <size_t >(nread) != bs->ByteLength ()) [[likely]] {
22272186 // Shrink to the actual amount of used data.
22282187 std::unique_ptr<BackingStore> old_bs = std::move (bs);
22292188 bs = ArrayBuffer::NewBackingStore (
22302189 env ()->isolate (),
22312190 nread,
22322191 BackingStoreInitializationMode::kUninitialized );
22332192 memcpy (bs->Data (), old_bs->Data (), nread);
2234- } else {
2235- // This is a very unlikely case, and should only happen if the ReadStart()
2236- // call in OnStreamAfterWrite() immediately provides data. If that does
2237- // happen, we concatenate the data we received with the already-stored
2238- // pending input data, slicing off the already processed part.
2239- size_t pending_len = stream_buf_.len - stream_buf_offset_;
2240- std::unique_ptr<BackingStore> new_bs = ArrayBuffer::NewBackingStore (
2241- env ()->isolate (),
2242- pending_len + nread,
2243- BackingStoreInitializationMode::kUninitialized );
2244- memcpy (static_cast <char *>(new_bs->Data ()),
2245- stream_buf_.base + stream_buf_offset_,
2246- pending_len);
2247- memcpy (static_cast <char *>(new_bs->Data ()) + pending_len,
2248- bs->Data (),
2249- nread);
2250-
2251- bs = std::move (new_bs);
2252- nread = bs->ByteLength ();
2253- stream_buf_offset_ = 0 ;
2254- stream_buf_ab_.Reset ();
2255-
2256- // We have now fully processed the stream_buf_ input chunk (by moving the
2257- // remaining part into buf, which will be accounted for below).
2258- DecrementCurrentSessionMemory (stream_buf_.len );
22592193 }
22602194
22612195 IncrementCurrentSessionMemory (nread);
0 commit comments