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.
This commit is contained in:
Kittywhiskers Van Gogh 2024-09-11 14:32:46 +00:00
parent dbbfc8d766
commit 1a81979c1e
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
2 changed files with 16 additions and 12 deletions

View File

@ -70,14 +70,6 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
return; 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<Sock>(hSocket);
CNetAddr netaddr; CNetAddr netaddr;
if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) { if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) {
LogPrintf("ERROR: Unable to lookup host %s, cannot init StatsdClient\n", m_host); 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; 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<Sock>(hSocket);
LogPrintf("StatsdClient initialized to transmit stats to %s:%d\n", m_host, m_port); 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) 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)) { if (!ShouldSend(sample_rate)) {
return 0; 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) int StatsdClient::sendDouble(std::string key, double value, const std::string& type, float sample_rate)
{ {
if (!m_sock) {
return -3;
}
if (!ShouldSend(sample_rate)) { if (!ShouldSend(sample_rate)) {
return 0; 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) int StatsdClient::send(const std::string& message)
{ {
if (!m_init) assert(m_sock);
return -3;
if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0, if (::sendto(m_sock->Get(), message.data(), message.size(), /*flags=*/0,
reinterpret_cast<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) { reinterpret_cast<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) {

View File

@ -63,7 +63,6 @@ class StatsdClient {
mutable Mutex cs; mutable Mutex cs;
mutable FastRandomContext insecure_rand GUARDED_BY(cs); mutable FastRandomContext insecure_rand GUARDED_BY(cs);
bool m_init{false};
std::unique_ptr<Sock> m_sock{nullptr}; std::unique_ptr<Sock> m_sock{nullptr};
std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)}; std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)};