changeset 20707:a9bdefa7d022

stats plugin: Don't send any stats before CONNECT was successfully sent. After stats write failures this fixes warnings like: Warning: stats: Couldn't find session ID: smqAXQE8pIp/AAAB
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Thu, 08 Sep 2016 20:23:35 +0300
parents 85fe66810c94
children f013cf80c12c
files src/plugins/stats/mail-stats-connection.c src/plugins/stats/mail-stats-connection.h src/plugins/stats/stats-plugin.c src/plugins/stats/stats-plugin.h
diffstat 4 files changed, 21 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/stats/mail-stats-connection.c	Thu Sep 08 20:18:46 2016 +0300
+++ b/src/plugins/stats/mail-stats-connection.c	Thu Sep 08 20:23:35 2016 +0300
@@ -11,8 +11,8 @@
 #include "stats-plugin.h"
 #include "mail-stats-connection.h"
 
-void mail_stats_connection_connect(struct stats_connection *conn,
-				   struct mail_user *user)
+int mail_stats_connection_connect(struct stats_connection *conn,
+				  struct mail_user *user)
 {
 	struct stats_user *suser = STATS_USER_CONTEXT(user);
 	string_t *str = t_str_new(128);
@@ -36,7 +36,7 @@
 		str_append(str, net_ip2addr(user->remote_ip));
 	}
 	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
+	return stats_connection_send(conn, str);
 }
 
 void mail_stats_connection_disconnect(struct stats_connection *conn,
@@ -48,7 +48,10 @@
 	str_append(str, "DISCONNECT\t");
 	str_append(str, suser->stats_session_id);
 	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
+	if (stats_connection_send(conn, str) < 0) {
+		/* we could retry this later, but stats process will forget it
+		   anyway after 15 minutes. */
+	}
 }
 
 void mail_stats_connection_send_session(struct stats_connection *conn,
@@ -68,5 +71,5 @@
 	base64_encode(buf->data, buf->used, str);
 
 	str_append_c(str, '\n');
-	stats_connection_send(conn, str);
+	(void)stats_connection_send(conn, str);
 }
--- a/src/plugins/stats/mail-stats-connection.h	Thu Sep 08 20:18:46 2016 +0300
+++ b/src/plugins/stats/mail-stats-connection.h	Thu Sep 08 20:23:35 2016 +0300
@@ -6,8 +6,8 @@
 struct mail_stats;
 struct mail_user;
 
-void mail_stats_connection_connect(struct stats_connection *conn,
-				   struct mail_user *user);
+int mail_stats_connection_connect(struct stats_connection *conn,
+				  struct mail_user *user);
 void mail_stats_connection_disconnect(struct stats_connection *conn,
 				      struct mail_user *user);
 
--- a/src/plugins/stats/stats-plugin.c	Thu Sep 08 20:18:46 2016 +0300
+++ b/src/plugins/stats/stats-plugin.c	Thu Sep 08 20:23:35 2016 +0300
@@ -125,7 +125,12 @@
 	time_t now = time(NULL);
 	bool changed;
 
-	if (session_stats_need_send(suser, now, &changed, &to_next_secs)) {
+	if (!suser->stats_connected) {
+		if (mail_stats_connection_connect(suser->stats_conn, user) == 0)
+			suser->stats_connected = TRUE;
+	}
+	if (session_stats_need_send(suser, now, &changed, &to_next_secs) &&
+	    suser->stats_connected) {
 		suser->session_sent_duplicate = !changed;
 		suser->last_session_update = now;
 		stats_copy(suser->last_sent_session_stats, suser->session_stats);
@@ -333,7 +338,8 @@
 					 stats_io_deactivate, user);
 	/* send final stats before disconnection */
 	session_stats_refresh(user);
-	mail_stats_connection_disconnect(stats_conn, user);
+	if (suser->stats_connected)
+		mail_stats_connection_disconnect(stats_conn, user);
 
 	if (suser->to_stats_timeout != NULL)
 		timeout_remove(&suser->to_stats_timeout);
@@ -437,7 +443,8 @@
 	suser->last_sent_session_stats = stats_alloc(user->pool);
 
 	MODULE_CONTEXT_SET(user, stats_user_module, suser);
-	mail_stats_connection_connect(suser->stats_conn, user);
+	if (mail_stats_connection_connect(suser->stats_conn, user) == 0)
+		suser->stats_connected = TRUE;
 	suser->to_stats_timeout =
 		timeout_add(suser->refresh_secs*1000,
 			    session_stats_refresh_timeout, user);
--- a/src/plugins/stats/stats-plugin.h	Thu Sep 08 20:18:46 2016 +0300
+++ b/src/plugins/stats/stats-plugin.h	Thu Sep 08 20:23:35 2016 +0300
@@ -14,6 +14,7 @@
 	struct ioloop_context *ioloop_ctx;
 	struct stats_connection *stats_conn;
 	const char *stats_session_id;
+	bool stats_connected;
 
 	unsigned int refresh_secs;
 	bool track_commands;