]> source.dussan.org Git - rspamd.git/commitdiff
Restore redis backend compilation.
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 7 Jan 2016 16:12:22 +0000 (16:12 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Thu, 7 Jan 2016 16:12:22 +0000 (16:12 +0000)
src/libstat/CMakeLists.txt
src/libstat/backends/backends.h
src/libstat/backends/redis.c

index 80abf9d6f9e76823f413b483e218aa8ba2bd3fa7..b6dfeb3c8f953a739089f8b3925ce0d6b5195a9f 100644 (file)
@@ -10,8 +10,8 @@ SET(CLASSIFIERSSRC    ${CMAKE_CURRENT_SOURCE_DIR}/classifiers/bayes.c)
 SET(BACKENDSSRC        ${CMAKE_CURRENT_SOURCE_DIR}/backends/mmaped_file.c
                                        ${CMAKE_CURRENT_SOURCE_DIR}/backends/sqlite3_backend.c)
 IF(ENABLE_HIREDIS MATCHES "ON")
-       #SET(BACKENDSSRC        ${BACKENDSSRC}
-       #                               ${CMAKE_CURRENT_SOURCE_DIR}/backends/redis.c)
+       SET(BACKENDSSRC         ${BACKENDSSRC}
+                                       ${CMAKE_CURRENT_SOURCE_DIR}/backends/redis.c)
 ENDIF(ENABLE_HIREDIS MATCHES "ON")
 SET(CACHESSRC  ${CMAKE_CURRENT_SOURCE_DIR}/learn_cache/sqlite3_cache.c)
 
index 44c2f13edd3a104f57b55f0177ba74a2c6d49552..148ab4fe8bc3f4277d009d170ee30d2988e6b3b1 100644 (file)
@@ -106,5 +106,6 @@ struct rspamd_stat_backend {
 
 RSPAMD_STAT_BACKEND_DEF(mmaped_file);
 RSPAMD_STAT_BACKEND_DEF(sqlite3);
+RSPAMD_STAT_BACKEND_DEF(redis);
 
 #endif /* BACKENDS_H_ */
index 5f9b5d126fb25cd7af96e2952262a011805028b2..d7a34d960c96b9035a71b375f7ed95e412947a15 100644 (file)
@@ -38,7 +38,7 @@
 #define REDIS_DEFAULT_OBJECT "%s%l"
 #define REDIS_DEFAULT_TIMEOUT 0.5
 
-struct redis_stat_ctx_elt {
+struct redis_stat_ctx {
        struct upstream_list *read_servers;
        struct upstream_list *write_servers;
 
@@ -46,11 +46,8 @@ struct redis_stat_ctx_elt {
        gdouble timeout;
 };
 
-struct redis_stat_ctx {
-       GHashTable *redis_elts;
-};
-
 struct redis_stat_runtime {
+       struct redis_stat_ctx *ctx;
        struct rspamd_task *task;
        struct upstream *selected;
        struct event timeout_event;
@@ -333,119 +330,83 @@ rspamd_redis_connected (redisAsyncContext *c, gpointer r, gpointer priv)
 }
 
 gpointer
-rspamd_redis_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg)
+rspamd_redis_init (struct rspamd_stat_ctx *ctx,
+               struct rspamd_config *cfg, struct rspamd_statfile *st)
 {
-       struct redis_stat_ctx *new;
-       struct redis_stat_ctx_elt *backend;
-       struct rspamd_classifier_config *clf;
-       struct rspamd_statfile_config *stf;
-       GList *cur, *curst;
+       struct redis_stat_ctx *backend;
+       struct rspamd_statfile_config *stf = st->stcf;
        const ucl_object_t *elt;
 
-       new = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*new));
-       new->redis_elts = g_hash_table_new (g_direct_hash, g_direct_equal);
-
-       /* Iterate over all classifiers and load matching statfiles */
-       cur = cfg->classifiers;
-
-       while (cur) {
-               clf = cur->data;
-
-               if (clf->backend != NULL && strcmp (clf->backend, REDIS_BACKEND_TYPE)) {
-
-                       curst = clf->statfiles;
-                       while (curst) {
-                               stf = curst->data;
-
-                               /*
-                                * By default, all statfiles are treated as mmaped files
-                                */
-
-                               /*
-                                * Check configuration sanity
-                                */
-                               backend = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*backend));
-
-                               elt = ucl_object_find_key (stf->opts, "read_servers");
-                               if (elt == NULL) {
-                                       elt = ucl_object_find_key (stf->opts, "servers");
-                               }
-                               if (elt == NULL) {
-                                       msg_err ("statfile %s has no redis servers", stf->symbol);
-                                       curst = curst->next;
-                                       continue;
-                               }
-                               else {
-                                       backend->read_servers = rspamd_upstreams_create (cfg->ups_ctx);
-                                       if (!rspamd_upstreams_from_ucl (backend->read_servers, elt,
-                                                       REDIS_DEFAULT_PORT, NULL)) {
-                                               msg_err ("statfile %s cannot read servers configuration",
-                                                               stf->symbol);
-                                               curst = curst->next;
-                                               continue;
-                                       }
-                               }
-
-                               elt = ucl_object_find_key (stf->opts, "write_servers");
-                               if (elt == NULL) {
-                                       msg_err ("statfile %s has no write redis servers, "
-                                                       "so learning is impossible", stf->symbol);
-                                       curst = curst->next;
-                               }
-                               else {
-                                       backend->write_servers = rspamd_upstreams_create (cfg->ups_ctx);
-                                       if (!rspamd_upstreams_from_ucl (backend->write_servers, elt,
-                                                       REDIS_DEFAULT_PORT, NULL)) {
-                                               msg_err ("statfile %s cannot write servers configuration",
-                                                               stf->symbol);
-                                               rspamd_upstreams_destroy (backend->write_servers);
-                                               backend->write_servers = NULL;
-                                       }
-                               }
-
-                               elt = ucl_object_find_key (stf->opts, "prefix");
-                               if (elt == NULL || ucl_object_type (elt) != UCL_STRING) {
-                                       backend->redis_object = REDIS_DEFAULT_OBJECT;
-                               }
-                               else {
-                                       /* XXX: sanity check */
-                                       backend->redis_object = ucl_object_tostring (elt);
-                                       if (rspamd_redis_expand_object (backend->redis_object, stf,
-                                                       NULL, NULL) == 0) {
-                                               msg_err ("statfile %s cannot write servers configuration",
-                                                               stf->symbol);
-                                       }
-                               }
+       backend = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*backend));
 
-                               elt = ucl_object_find_key (stf->opts, "timeout");
-                               if (elt) {
-                                       backend->timeout = ucl_object_todouble (elt);
-                               }
-                               else {
-                                       backend->timeout = REDIS_DEFAULT_TIMEOUT;
-                               }
+       elt = ucl_object_find_key (stf->opts, "read_servers");
+       if (elt == NULL) {
+               elt = ucl_object_find_key (stf->opts, "servers");
+       }
+       if (elt == NULL) {
+               msg_err ("statfile %s has no redis servers", stf->symbol);
 
-                               g_hash_table_insert (new->redis_elts, stf, backend);
+               return NULL;
+       }
+       else {
+               backend->read_servers = rspamd_upstreams_create (cfg->ups_ctx);
+               if (!rspamd_upstreams_from_ucl (backend->read_servers, elt,
+                               REDIS_DEFAULT_PORT, NULL)) {
+                       msg_err ("statfile %s cannot read servers configuration",
+                                       stf->symbol);
+                       return NULL;
+               }
+       }
 
-                               ctx->statfiles ++;
+       elt = ucl_object_find_key (stf->opts, "write_servers");
+       if (elt == NULL) {
+               msg_err ("statfile %s has no write redis servers, "
+                               "so learning is impossible", stf->symbol);
+               backend->write_servers = NULL;
+       }
+       else {
+               backend->write_servers = rspamd_upstreams_create (cfg->ups_ctx);
+               if (!rspamd_upstreams_from_ucl (backend->write_servers, elt,
+                               REDIS_DEFAULT_PORT, NULL)) {
+                       msg_err ("statfile %s cannot write servers configuration",
+                                       stf->symbol);
+                       rspamd_upstreams_destroy (backend->write_servers);
+                       backend->write_servers = NULL;
+               }
+       }
 
-                               curst = curst->next;
-                       }
+       elt = ucl_object_find_key (stf->opts, "prefix");
+       if (elt == NULL || ucl_object_type (elt) != UCL_STRING) {
+               backend->redis_object = REDIS_DEFAULT_OBJECT;
+       }
+       else {
+               /* XXX: sanity check */
+               backend->redis_object = ucl_object_tostring (elt);
+               if (rspamd_redis_expand_object (backend->redis_object, stf,
+                               NULL, NULL) == 0) {
+                       msg_err ("statfile %s cannot write servers configuration",
+                                       stf->symbol);
                }
+       }
 
