From 5b5c0bd49631a9102fdc443295cde115c716e597 Mon Sep 17 00:00:00 2001 From: Kefu Chai Date: Sun, 6 Oct 2024 20:39:09 +0800 Subject: [PATCH] dns: replace deprecated ares_process() with ares_process_fd() c-ares marked `ares_process()` deprecated in 1.28.1 in this change, we: - update to use ares_process_fd() following c-ares 1.28.1 deprecation - improve code readability: * rename variables for clarity * move variable definition closer to usage. no performance impact caused by this change, as `fd_set` structs remain stack-allocated - allow removing socket when iterating over `_sockets`. `ares_process_fd()` could call `do_close()` if a certain socket should be cleaned up, so we cannot use a regular range-based for loop to iterate over `_sockets`. this change modernizes our c-ares usage and enhances code maintainability without introducing overhead. Refs #2197 Signed-off-by: Kefu Chai --- src/net/dns.cc | 67 +++++++++++++++++++++++++------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/src/net/dns.cc b/src/net/dns.cc index 9984502e77..60a2291a3c 100644 --- a/src/net/dns.cc +++ b/src/net/dns.cc @@ -594,51 +594,50 @@ dns_resolver::impl::end_call() { void dns_resolver::impl::poll_sockets() { - fd_set readers, writers; - int n = 0; - dns_log.trace("Poll sockets"); - do { + bool processed = false; + for (;;) { // Retrieve the set of file descriptors that the library wants us to monitor. + fd_set readers, writers; FD_ZERO(&readers); FD_ZERO(&writers); - n = ares_fds(_channel, &readers, &writers); - - dns_log.trace("ares_fds: {}", n); - - if (n == 0) { + int nr_fds = ares_fds(_channel, &readers, &writers); + dns_log.trace("ares_fds: {}", nr_fds); + if (nr_fds == 0) { break; } - n = 0; - - for (auto & p : _sockets) { - auto & e = p.second; - auto fd = p.first; - auto r = FD_ISSET(p.first, &readers); - auto w = FD_ISSET(p.first, &writers); - auto ra = e.avail & POLLIN; - auto wa = e.avail & POLLOUT; - - dns_log.trace("fd {} {}{}/{}{}", fd, (r ? "r" : ""), - (w ? "w" : ""), (ra ? "r" : ""), - (wa ? "w" : "")); - - if (!wa) { - FD_CLR(fd, &writers); - } - if (!ra) { - FD_CLR(fd, &readers); - } - if (FD_ISSET(fd, &writers) || FD_ISSET(fd, &readers)) { - ++n; + int processed_fds = 0; + for (auto it = _sockets.begin(); it != _sockets.end();) { + auto& [fd, e] = *it++; + bool read_monitor = FD_ISSET(fd, &readers); + bool write_monitor = FD_ISSET(fd, &writers); + bool read_avail = e.avail & POLLIN; + bool write_avail = e.avail & POLLOUT; + + dns_log.trace("fd {} {}{}/{}{}", fd, + read_monitor ? "r" : "", + write_monitor ? "w" : "", + read_avail ? "r" : "", + write_avail ? "w" : ""); + + ares_socket_t read_fd = read_monitor && read_avail ? fd : ARES_SOCKET_BAD; + ares_socket_t write_fd = write_monitor && write_avail ? fd : ARES_SOCKET_BAD; + if (read_fd != ARES_SOCKET_BAD || write_fd != ARES_SOCKET_BAD) { + ares_process_fd(_channel, read_fd, write_fd); + ++processed_fds; } } - - ares_process(_channel, &readers, &writers); - } while (n != 0); + if (processed_fds == 0) { + break; + } + processed = true; + } + if (!processed) { + ares_process_fd(_channel, ARES_SOCKET_BAD, ARES_SOCKET_BAD); + } } dns_resolver::srv_records