From 0d0ac4aa7ab8cdaaf97009c4ff01bef955bfd45a Mon Sep 17 00:00:00 2001 From: James Mulcahy Date: Thu, 18 Sep 2014 15:49:38 +0100 Subject: [PATCH 1/2] Implement copy constructor to prevent malloc errors Fixes #1 --- src/statsd_client.cpp | 10 ++++++++++ src/statsd_client.h | 1 + 2 files changed, 11 insertions(+) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 1fbadfa..234f31b 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() { 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: From 302ecd73e55e2eb6b71c12c0778af23337575c1c Mon Sep 17 00:00:00 2001 From: James Mulcahy Date: Thu, 18 Sep 2014 16:11:57 +0100 Subject: [PATCH 2/2] Use Non-Blocking IO on StatsD Socket --- src/statsd_client.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/statsd_client.cpp b/src/statsd_client.cpp index 234f31b..eb7de70 100644 --- a/src/statsd_client.cpp +++ b/src/statsd_client.cpp @@ -94,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);