Skip to content

Commit

Permalink
keep the entire packet alive (attached to GC root)
Browse files Browse the repository at this point in the history
  • Loading branch information
splitice committed Jan 31, 2022
1 parent 6ef5d59 commit 60d9bbc
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
1 change: 1 addition & 0 deletions src/quic/quic.cc
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ bool PreferredAddress::Resolve(

void Packet::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackFieldWithSize("allocated", ptr_ != data_ ? len_ : 0);
tracker->TrackField("retained", retained);
}

Path::Path(
Expand Down
5 changes: 5 additions & 0 deletions src/quic/quic.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,16 @@ class Packet final : public MemoryRetainer {
SET_MEMORY_INFO_NAME(Packet);
SET_SELF_SIZE(Packet);

inline void AddRetained(BaseObjectPtr<BaseObject> retain){
retained.push_back(retain);
}

private:
uint8_t data_[kDefaultMaxPacketLength];
uint8_t* ptr_ = nullptr;
size_t len_ = kDefaultMaxPacketLength;
const char* diagnostic_label_ = nullptr;
std::vector<BaseObjectPtr<BaseObject>> retained;
};

// A utility class that wraps ngtcp2_path to adapt it to work with SocketAddress
Expand Down
51 changes: 26 additions & 25 deletions src/quic/session.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3241,20 +3241,21 @@ bool Session::Application::SendPendingData() {
return false;
}

// If stream_data.id is -1, then we're not serializing any data for any
// specific stream. We still need to process QUIC session packets tho.
if (stream_data.id > -1)
Debug(session(), "Serializing packets for stream id %" PRId64,
stream_data.id);
else
Debug(session(), "Serializing session packets");

// If the packet was sent previously, then packet will have been reset.
if (!packet) {
if (!pos) {
packet = CreateStreamDataPacket();
pos = packet->data();
}

// If stream_data.id is -1, then we're not serializing any data for any
// specific stream. We still need to process QUIC session packets tho.
if (stream_data.id > -1) {
Debug(session(), "Serializing packets for stream id %" PRId64,
stream_data.id);
packet->AddRetained(stream_data.stream->GetOutboundSource());
} else
Debug(session(), "Requesting serialized packet flush");

ssize_t nwrite = WriteVStream(&path, pos, &ndatalen, stream_data);
if (stream_data.id >= 0) {
Debug(session(),
Expand Down Expand Up @@ -3312,25 +3313,26 @@ bool Session::Application::SendPendingData() {
continue;
}

if(nwrite != 0){
if(nwrite != 0){ // -ve response i.e error
packet.reset();
session()->set_last_error(kQuicInternalError);
return false;
} else {
if (stream_data.id >= 0)
ResumeStream(stream_data.id);

// We are either congestion limited or done.
if (pos - packet->data()) {
// Some data was serialized into the packet. We need to send it.
packet->set_length(pos - packet->data());
Debug(session(), "Congestion limited, but %" PRIu64 " bytes pending",
packet->length());
if (!session()->SendPacket(std::move(packet), path))
return false;
}
return true;
}

// 0 bytes in this sending operation
if (stream_data.id >= 0)
ResumeStream(stream_data.id);

// We are either congestion limited or done.
if (pos - packet->data()) {
// Some data was serialized into the packet. We need to send it.
packet->set_length(pos - packet->data());
Debug(session(), "Congestion limited, but %" PRIu64 " bytes pending",
packet->length());
if (!session()->SendPacket(std::move(packet), path))
return false;
}
return true;
}

pos += nwrite;
Expand All @@ -3346,7 +3348,6 @@ bool Session::Application::SendPendingData() {
Debug(session(), "-- Failed to send packet");
return false;
}
packet.reset();
pos = nullptr;
if (++packets_sent == kMaxPackets) {
Debug(session(), "-- Max packets sent");
Expand Down
4 changes: 4 additions & 0 deletions src/quic/stream.cc
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ void Stream::Commit(size_t amount) {
CHECK_LE(actual, amount);
}

BaseObjectPtr<BaseObject> Stream::GetOutboundSource() const {
return outbound_source_strong_ptr_;
}

int Stream::DoPull(
bob::Next<ngtcp2_vec> next,
int options,
Expand Down
2 changes: 2 additions & 0 deletions src/quic/stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ class Stream final : public AsyncWrap,
// Attaches an outbound Buffer::Source
void AttachOutboundSource(Buffer::Source* source);

BaseObjectPtr<BaseObject> GetOutboundSource() const;

// Signals the beginning of a new block of headers.
void BeginHeaders(HeadersKind kind);

Expand Down

0 comments on commit 60d9bbc

Please sign in to comment.