Code

write_graphite plugin: Fix locking.
[collectd.git] / src / write_graphite.c
index 502e22c31fa2ebfc47c8d172f41be3e2d1645c62..6b16bec215539245220fa662352604aad4ecef83 100644 (file)
@@ -81,7 +81,6 @@
 struct wg_callback
 {
     int      sock_fd;
-    struct hostent *server;
 
     char    *node;
     char    *service;
@@ -121,20 +120,15 @@ static int wg_send_buffer (struct wg_callback *cb)
                 status,
                 strerror (errno));
 
-        pthread_mutex_trylock (&cb->send_lock);
-
-        DEBUG ("write_graphite plugin: closing socket and restting fd "
-                "so reinit will occur");
         close (cb->sock_fd);
         cb->sock_fd = -1;
 
-        pthread_mutex_unlock (&cb->send_lock);
-
         return (-1);
     }
     return (0);
 }
 
+/* NOTE: You must hold cb->send_lock when calling this function! */
 static int wg_flush_nolock (cdtime_t timeout, struct wg_callback *cb)
 {
     int status;
@@ -241,14 +235,20 @@ static void wg_callback_free (void *data)
 
     cb = data;
 
+    pthread_mutex_lock (&cb->send_lock);
+
     wg_flush_nolock (/* timeout = */ 0, cb);
 
     close(cb->sock_fd);
+    cb->sock_fd = -1;
+
     sfree(cb->node);
     sfree(cb->service);
     sfree(cb->prefix);
     sfree(cb->postfix);
 
+    pthread_mutex_destroy (&cb->send_lock);
+
     sfree(cb);
 }
 
@@ -636,7 +636,6 @@ static int wg_config_carbon (oconfig_item_t *ci)
     cb->service = NULL;
     cb->prefix = NULL;
     cb->postfix = NULL;
-    cb->server = NULL;
     cb->escape_char = '_';
 
     pthread_mutex_init (&cb->send_lock, /* attr = */ NULL);