Skip to content

Commit

Permalink
stats plugin: Don't send any stats before CONNECT was successfully sent.
Browse files Browse the repository at this point in the history
After stats write failures this fixes warnings like:
Warning: stats: Couldn't find session ID: smqAXQE8pIp/AAAB
  • Loading branch information
sirainen committed Sep 9, 2016
1 parent e19ba44 commit 853992a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
13 changes: 8 additions & 5 deletions src/plugins/stats/mail-stats-connection.c
Expand Up @@ -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);
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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);
}
4 changes: 2 additions & 2 deletions src/plugins/stats/mail-stats-connection.h
Expand Up @@ -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);

Expand Down
13 changes: 10 additions & 3 deletions src/plugins/stats/stats-plugin.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/plugins/stats/stats-plugin.h
Expand Up @@ -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;
Expand Down

0 comments on commit 853992a

Please sign in to comment.