Skip to content

Commit

Permalink
buffer: draining any zero byte fragments (#9837) (#109) (#123)
Browse files Browse the repository at this point in the history
Given that we allow creating zero byte fragments, it'd be good to proactively drain them. For example if someone is doing timing instrumentation and wants to know when Network::Connection data is written to the kernel, it could be useful to have a zero byte sentinel.

Risk Level: Low (I don't think anyone is adding zero byte fragments yet)
Testing: new unit test
Docs Changes: n/a
Release Notes: n/a

Signed-off-by: Alyssa Wilk <alyssar@chromium.org>
Signed-off-by: Jianfei Hu <jianfeih@google.com>
Co-authored-by: Lizan Zhou <lizan@tetrate.io>
  • Loading branch information
Jianfei Hu and lizan committed Mar 3, 2020
1 parent b49b560 commit 92bd1e9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/common/buffer/buffer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ void OwnedImpl::drain(uint64_t size) {
}
}
}
// Make sure to drain any zero byte fragments that might have been added as
// sentinels for flushed data.
while (!slices_.empty() && slices_.front()->dataSize() == 0) {
slices_.pop_front();
}
}

uint64_t OwnedImpl::getRawSlices(RawSlice* out, uint64_t out_size) const {
Expand Down
18 changes: 18 additions & 0 deletions test/common/buffer/owned_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ TEST_P(OwnedImplTest, AddBufferFragmentWithCleanup) {
EXPECT_TRUE(release_callback_called_);
}

TEST_P(OwnedImplTest, AddEmptyFragment) {
char input[] = "hello world";
BufferFragmentImpl frag1(input, 11, [](const void*, size_t, const BufferFragmentImpl*) {});
BufferFragmentImpl frag2("", 0, [this](const void*, size_t, const BufferFragmentImpl*) {
release_callback_called_ = true;
});
Buffer::OwnedImpl buffer;
buffer.addBufferFragment(frag1);
EXPECT_EQ(11, buffer.length());

buffer.addBufferFragment(frag2);
EXPECT_EQ(11, buffer.length());

buffer.drain(11);
EXPECT_EQ(0, buffer.length());
EXPECT_TRUE(release_callback_called_);
}

TEST_P(OwnedImplTest, AddBufferFragmentDynamicAllocation) {
char input_stack[] = "hello world";
char* input = new char[11];
Expand Down

0 comments on commit 92bd1e9

Please sign in to comment.