diff options
author | Costa Tsaousis <costa@netdata.cloud> | 2023-06-29 15:02:13 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-29 15:02:13 +0300 |
commit | c62dcb2a9bd8ca6ec0c483bb3506c733d96648c6 (patch) | |
tree | 15f9e27879cfb951fa853fcb4f570066cb7f54a6 /libnetdata | |
parent | a1503807bb57399eae83606146281036da25e610 (diff) |
Optimizations part 2 (#15280)
* make all pluginsd functions inline, instead of function pointers
* dynamic MRG partitions based on the number of CPUs
* report the right size of the MRG
* prevent invalid read on pluginsd exit
* faster service_running() check; fix compiler warnings; shutdown replication after streaming to prevent crash on shutdown
* sender is now using a spinlock
* rrdcontext uses spinlock
* replace select() with poll()
* signed calculation of threads
* disable read-ahead on jnfv2 files during scan
Diffstat (limited to 'libnetdata')
-rw-r--r-- | libnetdata/socket/socket.c | 35 |
1 files changed, 25 insertions, 10 deletions
diff --git a/libnetdata/socket/socket.c b/libnetdata/socket/socket.c index f8927b4550..f52aa9c815 100644 --- a/libnetdata/socket/socket.c +++ b/libnetdata/socket/socket.c @@ -812,21 +812,36 @@ int connect_to_this_ip46(int protocol, int socktype, const char *host, uint32_t if(errno == EALREADY || errno == EINPROGRESS) { info("Waiting for connection to ip %s port %s to be established", hostBfr, servBfr); - fd_set fds; - FD_ZERO(&fds); - FD_SET(0, &fds); - int rc = select (1, NULL, &fds, NULL, timeout); - - if(rc > 0 && FD_ISSET(fd, &fds)) { - info("connect() to ip %s port %s completed successfully", hostBfr, servBfr); + // Convert 'struct timeval' to milliseconds for poll(): + int timeout_milliseconds = timeout->tv_sec * 1000 + timeout->tv_usec / 1000; + + struct pollfd fds[1]; + fds[0].fd = fd; + fds[0].events = POLLOUT; // We are looking for the ability to write to the socket + + int ret = poll(fds, 1, timeout_milliseconds); + if (ret > 0) { + // poll() completed normally. We can check the revents to see what happened + if (fds[0].revents & POLLOUT) { + // connect() completed successfully, socket is writable. + info("connect() to ip %s port %s completed successfully", hostBfr, servBfr); + } + else { + // This means that the socket is in error. We will close it and set fd to -1 + error("Failed to connect to '%s', port '%s'.", hostBfr, servBfr); + close(fd); + fd = -1; + } } - else if(rc == -1) { - error("Failed to connect to '%s', port '%s'. select() returned %d", hostBfr, servBfr, rc); + else if (ret == 0) { + // poll() timed out, the connection is not established within the specified timeout. + error("Timed out while connecting to '%s', port '%s'.", hostBfr, servBfr); close(fd); fd = -1; } else { - error("Timed out while connecting to '%s', port '%s'. select() returned %d", hostBfr, servBfr, rc); + // poll() returned an error. + error("Failed to connect to '%s', port '%s'. poll() returned %d", hostBfr, servBfr, ret); close(fd); fd = -1; } |