-               cur = g_list_next (cur);
+       elt = ucl_object_find_key (stf->opts, "timeout");
+       if (elt) {
+               backend->timeout = ucl_object_todouble (elt);
        }
+       else {
+               backend->timeout = REDIS_DEFAULT_TIMEOUT;
+       }
+
 
-       return (gpointer)new;
+       return (gpointer)backend;
 }
 
 gpointer
 rspamd_redis_runtime (struct rspamd_task *task,
-               struct rspamd_statfile_config *stcf,
+               struct rspamd_statfile_config *stcf, \
                gboolean learn, gpointer c)
 {
        struct redis_stat_ctx *ctx = REDIS_CTX (c);
-       struct redis_stat_ctx_elt *elt;
        struct redis_stat_runtime *rt;
        struct upstream *up;
        rspamd_inet_addr_t *addr;
@@ -454,22 +415,19 @@ rspamd_redis_runtime (struct rspamd_task *task,
        g_assert (ctx != NULL);
        g_assert (stcf != NULL);
 
-       elt = g_hash_table_lookup (ctx->redis_elts, stcf);
-       g_assert (elt != NULL);
-
-       if (learn && elt->write_servers == NULL) {
+       if (learn && ctx->write_servers == NULL) {
                msg_err ("no write servers defined for %s, cannot learn", stcf->symbol);
                return NULL;
        }
 
        if (learn) {
-               up = rspamd_upstream_get (elt->write_servers,
+               up = rspamd_upstream_get (ctx->write_servers,
                                RSPAMD_UPSTREAM_MASTER_SLAVE,
                                NULL,
                                0);
        }
        else {
-               up = rspamd_upstream_get (elt->read_servers,
+               up = rspamd_upstream_get (ctx->read_servers,
                                RSPAMD_UPSTREAM_ROUND_ROBIN,
                                NULL,
                                0);
@@ -481,7 +439,7 @@ rspamd_redis_runtime (struct rspamd_task *task,
        }
 
        rt = rspamd_mempool_alloc0 (task->task_pool, sizeof (*rt));
-       rspamd_redis_expand_object (elt->redis_object, stcf, task,
+       rspamd_redis_expand_object (ctx->redis_object, stcf, task,
                        &rt->redis_object_expanded);
        rt->selected = up;
        rt->task = task;
@@ -499,7 +457,7 @@ rspamd_redis_runtime (struct rspamd_task *task,
        /* Now check stats */
        event_set (&rt->timeout_event, -1, EV_TIMEOUT, rspamd_redis_timeout, rt);
        event_base_set (task->ev_base, &rt->timeout_event);
-       double_to_tv (elt->timeout, &tv);
+       double_to_tv (ctx->timeout, &tv);
        event_add (&rt->timeout_event, &tv);
        redisAsyncCommand (rt->redis, rspamd_redis_connected, rt, "HGET %s %s",
                        rt->redis_object_expanded, "learned");