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

Setting TCP_QUICKACK reduces trylock latency by 40ms. #254

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions src/util/network/tcp_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <array>
#include <cstring>
#include <unistd.h>
#include <netinet/tcp.h> // for TCP_NODELAY, TCP_QUICKACK

namespace cbdc::network {
auto tcp_socket::connect(const endpoint_t& ep) -> bool {
Expand Down Expand Up @@ -36,6 +37,10 @@ namespace cbdc::network {
continue;
}

static constexpr int one = 1;
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); // sending side. needed?
Copy link
Collaborator

@anders94 anders94 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should check this.

setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side

break;
}

Expand Down Expand Up @@ -88,13 +93,18 @@ namespace cbdc::network {
}

auto tcp_socket::receive(buffer& pkt) const -> bool {
static constexpr int one = 1;
// apparently TCP_QUICKACK needs to be re-set after each read (incurring a syscall...)
// cf. https://github.com/netty/netty/issues/13610

uint64_t pkt_sz{};
std::array<std::byte, sizeof(pkt_sz)> sz_buf{};
uint64_t total_read{0};
while(total_read != sz_buf.size()) {
auto n = read(m_sock_fd,
&sz_buf.at(total_read),
sizeof(pkt_sz) - total_read);
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side
if(n <= 0) {
return false;
}
Expand All @@ -109,6 +119,7 @@ namespace cbdc::network {
while(total_read < pkt_sz) {
const auto buf_sz = pkt_sz - total_read;
auto n = read(m_sock_fd, buf.data(), buf_sz);
setsockopt(m_sock_fd, IPPROTO_TCP, TCP_QUICKACK, &one, sizeof(one)); // receiving side
if(n <= 0) {
return false;
}
Expand Down
Loading