From: Timo Sirainen Date: Thu, 8 Sep 2016 17:23:35 +0000 (+0300) Subject: stats plugin: Don't send any stats before CONNECT was successfully sent. X-Git-Tag: 2.2.26~294 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=853992ac27d2a110abfd5b203997a20b7a306ea7;p=thirdparty%2Fdovecot%2Fcore.git 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 --- diff --git a/src/plugins/stats/mail-stats-connection.c b/src/plugins/stats/mail-stats-connection.c index f3103be80e..a768eb470b 100644 --- a/src/plugins/stats/mail-stats-connection.c +++ b/src/plugins/stats/mail-stats-connection.c @@ -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 @@ void mail_stats_connection_connect(struct stats_connection *conn, 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 @@ void mail_stats_connection_disconnect(struct stats_connection *conn, 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 @@ void mail_stats_connection_send_session(struct stats_connection *conn, base64_encode(buf->data, buf->used, str); str_append_c(str, '\n'); - stats_connection_send(conn, str); + (void)stats_connection_send(conn, str); } diff --git a/src/plugins/stats/mail-stats-connection.h b/src/plugins/stats/mail-stats-connection.h index 3b3362b580..f98b691b0f 100644 --- a/src/plugins/stats/mail-stats-connection.h +++ b/src/plugins/stats/mail-stats-connection.h @@ -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); diff --git a/src/plugins/stats/stats-plugin.c b/src/plugins/stats/stats-plugin.c index 283817b0bd..efb796690c 100644 --- a/src/plugins/stats/stats-plugin.c +++ b/src/plugins/stats/stats-plugin.c @@ -125,7 +125,12 @@ static void session_stats_refresh(struct mail_user *user) 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 @@ static void stats_user_deinit(struct mail_user *user) 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 @@ static void stats_user_created(struct mail_user *user) 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); diff --git a/src/plugins/stats/stats-plugin.h b/src/plugins/stats/stats-plugin.h index 776cc8f9e6..bbf437fa29 100644 --- a/src/plugins/stats/stats-plugin.h +++ b/src/plugins/stats/stats-plugin.h @@ -14,6 +14,7 @@ struct stats_user { 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;