]> source.dussan.org Git - rspamd.git/commitdiff
[Project] Update Redis backend to support refreshing
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jul 2018 11:58:43 +0000 (12:58 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 5 Jul 2018 11:58:43 +0000 (12:58 +0100)
src/libserver/fuzzy_backend_redis.c

index d25f057edc47df964ebc5254db391865133161c0..550e76f6fa61cb66744052055d99d743df2ac855 100644 (file)
@@ -1157,6 +1157,40 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk,
                        return FALSE;
                }
        }
+       else if (cmd->cmd == FUZZY_REFRESH) {
+               /*
+                * Issue refresh command by just EXPIRE command
+                * EXPIRE <key> <expire>
+                * Where <key> is <prefix> || <digest>
+                */
+
+               klen = strlen (session->backend->redis_object) +
+                          sizeof (cmd->digest) + 1;
+
+               /* EXPIRE */
+               key = g_string_sized_new (klen);
+               g_string_append (key, session->backend->redis_object);
+               g_string_append_len (key, cmd->digest, sizeof (cmd->digest));
+               value = g_string_sized_new (30);
+               rspamd_printf_gstring (value, "%d",
+                               (gint)rspamd_fuzzy_backend_get_expire (bk));
+               session->argv[cur_shift] = g_strdup ("EXPIRE");
+               session->argv_lens[cur_shift++] = sizeof ("EXPIRE") - 1;
+               session->argv[cur_shift] = key->str;
+               session->argv_lens[cur_shift++] = key->len;
+               session->argv[cur_shift] = value->str;
+               session->argv_lens[cur_shift++] = value->len;
+               g_string_free (key, FALSE);
+               g_string_free (value, FALSE);
+
+               if (redisAsyncCommandArgv (session->ctx, NULL, NULL,
+                               3,
+                               (const gchar **)&session->argv[cur_shift - 3],
+                               &session->argv_lens[cur_shift - 3]) != REDIS_OK) {
+
+                       return FALSE;
+               }
+       }
        else {
                g_assert_not_reached ();
        }
@@ -1230,6 +1264,47 @@ rspamd_fuzzy_update_append_command (struct rspamd_fuzzy_backend *bk,
                                }
                        }
                }
+               else if (cmd->cmd == FUZZY_REFRESH) {
+                       klen = strlen (session->backend->redis_object) +
+                                  64 + 1;
+
+                       for (i = 0; i < RSPAMD_SHINGLE_SIZE; i ++) {
+                               guchar *hval;
+                               /*
+                                * For each command with shingles we additionally emit 32 commands:
+                                * EXPIRE <prefix>_<number>_<value> <expire>
+                                */
+
+                               /* Expire */
+                               key = g_string_sized_new (klen);
+                               rspamd_printf_gstring (key, "%s_%d_%uL",
+                                               session->backend->redis_object,
+                                               i,
+                                               io_cmd->cmd.shingle.sgl.hashes[i]);
+                               value = g_string_sized_new (30);
+                               rspamd_printf_gstring (value, "%d",
+                                               (gint)rspamd_fuzzy_backend_get_expire (bk));
+                               hval = g_malloc (sizeof (io_cmd->cmd.shingle.basic.digest));
+                               memcpy (hval, io_cmd->cmd.shingle.basic.digest,
+                                               sizeof (io_cmd->cmd.shingle.basic.digest));
+                               session->argv[cur_shift] = g_strdup ("EXPIRE");
+                               session->argv_lens[cur_shift++] = sizeof ("EXPIRE") - 1;
+                               session->argv[cur_shift] = key->str;
+                               session->argv_lens[cur_shift++] = key->len;
+                               session->argv[cur_shift] = value->str;
+                               session->argv_lens[cur_shift++] = value->len;
+                               g_string_free (key, FALSE);
+                               g_string_free (value, FALSE);
+
+                               if (redisAsyncCommandArgv (session->ctx, NULL, NULL,
+                                               3,
+                                               (const gchar **)&session->argv[cur_shift - 3],
+                                               &session->argv_lens[cur_shift - 3]) != REDIS_OK) {
+
+                                       return FALSE;
+                               }
+                       }
+               }
                else {
                        g_assert_not_reached ();
                }
@@ -1353,6 +1428,15 @@ rspamd_fuzzy_backend_update_redis (struct rspamd_fuzzy_backend *bk,
                                nargs += RSPAMD_SHINGLE_SIZE * 2;
                        }
                }
+               else if (cmd->cmd == FUZZY_REFRESH) {
+                       ncommands += 1;
+                       nargs += 3;
+
+                       if (io_cmd->is_shingle) {
+                               ncommands += RSPAMD_SHINGLE_SIZE;
+                               nargs += RSPAMD_SHINGLE_SIZE * 3;
+                       }
+               }
        }
 
        /* Now we need to create a new request */