Move maximize_buffer to NativeFd

This commit is contained in:
iliavy 2024-04-05 21:25:22 +03:00 committed by Aliaksei Levin
parent e08cf00efb
commit 27d105ef51
4 changed files with 45 additions and 44 deletions

View File

@ -789,45 +789,6 @@ const NativeFd &UdpSocketFd::get_native_fd() const {
return get_poll_info().native_fd();
}
static Result<uint32> maximize_buffer(const NativeFd &fd, int optname, uint32 max_size) {
if (setsockopt(fd.socket(), SOL_SOCKET, optname, reinterpret_cast<const char *>(&max_size), sizeof(max_size)) == 0) {
// fast path
return max_size;
}
/* Start with the default size. */
uint32 old_size = 0;
socklen_t intsize = sizeof(old_size);
if (getsockopt(fd.socket(), SOL_SOCKET, optname, reinterpret_cast<char *>(&old_size), &intsize)) {
return OS_SOCKET_ERROR("getsockopt() failed");
}
#if TD_LINUX
old_size /= 2;
#endif
/* Binary-search for the real maximum. */
uint32 last_good_size = old_size;
uint32 min_size = old_size;
while (min_size <= max_size) {
uint32 avg_size = min_size + (max_size - min_size) / 2;
if (setsockopt(fd.socket(), SOL_SOCKET, optname, reinterpret_cast<const char *>(&avg_size), sizeof(avg_size)) == 0) {
last_good_size = avg_size;
min_size = avg_size + 1;
} else {
max_size = avg_size - 1;
}
}
return last_good_size;
}
Result<uint32> UdpSocketFd::maximize_snd_buffer(uint32 max_size) {
return maximize_buffer(get_native_fd(), SO_SNDBUF, max_size == 0 ? DEFAULT_UDP_MAX_SND_BUFFER_SIZE : max_size);
}
Result<uint32> UdpSocketFd::maximize_rcv_buffer(uint32 max_size) {
return maximize_buffer(get_native_fd(), SO_RCVBUF, max_size == 0 ? DEFAULT_UDP_MAX_RCV_BUFFER_SIZE : max_size);
}
#if TD_PORT_POSIX
Status UdpSocketFd::send_message(const OutboundMessage &message, bool &is_sent) {
return impl_->send_message(message, is_sent);

View File

@ -44,9 +44,6 @@ class UdpSocketFd {
UdpSocketFd(const UdpSocketFd &) = delete;
UdpSocketFd &operator=(const UdpSocketFd &) = delete;
Result<uint32> maximize_snd_buffer(uint32 max_size = 0);
Result<uint32> maximize_rcv_buffer(uint32 max_size = 0);
static Result<UdpSocketFd> open(const IPAddress &address) TD_WARN_UNUSED_RESULT;
PollableFdInfo &get_poll_info();
@ -83,8 +80,6 @@ class UdpSocketFd {
#endif
private:
static constexpr uint32 DEFAULT_UDP_MAX_SND_BUFFER_SIZE = (1 << 24);
static constexpr uint32 DEFAULT_UDP_MAX_RCV_BUFFER_SIZE = (1 << 24);
std::unique_ptr<detail::UdpSocketFdImpl, detail::UdpSocketFdImplDeleter> impl_;
explicit UdpSocketFd(unique_ptr<detail::UdpSocketFdImpl> impl);
};

View File

@ -13,6 +13,7 @@
#if TD_PORT_POSIX
#include <fcntl.h>
#include <unistd.h>
#include <sys/socket.h>
#endif
#if TD_FD_DEBUG
@ -223,6 +224,45 @@ Status NativeFd::duplicate(const NativeFd &to) const {
#endif
}
static Result<uint32> maximize_buffer(const NativeFd::Socket &socket, int optname, uint32 max_size) {
if (setsockopt(socket, SOL_SOCKET, optname, reinterpret_cast<const char *>(&max_size), sizeof(max_size)) == 0) {
// fast path
return max_size;
}
/* Start with the default size. */
uint32 old_size = 0;
socklen_t intsize = sizeof(old_size);
if (getsockopt(socket, SOL_SOCKET, optname, reinterpret_cast<char *>(&old_size), &intsize)) {
return OS_SOCKET_ERROR("getsockopt() failed");
}
#if TD_LINUX
old_size /= 2;
#endif
/* Binary-search for the real maximum. */
uint32 last_good_size = old_size;
uint32 min_size = old_size;
while (min_size <= max_size) {
uint32 avg_size = min_size + (max_size - min_size) / 2;
if (setsockopt(socket, SOL_SOCKET, optname, reinterpret_cast<const char *>(&avg_size), sizeof(avg_size)) == 0) {
last_good_size = avg_size;
min_size = avg_size + 1;
} else {
max_size = avg_size - 1;
}
}
return last_good_size;
}
Result<uint32> NativeFd::maximize_snd_buffer(uint32 max_size) const {
return maximize_buffer(socket(), SO_SNDBUF, max_size == 0 ? DEFAULT_MAX_SND_BUFFER_SIZE : max_size);
}
Result<uint32> NativeFd::maximize_rcv_buffer(uint32 max_size) const {
return maximize_buffer(socket(), SO_RCVBUF, max_size == 0 ? DEFAULT_MAX_RCV_BUFFER_SIZE : max_size);
}
void NativeFd::close() {
if (!*this) {
return;

View File

@ -49,6 +49,9 @@ class NativeFd {
Status duplicate(const NativeFd &to) const;
Result<uint32> maximize_snd_buffer(uint32 max_size = 0) const;
Result<uint32> maximize_rcv_buffer(uint32 max_size = 0) const;
void close();
Fd release();
@ -61,6 +64,8 @@ class NativeFd {
#if TD_PORT_WINDOWS
bool is_socket_{false};
#endif
static constexpr uint32 DEFAULT_MAX_SND_BUFFER_SIZE = (1 << 24);
static constexpr uint32 DEFAULT_MAX_RCV_BUFFER_SIZE = (1 << 24);
};
StringBuilder &operator<<(StringBuilder &sb, const NativeFd &fd);