Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http2: fix session memory tracking and small clean up #30351

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 15 additions & 17 deletions src/node_http2.cc
Expand Up @@ -1862,33 +1862,31 @@ void Http2Session::OnStreamRead(ssize_t nread, const uv_buf_t& buf_) {

statistics_.data_received += nread;

if (UNLIKELY(stream_buf_offset_ > 0)) {
if (LIKELY(stream_buf_offset_ == 0)) {
// Shrink to the actual amount of used data.
buf.Resize(nread);
IncrementCurrentSessionMemory(nread);
} else {
// This is a very unlikely case, and should only happen if the ReadStart()
// call in OnStreamAfterWrite() immediately provides data. If that does
// happen, we concatenate the data we received with the already-stored
// pending input data, slicing off the already processed part.
AllocatedBuffer new_buf = env()->AllocateManaged(
stream_buf_.len - stream_buf_offset_ + nread);
memcpy(new_buf.data(),
stream_buf_.base + stream_buf_offset_,
stream_buf_.len - stream_buf_offset_);
memcpy(new_buf.data() + stream_buf_.len - stream_buf_offset_,
buf.data(),
nread);
size_t pending_len = stream_buf_.len - stream_buf_offset_;
AllocatedBuffer new_buf = env()->AllocateManaged(pending_len + nread);
memcpy(new_buf.data(), stream_buf_.base + stream_buf_offset_, pending_len);
memcpy(new_buf.data() + pending_len, buf.data(), nread);

// The data in stream_buf_ is already accounted for, add nread received
// bytes to session memory but remove the already processed
// stream_buf_offset_ bytes.
IncrementCurrentSessionMemory(nread - stream_buf_offset_);

buf = std::move(new_buf);
nread = buf.size();
stream_buf_offset_ = 0;
stream_buf_ab_.Reset();

// We have now fully processed the stream_buf_ input chunk (by moving the
// remaining part into buf, which will be accounted for below).
DecrementCurrentSessionMemory(stream_buf_.len);
}

// Shrink to the actual amount of used data.
buf.Resize(nread);
IncrementCurrentSessionMemory(nread);

// Remember the current buffer, so that OnDataChunkReceived knows the
// offset of a DATA frame's data into the socket read buffer.
stream_buf_ = uv_buf_init(buf.data(), nread);
Expand Down