]> source.dussan.org Git - rspamd.git/commitdiff
Fix upstreams usage
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 20 Nov 2015 16:17:24 +0000 (16:17 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 20 Nov 2015 16:17:24 +0000 (16:17 +0000)
src/controller.c
src/http_proxy.c
src/libserver/cfg_file.h
src/libstat/backends/redis.c
src/lua/lua_upstream.c
src/plugins/fuzzy_check.c
src/plugins/surbl.c
src/smtp_proxy.c
src/worker.c

index be60050fc4b203c2927cef7ee7dd7fa9f8d737e1..31274f5d69b299f35969f56805b50c9459a1b706 100644 (file)
@@ -2550,8 +2550,9 @@ start_controller_worker (struct rspamd_worker *worker)
                        ctx->ev_base,
                        worker->srv->cfg);
 
-       rspamd_upstreams_library_init (ctx->resolver->r, ctx->ev_base);
-       rspamd_upstreams_library_config (worker->srv->cfg);
+       worker->srv->cfg->ups_ctx = rspamd_upstreams_library_init (ctx->resolver->r,
+                       ctx->ev_base);
+       rspamd_upstreams_library_config (worker->srv->cfg, worker->srv->cfg->ups_ctx);
        /* Maps events */
        rspamd_map_watch (worker->srv->cfg, ctx->ev_base);
        rspamd_symbols_cache_start_refresh (worker->srv->cfg->cache, ctx->ev_base);
