]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
Implement the concept of fuzzy protocol epoches.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 21 Apr 2015 17:19:40 +0000 (18:19 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Tue, 21 Apr 2015 17:19:40 +0000 (18:19 +0100)
src/fuzzy_storage.c
src/main.h

index 22b61256a334b5738fa646d381dbe1230afd3e97..4f86fbd9400288c1de629ce8306443c68e879b1f 100644 (file)
@@ -160,7 +160,8 @@ rspamd_fuzzy_write_reply (struct fuzzy_session *session,
 }
 
 static void
-rspamd_fuzzy_process_command (struct fuzzy_session *session)
+rspamd_fuzzy_process_command (struct fuzzy_session *session,
+               enum rspamd_fuzzy_epoch epoch)
 {
        struct rspamd_fuzzy_reply rep = {0, 0, 0, 0.0};
        gboolean res = FALSE;
@@ -168,6 +169,12 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session)
        if (session->cmd->cmd == FUZZY_CHECK) {
                rep = rspamd_fuzzy_backend_check (session->ctx->backend, session->cmd,
                                session->ctx->expire);
+               /* XXX: actually, these updates are not atomic, but we don't care */
+               server_stat->fuzzy_hashes_checked[epoch] ++;
+
+               if (rep.prob > 0.5) {
+                       server_stat->fuzzy_hashes_found[epoch] ++;
+               }
        }
        else {
                rep.flag = session->cmd->flag;
@@ -201,17 +208,21 @@ rspamd_fuzzy_process_command (struct fuzzy_session *session)
 }
 
 
-static gboolean
+static enum rspamd_fuzzy_epoch
 rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r)
 {
+       enum rspamd_fuzzy_epoch ret = RSPAMD_FUZZY_EPOCH_MAX;
+
        if (cmd->version == RSPAMD_FUZZY_VERSION) {
                if (cmd->shingles_count > 0) {
                        if (r == sizeof (struct rspamd_fuzzy_shingle_cmd)) {
-                               return TRUE;
+                               ret = RSPAMD_FUZZY_EPOCH9;
                        }
                }
                else {
-                       return (r == sizeof (*cmd));
+                       if (r == sizeof (*cmd)) {
+                               ret = RSPAMD_FUZZY_EPOCH9;
+                       }
                }
        }
        else if (cmd->version == 2) {
@@ -221,15 +232,15 @@ rspamd_fuzzy_command_valid (struct rspamd_fuzzy_cmd *cmd, gint r)
                 */
                if (cmd->shingles_count > 0) {
                        if (r == sizeof (struct rspamd_fuzzy_shingle_cmd)) {
-                               return TRUE;
+                               ret = RSPAMD_FUZZY_EPOCH8;
                        }
                }
                else {
-                       return (r == sizeof (*cmd));
+                       ret = RSPAMD_FUZZY_EPOCH8;
                }
        }
 
-       return FALSE;
+       return ret;
 }
 /*
  * Accept new connection and construct task
@@ -243,6 +254,7 @@ accept_fuzzy_socket (gint fd, short what, void *arg)
        guint8 buf[2048];
        struct rspamd_fuzzy_cmd *cmd = NULL, lcmd;
        struct legacy_fuzzy_cmd *l;
+       enum rspamd_fuzzy_epoch epoch = RSPAMD_FUZZY_EPOCH_MAX;
 
        session.worker = worker;
        session.fd = fd;
@@ -273,14 +285,17 @@ accept_fuzzy_socket (gint fd, short what, void *arg)
                        lcmd.value = l->value;
                        lcmd.tag = 0;
                        cmd = &lcmd;
+                       epoch = RSPAMD_FUZZY_EPOCH6;
                }
                else if ((guint)r >= sizeof (struct rspamd_fuzzy_cmd)) {
                        /* Check shingles count sanity */
                        session.legacy = FALSE;
                        cmd = (struct rspamd_fuzzy_cmd *)buf;
-                       if (!rspamd_fuzzy_command_valid (cmd, r)) {
+                       epoch = rspamd_fuzzy_command_valid (cmd, r);
+                       if (epoch == RSPAMD_FUZZY_EPOCH_MAX) {
                                /* Bad input */
                                msg_debug ("invalid fuzzy command of size %d received", r);
+                               cmd = NULL;
                        }
                }
                else {
@@ -289,7 +304,7 @@ accept_fuzzy_socket (gint fd, short what, void *arg)
                }
                if (cmd != NULL) {
                        session.cmd = cmd;
-                       rspamd_fuzzy_process_command (&session);
+                       rspamd_fuzzy_process_command (&session, epoch);
                }
 
                rspamd_inet_address_destroy (session.addr);
index d33e4ed1a74f6ff60768fbcb0633321de7b5756d..29096b86fdd6b6dbd1998ee1a8e65039e97f3371 100644 (file)
@@ -83,6 +83,16 @@ struct mime_part;
 struct rspamd_dns_resolver;
 struct rspamd_task;
 
+/**
+ * The epoch of the fuzzy client
+ */
+enum rspamd_fuzzy_epoch {
+       RSPAMD_FUZZY_EPOCH6 = 0, /**< pre 0.6.x */
+       RSPAMD_FUZZY_EPOCH8,     /**< 0.8 till 0.9 */
+       RSPAMD_FUZZY_EPOCH9,     /**< 0.9 + */
+       RSPAMD_FUZZY_EPOCH_MAX
+};
+
 /**
  * Server statistics
  */
@@ -94,6 +104,8 @@ struct rspamd_stat {
        guint messages_learned;                             /**< messages learned                                                               */
        guint fuzzy_hashes;                                 /**< number of fuzzy hashes stored                                  */
        guint fuzzy_hashes_expired;                         /**< number of fuzzy hashes expired                                 */
+       guint64 fuzzy_hashes_checked[RSPAMD_FUZZY_EPOCH_MAX]; /**< ammount of check requests for each epoch             */
+       guint64 fuzzy_hashes_found[RSPAMD_FUZZY_EPOCH_MAX]; /**< amount of hashes found by epoch                                */
 };
 
 /**