diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 1fbadfa..eb7de70 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -50,6 +50,16 @@ StatsdClient::StatsdClient(const string& host, int port, const string& ns) config(host, port, ns); srandom(time(NULL)); } + +StatsdClient::StatsdClient(const StatsdClient &original) +{ + // Must create our own storage for ClientData object; don't share original's + d = new _StatsdClientData; + *d = *original.d; + + // Don't share original's socket, either. + d->sock = -1; +} StatsdClient::~StatsdClient() { @@ -84,6 +94,13 @@ int StatsdClient::init() return -1; } + // Don't block on stats sends.... + int flags = fcntl(d->sock, F_GETFL, 0); + if (fcntl(d->sock, F_SETFL, flags | O_NONBLOCK) == -1) { + snprintf(d->errmsg, sizeof(d->errmsg), "could not enabled non blocking IO, err=%m"); + return -1; + } + memset(&d->server, 0, sizeof(d->server)); d->server.sin_family = AF_INET; d->server.sin_port = htons(d->port); diff --git a/src/statsd_client.h b/src/statsd_client.h index 9e97e83..9b0224d 100644 --- a/src/statsd_client.h +++ b/src/statsd_client.h @@ -14,6 +14,7 @@ struct _StatsdClientData; class StatsdClient { public: StatsdClient(const std::string& host="127.0.0.1", int port=8125, const std::string& ns = ""); + StatsdClient(const StatsdClient &original); ~StatsdClient(); public: