[collectd] [PATCH] plugin.c: prevent re-adding read functions

Blaise Tarr blaise.tarr at gmail.com
Mon Jan 10 23:15:47 CET 2011


When multiple LoadPlugin options are used with a plugin, the plugin is
only loaded once, however the plugin's read function is invoked
multiple times at every interval. This in turn causes undesired
redundant data to be sent to the write plugins.

This patch prevents a plugin's read function from being re-added to
the read_heap and read_list during plugin registration.

Signed-off-by: Blaise Tarr <blaise.tarr at gmail.com>
---
 src/plugin.c |   38 ++++++++++++++++++++++++--------------
 1 files changed, 24 insertions(+), 14 deletions(-)

diff --git a/src/plugin.c b/src/plugin.c
index af894d5..7756327 100644
--- a/src/plugin.c
+++ b/src/plugin.c
@@ -734,26 +734,36 @@ static int plugin_insert_read (read_func_t *rf)
 		}
 	}
 
-	le = llentry_create (rf->rf_name, rf);
+	le = llist_search (read_list, rf->rf_name);
 	if (le == NULL)
 	{
-		pthread_mutex_unlock (&read_lock);
-		ERROR ("plugin_insert_read: llentry_create failed.");
-		return (-1);
-	}
+		le = llentry_create (rf->rf_name, rf);
+		if (le == NULL)
+		{
+			pthread_mutex_unlock (&read_lock);
+			ERROR ("plugin_insert_read: llentry_create failed.");
+			return (-1);
+		}
+
+		status = c_heap_insert (read_heap, rf);
+		if (status != 0)
+		{
+			pthread_mutex_unlock (&read_lock);
+			ERROR ("plugin_insert_read: c_heap_insert failed.");
+			llentry_destroy (le);
+			return (-1);
+		}
 
-	status = c_heap_insert (read_heap, rf);
-	if (status != 0)
+		/* This does not fail. */
+		llist_append (read_list, le);
+	}
+	else
 	{
-		pthread_mutex_unlock (&read_lock);
-		ERROR ("plugin_insert_read: c_heap_insert failed.");
-		llentry_destroy (le);
-		return (-1);
+		INFO ("plugin: plugin_insert_read: "
+				"read function for plugin `%s' already added.",
+				rf->rf_name);
 	}
 
-	/* This does not fail. */
-	llist_append (read_list, le);
-
 	pthread_mutex_unlock (&read_lock);
 	return (0);
 } /* int plugin_insert_read */
-- 
1.7.3.4




More information about the collectd mailing list