Skip to content

Commit

Permalink
feat(stats): extend connection support to IPv6, bail out earlier
Browse files Browse the repository at this point in the history
Offloading `sockaddr` generation to `CService` lets us borrow its IPv6
`sockaddr` generation code but this requires us to first parse the host
before creating a socket.

As a bonus, 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.
  • Loading branch information
kwvg committed Sep 5, 2024
1 parent f3b12a6 commit 2ed309a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
31 changes: 18 additions & 13 deletions src/statsd_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,12 @@ 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<Sock>(hSocket);

CNetAddr netaddr;
if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/true)) {
if (!LookupHost(m_host, netaddr, /*fAllowLookup=*/false)) {
LogPrintf("ERROR: Unable to lookup host %s, cannot init StatsdClient\n", m_host);
return;
}
if (!netaddr.IsIPv4()) {
if (!netaddr.IsIPv4() && !netaddr.IsIPv6()) {
LogPrintf("ERROR: Host %s on unsupported network, cannot init StatsdClient\n", m_host);
return;
}
Expand All @@ -92,7 +84,13 @@ StatsdClient::StatsdClient(const std::string& host, const std::string& nodename,
return;
}

m_init = true;
SOCKET hSocket = ::socket(reinterpret_cast<struct sockaddr*>(&m_server.first)->sa_family, 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);
}
Expand Down Expand Up @@ -140,6 +138,10 @@ int StatsdClient::timing(const std::string& key, uint32_t ms, float sample_rate)

int StatsdClient::send(std::string key, uint32_t value, const std::string& type, float sample_rate)
{
if (!m_sock) {
return -3;
}

if (!ShouldSend(sample_rate)) {
return 0;
}
Expand All @@ -160,6 +162,10 @@ int StatsdClient::send(std::string key, uint32_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;
}
Expand All @@ -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<struct sockaddr*>(&m_server.first), m_server.second) == SOCKET_ERROR) {
Expand Down
1 change: 0 additions & 1 deletion src/statsd_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ class StatsdClient {
mutable Mutex cs;
mutable FastRandomContext insecure_rand GUARDED_BY(cs);

bool m_init{false};
std::unique_ptr<Sock> m_sock{nullptr};
std::pair<struct sockaddr_storage, socklen_t> m_server{{}, sizeof(struct sockaddr_storage)};

Expand Down

0 comments on commit 2ed309a

Please sign in to comment.