index 85ee1c4bd87f7cd1cb7aac7dc8d91d8020d3cd06..a5290de635cf1629c39e786bee1ab57f464c03bd 100644 (file)
@@ -63,6 +63,7 @@ struct rspamd_http_upstream {
 struct http_proxy_ctx {
        gdouble timeout;
        struct timeval io_tv;
+       struct rspamd_config *cfg;
        /* DNS resolver */
        struct rspamd_dns_resolver *resolver;
        /* Events base */
@@ -155,7 +156,7 @@ http_proxy_parse_upstream (rspamd_mempool_t *pool,
                goto err;
        }
 
-       up->u = rspamd_upstreams_create ();
+       up->u = rspamd_upstreams_create (ctx->cfg->ups_ctx);
        if (!rspamd_upstreams_from_ucl (up->u, elt, 11333, NULL)) {
                g_set_error (err, http_proxy_quark (), 100,
                                "upstream has bad hosts definition");
@@ -200,6 +201,7 @@ init_http_proxy (struct rspamd_config *cfg)
        ctx->timeout = 5.0;
        ctx->upstreams = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
        ctx->rotate_tm = DEFAULT_ROTATION_TIME;
+       ctx->cfg = cfg;
 
        rspamd_rcl_register_worker_option (cfg, type, "timeout",
                rspamd_rcl_parse_struct_time, ctx,
@@ -457,8 +459,9 @@ start_http_proxy (struct rspamd_worker *worker)
                        worker->srv->cfg);
        double_to_tv (ctx->timeout, &ctx->io_tv);
 
-       rspamd_upstreams_library_init (ctx->resolver->r, ctx->ev_base);
-       rspamd_upstreams_library_config (worker->srv->cfg);
+       ctx->cfg->ups_ctx = rspamd_upstreams_library_init (ctx->resolver->r,
+                       ctx->ev_base);
+       rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx);
 
        /* XXX: stupid default */
        ctx->keys_cache = rspamd_keypair_cache_new (256);
index 96fc56d52ea3d4572dc3cd4c0cda06ccaba21c83..6263705ac94cc74499455d73bad858cf854c07f8 100644 (file)
@@ -309,6 +309,7 @@ struct rspamd_config {
        guint upstream_max_errors;                                              /**< upstream max errors before shutting off                    */
        gdouble upstream_error_time;                                    /**< rate of upstream errors                                                    */
        gdouble upstream_revive_time;                                   /**< revive timeout for upstreams                                               */
+       struct upstream_ctx *ups_ctx;                                   /**< upstream context                                                                   */
 
        guint min_word_len;                                                             /**< minimum length of the word to be considered                */
        guint max_word_len;                                                             /**< maximum length of the word to be considered                */
index 39d7dc21a19b456326cfa6b47714c93ef2be5400..66bad44719222507cc6f460c6bdc0c2fd1bdb3bc 100644 (file)
@@ -319,7 +319,7 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg)
                                        continue;
                                }
                                else {
-                                       backend->read_servers = rspamd_upstreams_create ();
+                                       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",
@@ -337,7 +337,7 @@ rspamd_redis_init (struct rspamd_stat_ctx *ctx, struct rspamd_config *cfg)
                                        continue;
                                }
                                else {
-                                       backend->write_servers = rspamd_upstreams_create ();
+                                       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",
index 0b273990314c8c0d140cbb4519dea791ae3093a9..8ceadcc8c99dabdeea17d241ee080b1e359c3bdb 100644 (file)
@@ -168,54 +168,39 @@ lua_check_upstream_list (lua_State * L)
 }
 
 /***
- * @function upstream_list.create(def)
+ * @function upstream_list.create(cfg, def, [default_port])
  * Create new upstream list from its string definition in form `<upstream>,<upstream>;<upstream>`
+ * @param {rspamd_config} cfg configuration reference
  * @param {string} def upstream list definition
+ * @param {number} default_port default port for upstreams
  * @return {upstream_list} upstream list structure
  */
 static gint
 lua_upstream_list_create (lua_State *L)
 {
        struct upstream_list *new = NULL, **pnew;
+       struct rspamd_config *cfg = lua_check_config (L, 1);
        const gchar *def;
-       char **tokens, **t;
        guint default_port = 0;
 
-       def = luaL_checkstring (L, 1);
+       def = luaL_checkstring (L, 2);
        if (def) {
-               if (lua_gettop (L) >= 2) {
-                       default_port = luaL_checknumber (L, 2);
+               if (lua_gettop (L) >= 3) {
+                       default_port = luaL_checknumber (L, 3);
                }
 
-               tokens = g_strsplit_set (def, ",;", 0);
-               if (!tokens || !tokens[0]) {
-                       goto err;
-               }
-               t = tokens;
+               new = rspamd_upstreams_create (cfg->ups_ctx);
 
-               new = rspamd_upstreams_create ();
-               while (*t != NULL) {
-                       if (!rspamd_upstreams_add_upstream (new, *t, default_port, NULL)) {
-                               goto err;
-                       }
-                       t ++;
+               if (rspamd_upstreams_parse_line (new, def, default_port, NULL)) {
+                       pnew = lua_newuserdata (L, sizeof (struct upstream_list *));
+                       rspamd_lua_setclass (L, "rspamd{upstream_list}", -1);
+                       *pnew = new;
+               }
+               else {
+                       rspamd_upstreams_destroy (new);
+                       lua_pushnil (L);
                }
-               pnew = lua_newuserdata (L, sizeof (struct upstream_list *));
-               rspamd_lua_setclass (L, "rspamd{upstream_list}", -1);
-
-               g_strfreev (tokens);
-               *pnew = new;
-       }
-
-       return 1;
-err:
-       if (tokens) {
-               g_strfreev (tokens);
-       }
-       if (new) {
-               rspamd_upstreams_destroy (new);
        }
-       lua_pushnil (L);
 
        return 1;
 }
index f8379d477b23adbf53f038b13d3be8a98e1be3f1..c6db5e016c519ed4a2a6e50d294a1d33d656fd07 100644 (file)
@@ -365,7 +365,7 @@ fuzzy_parse_rule (struct rspamd_config *cfg, const ucl_object_t *obj, gint cb_id
        }
 
        if ((value = ucl_object_find_key (obj, "servers")) != NULL) {
-               rule->servers = rspamd_upstreams_create ();
+               rule->servers = rspamd_upstreams_create (cfg->ups_ctx);
                rspamd_mempool_add_destructor (fuzzy_module_ctx->fuzzy_pool,
                                (rspamd_mempool_destruct_t)rspamd_upstreams_destroy,
                                rule->servers);
index 40ab9f6681667abae5933ff6431b4e0995a60037..c53fc00bd066aff2de6af233b79e5cdfdca79703 100644 (file)
@@ -350,7 +350,7 @@ surbl_module_config (struct rspamd_config *cfg)
 
        if ((value =
                rspamd_config_get_module_opt (cfg, "surbl", "redirector")) != NULL) {
-               surbl_module_ctx->redirectors = rspamd_upstreams_create ();
+               surbl_module_ctx->redirectors = rspamd_upstreams_create (cfg->ups_ctx);
                rspamd_mempool_add_destructor (surbl_module_ctx->surbl_pool,
                                (rspamd_mempool_destruct_t)rspamd_upstreams_destroy,
                                surbl_module_ctx->redirectors);
index 0ae9c70c48752e4e28666fed7ffb299eb863f27c..390ca292b5eb25b59881ca2e5a5fe95a19bc796a 100644 (file)
@@ -1073,8 +1073,9 @@ start_smtp_proxy (struct rspamd_worker *worker)
                        ctx->ev_base,
                        worker->srv->cfg);
 
-       rspamd_upstreams_library_init (ctx->resolver->r, ctx->ev_base);
-       rspamd_upstreams_library_config (worker->srv->cfg);
+       worker->srv->cfg->ups_ctx = rspamd_upstreams_library_init (ctx->resolver->r,
+                       ctx->ev_base);
+       rspamd_upstreams_library_config (worker->srv->cfg, worker->srv->cfg->ups_ctx);
        /* Set umask */
        umask (S_IWGRP | S_IWOTH | S_IROTH | S_IRGRP);
 
index 933e8305354ae958eeaf6c1e282828ae95b3b153..cd63a4e003294776ee7cf8f045b1e28e646676ed 100644 (file)
@@ -352,8 +352,9 @@ start_worker (struct rspamd_worker *worker)
                        ctx->ev_base,
                        worker->srv->cfg);
 
-       rspamd_upstreams_library_init (ctx->resolver->r, ctx->ev_base);
-       rspamd_upstreams_library_config (worker->srv->cfg);
+       ctx->cfg->ups_ctx = rspamd_upstreams_library_init (ctx->resolver->r,
+                       ctx->ev_base);
+       rspamd_upstreams_library_config (worker->srv->cfg, ctx->cfg->ups_ctx);
 
        /* XXX: stupid default */
        ctx->keys_cache = rspamd_keypair_cache_new (256);