From ea38df2c2888c55feea7383af7f7913d2e6d2481 Mon Sep 17 00:00:00 2001 From: Arthur Passos Date: Thu, 26 Jan 2023 13:54:12 -0300 Subject: [PATCH] add comments to the code --- src/Common/CaresPTRResolver.cpp | 32 +++++++++++++++++--------------- src/Common/CaresPTRResolver.h | 5 ++++- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/Common/CaresPTRResolver.cpp b/src/Common/CaresPTRResolver.cpp index 31edd3beb0d..600ddf44694 100644 --- a/src/Common/CaresPTRResolver.cpp +++ b/src/Common/CaresPTRResolver.cpp @@ -82,7 +82,7 @@ namespace DB resolve(ip, ptr_records); - if (!wait()) + if (!wait_and_process()) { cancel_requests(); throw DB::Exception("Failed to complete reverse DNS query for IP " + ip, DB::ErrorCodes::DNS_ERROR); @@ -99,7 +99,7 @@ namespace DB resolve_v6(ip, ptr_records); - if (!wait()) + if (!wait_and_process()) { cancel_requests(); throw DB::Exception("Failed to complete reverse DNS query for IP " + ip, DB::ErrorCodes::DNS_ERROR); @@ -125,7 +125,7 @@ namespace DB ares_gethostbyaddr(channel, reinterpret_cast(&addr), sizeof(addr), AF_INET6, callback, &response); } - bool CaresPTRResolver::wait() + bool CaresPTRResolver::wait_and_process() { int sockets[ARES_GETSOCK_MAXNUM]; pollfd pollfd[ARES_GETSOCK_MAXNUM]; @@ -140,17 +140,19 @@ namespace DB { number_of_fds_ready = poll(readable_sockets.data(), static_cast(readable_sockets.size()), static_cast(timeout)); - // will add comments/ improve readability soon - if (number_of_fds_ready < 0) + bool poll_error = number_of_fds_ready < 0; + bool is_poll_error_an_interrupt = poll_error && errno == EINTR; + + /* + * Retry in case of interrupts and return false in case of actual errors. + * */ + if (is_poll_error_an_interrupt) { - if (errno == EINTR) - { - continue; - } - else - { - return false; - } + continue; + } + else if (poll_error) + { + return false; } } @@ -187,7 +189,7 @@ namespace DB if (ARES_GETSOCK_READABLE(sockets_bitmask, i)) { pollfd[i].fd = sockets[i]; - pollfd[i].events = POLLRDNORM | POLLIN; + pollfd[i].events = C_ARES_POLL_EVENTS; } if (pollfd[i].events) @@ -227,7 +229,7 @@ namespace DB { for (auto readable_socket : readable_sockets) { - auto fd = readable_socket.revents & (POLLRDNORM | POLLIN) ? readable_socket.fd : ARES_SOCKET_BAD; + auto fd = readable_socket.revents & C_ARES_POLL_EVENTS ? readable_socket.fd : ARES_SOCKET_BAD; ares_process_fd(channel, fd, ARES_SOCKET_BAD); } } diff --git a/src/Common/CaresPTRResolver.h b/src/Common/CaresPTRResolver.h index 436542f9fd0..454509ae43c 100644 --- a/src/Common/CaresPTRResolver.h +++ b/src/Common/CaresPTRResolver.h @@ -23,6 +23,9 @@ namespace DB * Allow only DNSPTRProvider to instantiate this class * */ struct provider_token {}; + + static constexpr auto C_ARES_POLL_EVENTS = POLLRDNORM | POLLIN; + public: explicit CaresPTRResolver(provider_token); ~CaresPTRResolver() override; @@ -32,7 +35,7 @@ namespace DB std::unordered_set resolve_v6(const std::string & ip) override; private: - bool wait(); + bool wait_and_process(); void cancel_requests();