From e163c950a7e7a09684112e7132390a0f9a04b3d8 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 5 Jan 2017 14:08:25 +0000 Subject: [PATCH] [Fix] Fix probability calculations for fuzzy redis backend --- src/libserver/fuzzy_backend_redis.c | 86 +++++++++++++++-------------- 1 file changed, 46 insertions(+), 40 deletions(-) diff --git a/src/libserver/fuzzy_backend_redis.c b/src/libserver/fuzzy_backend_redis.c index 3804e9fd0..e62dff3f3 100644 --- a/src/libserver/fuzzy_backend_redis.c +++ b/src/libserver/fuzzy_backend_redis.c @@ -360,9 +360,6 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, } } - session->prob = ((float)found) / RSPAMD_SHINGLE_SIZE; - rep.prob = session->prob; - if (found > RSPAMD_SHINGLE_SIZE / 2) { /* Now sort to find the most frequent element */ qsort (shingles, RSPAMD_SHINGLE_SIZE, @@ -390,47 +387,56 @@ rspamd_fuzzy_redis_shingles_callback (redisAsyncContext *c, gpointer r, } } - g_assert (sel != NULL); - - /* Prepare new check command */ - rspamd_fuzzy_redis_session_free_args (session); - session->nargs = 4; - session->argv = g_malloc (sizeof (gchar *) * session->nargs); - session->argv_lens = g_malloc (sizeof (gsize) * session->nargs); + if (max_found > RSPAMD_SHINGLE_SIZE / 2) { + session->prob = ((float)max_found) / RSPAMD_SHINGLE_SIZE; + rep.prob = session->prob; + + g_assert (sel != NULL); + + /* Prepare new check command */ + rspamd_fuzzy_redis_session_free_args (session); + session->nargs = 4; + session->argv = g_malloc (sizeof (gchar *) * session->nargs); + session->argv_lens = g_malloc (sizeof (gsize) * session->nargs); + + key = g_string_new (session->backend->redis_object); + g_string_append_len (key, sel->digest, sizeof (sel->digest)); + session->argv[0] = g_strdup ("HMGET"); + session->argv_lens[0] = 5; + session->argv[1] = key->str; + session->argv_lens[1] = key->len; + session->argv[2] = g_strdup ("V"); + session->argv_lens[2] = 1; + session->argv[3] = g_strdup ("F"); + session->argv_lens[3] = 1; + g_string_free (key, FALSE); /* Do not free underlying array */ + + g_assert (session->ctx != NULL); + if (redisAsyncCommandArgv (session->ctx, + rspamd_fuzzy_redis_check_callback, + session, session->nargs, + (const gchar **)session->argv, + session->argv_lens) != REDIS_OK) { + + if (session->callback.cb_check) { + memset (&rep, 0, sizeof (rep)); + session->callback.cb_check (&rep, session->cbdata); + } - key = g_string_new (session->backend->redis_object); - g_string_append_len (key, sel->digest, sizeof (sel->digest)); - session->argv[0] = g_strdup ("HMGET"); - session->argv_lens[0] = 5; - session->argv[1] = key->str; - session->argv_lens[1] = key->len; - session->argv[2] = g_strdup ("V"); - session->argv_lens[2] = 1; - session->argv[3] = g_strdup ("F"); - session->argv_lens[3] = 1; - g_string_free (key, FALSE); /* Do not free underlying array */ - - g_assert (session->ctx != NULL); - if (redisAsyncCommandArgv (session->ctx, rspamd_fuzzy_redis_check_callback, - session, session->nargs, - (const gchar **)session->argv, session->argv_lens) != REDIS_OK) { - if (session->callback.cb_check) { - memset (&rep, 0, sizeof (rep)); - session->callback.cb_check (&rep, session->cbdata); + rspamd_fuzzy_redis_session_dtor (session); + } + else { + /* Add timeout */ + event_set (&session->timeout, -1, EV_TIMEOUT, + rspamd_fuzzy_redis_timeout, + session); + event_base_set (session->ev_base, &session->timeout); + double_to_tv (session->backend->timeout, &tv); + event_add (&session->timeout, &tv); } - rspamd_fuzzy_redis_session_dtor (session); - } - else { - /* Add timeout */ - event_set (&session->timeout, -1, EV_TIMEOUT, rspamd_fuzzy_redis_timeout, - session); - event_base_set (session->ev_base, &session->timeout); - double_to_tv (session->backend->timeout, &tv); - event_add (&session->timeout, &tv); + return; } - - return; } } -- 2.39.5