Skip to content

Commit c565e11

Browse files
Change non_blocking_send threshold to sendBufferSize (#6073) (#6077)
(cherry picked from commit 81a5b12) Signed-off-by: Juan Lopez Fernandez <juanlopez@eprosima.com> Co-authored-by: juanlofer-eprosima <88179026+juanlofer-eprosima@users.noreply.github.com>
1 parent 2b56301 commit c565e11

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/cpp/rtps/transport/TCPChannelResource.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,9 +363,10 @@ bool TCPChannelResource::check_socket_send_buffer(
363363

364364

365365
size_t future_queue_size = size_t(bytesInSendQueue) + msg_size;
366-
// TCP actually allocates twice the size of the buffer requested.
367-
if (future_queue_size > size_t(2 * parent_->configuration()->sendBufferSize))
366+
if (future_queue_size > size_t(parent_->configuration()->sendBufferSize))
368367
{
368+
// NOTE: TCP actually allocates about twice the size of the buffer requested, still we use the user-provided
369+
// value as threshold to avoid blocking if the actual allocated space falls below our estimation
369370
return false;
370371
}
371372
return true;

test/unittest/transport/TCPv4Tests.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1570,15 +1570,15 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
15701570

15711571
// Prepare the message
15721572
asio::error_code ec;
1573-
std::vector<octet> message(msg_size * 2, 0);
1573+
std::vector<octet> message(msg_size, 0);
15741574
const octet* data = message.data();
15751575
size_t size = message.size();
15761576
NetworkBuffer buffers(data, size);
15771577
std::vector<NetworkBuffer> buffer_list;
15781578
buffer_list.push_back(buffers);
15791579

1580-
// Send the message with no header. Since TCP actually allocates twice the size of the buffer requested
1581-
// it should be able to send a message of msg_size*2.
1580+
// Send the message with no header. Since TCP actually allocates about twice the size of the buffer requested, and
1581+
// since the threshold to discard (sendBufferSize) is set to msg_size, it should be able to send a message of msg_size.
15821582
size_t bytes_sent = sender_channel_resource->send(nullptr, 0, buffer_list, size, ec);
15831583
ASSERT_EQ(bytes_sent, size);
15841584

@@ -1589,8 +1589,9 @@ TEST_F(TCPv4Tests, secure_non_blocking_send)
15891589
ASSERT_EQ(ec, asio::error_code());
15901590
ASSERT_EQ(bytes_read, size);
15911591

1592-
// Now try to send a message that is bigger than the buffer size: (msg_size*2 + 1) + bytes_in_send_buffer(0) > 2*sendBufferSize
1593-
message.resize(msg_size * 2 + 1);
1592+
// Now try to send a message whose size surpasses the threshold to discard (sendBufferSize):
1593+
// (msg_size + 1) + bytes_in_send_buffer(0) > sendBufferSize
1594+
message.resize(msg_size + 1);
15941595
data = message.data();
15951596
size = message.size();
15961597
buffer_list.clear();
@@ -2108,15 +2109,15 @@ TEST_F(TCPv4Tests, non_blocking_send)
21082109

21092110
// Prepare the message
21102111
asio::error_code ec;
2111-
std::vector<octet> message(msg_size * 2, 0);
2112+
std::vector<octet> message(msg_size, 0);
21122113
const octet* data = message.data();
21132114
size_t size = message.size();
21142115
NetworkBuffer buffers(data, size);
21152116
std::vector<NetworkBuffer> buffer_list;
21162117
buffer_list.push_back(buffers);
21172118

2118-
// Send the message with no header. Since TCP actually allocates twice the size of the buffer requested
2119-
// it should be able to send a message of msg_size*2.
2119+
// Send the message with no header. Since TCP actually allocates about twice the size of the buffer requested, and
2120+
// since the threshold to discard (sendBufferSize) is set to msg_size, it should be able to send a message of msg_size.
21202121
size_t bytes_sent = sender_channel_resource->send(nullptr, 0, buffer_list, size, ec);
21212122
ASSERT_EQ(bytes_sent, size);
21222123

@@ -2125,8 +2126,9 @@ TEST_F(TCPv4Tests, non_blocking_send)
21252126
size_t bytes_read = asio::read(socket, asio::buffer(buffer, size), asio::transfer_exactly(size), ec);
21262127
ASSERT_EQ(bytes_read, size);
21272128

2128-
// Now try to send a message that is bigger than the buffer size: (msg_size*2 + 1) + bytes_in_send_buffer(0) > 2*sendBufferSize
2129-
message.resize(msg_size * 2 + 1);
2129+
// Now try to send a message whose size surpasses the threshold to discard (sendBufferSize):
2130+
// (msg_size + 1) + bytes_in_send_buffer(0) > sendBufferSize
2131+
message.resize(msg_size + 1);
21302132
data = message.data();
21312133
size = message.size();
21322134
buffer_list.clear();

test/unittest/transport/TCPv6Tests.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -438,15 +438,15 @@ TEST_F(TCPv6Tests, non_blocking_send)
438438

439439
// Prepare the message
440440
asio::error_code ec;
441-
std::vector<octet> message(msg_size * 2, 0);
441+
std::vector<octet> message(msg_size, 0);
442442
const octet* data = message.data();
443443
size_t size = message.size();
444444
NetworkBuffer buffers(data, size);
445445
std::vector<NetworkBuffer> buffer_list;
446446
buffer_list.push_back(buffers);
447447

448-
// Send the message with no header. Since TCP actually allocates twice the size of the buffer requested
449-
// it should be able to send a message of msg_size*2.
448+
// Send the message with no header. Since TCP actually allocates about twice the size of the buffer requested, and
449+
// since the threshold to discard (sendBufferSize) is set to msg_size, it should be able to send a message of msg_size.
450450
size_t bytes_sent = sender_channel_resource->send(nullptr, 0, buffer_list, size, ec);
451451
ASSERT_EQ(bytes_sent, size);
452452

@@ -455,8 +455,9 @@ TEST_F(TCPv6Tests, non_blocking_send)
455455
size_t bytes_read = asio::read(socket, asio::buffer(buffer, size), asio::transfer_exactly(size), ec);
456456
ASSERT_EQ(bytes_read, size);
457457

458-
// Now try to send a message that is bigger than the buffer size: (msg_size*2 + 1) + bytes_in_send_buffer(0) > 2*sendBufferSize
459-
message.resize(msg_size * 2 + 1);
458+
// Now try to send a message whose size surpasses the threshold to discard (sendBufferSize):
459+
// (msg_size + 1) + bytes_in_send_buffer(0) > sendBufferSize
460+
message.resize(msg_size + 1);
460461
data = message.data();
461462
size = message.size();
462463
buffer_list.clear();

0 commit comments

Comments
 (0)