From 1a81979c1e8cf47df28d785689b4049e1504d0d1 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Wed, 11 Sep 2024 14:32:46 +0000 Subject: [PATCH] stats: initialize socket after we have a valid socket address As creating the socket is now the last step, we don't need `m_init` anymore. We can just see if a socket is successfully constructed and take that as our validity indicator. We'll also move it out of the inner `send` function so we can bail out before we bother with all the string processing and manipulation. --- src/statsd_client.cpp | 27 ++++++++++++++++----------- src/statsd_client.h | 1 - 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 5e1ef4376c..925e47cd3e 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -70,14 +70,6 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, return; } - SOCKET hSocket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - if (hSocket == INVALID_SOCKET) { - LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n", - NetworkErrorString(WSAGetLastError())); - return; - } - m_sock = std::make_unique(hSocket); - CNetAddr netaddr; if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) { LogPrintf("ERROR: Unable to lookup host %s, cannot init StatsdClient\n", m_host); @@ -92,7 +84,13 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename, return; } - m_init = true; + SOCKET hSocket = ::socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + if (hSocket == INVALID_SOCKET) { + LogPrintf("ERROR: Cannot create socket (socket() returned error %s), cannot init StatsdClient\n", + NetworkErrorString(WSAGetLastError())); + return; + } + m_sock = std::make_unique(hSocket); LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port); } @@ -140,6 +138,10 @@ int StatsdClient::timing(const std::string& key, int64_t ms, float sample_rate) int StatsdClient::send(std::string key, int64_t value, const std::string& type, float sample_rate) { + if (!m_sock) { + return -3; + } + if (!ShouldSend(sample_rate)) { return 0; } @@ -160,6 +162,10 @@ int StatsdClient::send(std::string key, int64_t value, const std::string& type, int StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate) { + if (!m_sock) { + return -3; + } + if (!ShouldSend(sample_rate)) { return 0; } @@ -180,8 +186,7 @@ int StatsdClient::sendDouble(std::string key, double value, const std::string& t int StatsdClient::send(const std::string& message) { - if (!m_init) - return -3; + assert(m_sock); if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0, reinterpret_cast(&m_server.first), m_server.second) == SOCKET_ERROR) { diff --git a/src/statsd_client.h b/src/statsd_client.h index 4bf92fa31e..95ff75b2af 100644 --- a/src/statsd_client.h +++ b/src/statsd_client.h @@ -63,7 +63,6 @@ class StatsdClient { mutable Mutex cs; mutable FastRandomContext insecure_rand GUARDED_BY(cs); - bool m_init{false}; std::unique_ptr m_sock{nullptr}; std::pair m_server{{}, sizeof(struct sockaddr_storage)};