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

network: draining the buffer on close (#9870) (#110) #179

Merged
merged 1 commit into from
Mar 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions source/common/network/connection_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,13 @@ void ConnectionImpl::closeSocket(ConnectionEvent close_type) {
// Drain input and output buffers.
updateReadBufferStats(0, 0);
updateWriteBufferStats(0, 0);

// As the socket closes, drain any remaining data.
// The data won't be written out at this point, and where there are reference
// counted buffer fragments, it helps avoid lifetime issues with the
// connection outlasting the subscriber.
write_buffer_->drain(write_buffer_->length());

connection_stats_.reset();

file_event_.reset();
Expand Down
8 changes: 6 additions & 2 deletions test/common/network/connection_impl_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ class ConnectionImplTest : public testing::TestWithParam<Address::IpVersion> {
}

void disconnect(bool wait_for_remote_close) {
if (client_write_buffer_) {
EXPECT_CALL(*client_write_buffer_, drain(_))
.Times(AnyNumber())
.WillOnce(Invoke([&](uint64_t size) -> void { client_write_buffer_->baseDrain(size); }));
}
EXPECT_CALL(client_callbacks_, onEvent(ConnectionEvent::LocalClose));
client_connection_->close(ConnectionCloseType::NoFlush);
if (wait_for_remote_close) {
Expand Down Expand Up @@ -845,8 +850,6 @@ TEST_P(ConnectionImplTest, WriteWithWatermarks) {
// call to write() will succeed, bringing the connection back under the low watermark.
EXPECT_CALL(*client_write_buffer_, write(_))
.WillOnce(Invoke(client_write_buffer_, &MockWatermarkBuffer::trackWrites));
EXPECT_CALL(*client_write_buffer_, drain(_))
.WillOnce(Invoke(client_write_buffer_, &MockWatermarkBuffer::trackDrains));
EXPECT_CALL(client_callbacks_, onBelowWriteBufferLowWatermark()).Times(1);

disconnect(true);
Expand Down Expand Up @@ -924,6 +927,7 @@ TEST_P(ConnectionImplTest, WatermarkFuzzing) {
dispatcher_->run(Event::Dispatcher::RunType::NonBlock);
}

EXPECT_CALL(client_callbacks_, onBelowWriteBufferLowWatermark()).Times(AnyNumber());
disconnect(true);
}

Expand Down
2 changes: 1 addition & 1 deletion test/extensions/transport_sockets/tls/ssl_socket_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4126,7 +4126,7 @@ class SslReadBufferLimitTest : public SslSocketTest {
EXPECT_CALL(*client_write_buffer, move(_))
.WillRepeatedly(DoAll(AddBufferToStringWithoutDraining(&data_written),
Invoke(client_write_buffer, &MockWatermarkBuffer::baseMove)));
EXPECT_CALL(*client_write_buffer, drain(_)).WillOnce(Invoke([&](uint64_t n) -> void {
EXPECT_CALL(*client_write_buffer, drain(_)).Times(2).WillOnce(Invoke([&](uint64_t n) -> void {
client_write_buffer->baseDrain(n);
dispatcher_->exit();
}));
Expand Down