]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Allow to set peak callbacks from Lua
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 25 Jan 2017 15:30:48 +0000 (15:30 +0000)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Wed, 25 Jan 2017 15:31:11 +0000 (15:31 +0000)
src/libserver/symbols_cache.c
src/lua/lua_config.c

index 020f35c805f659edae86eba2926c0b5a3894d932..5c53a0e08f42b540e7d7d6e11cfa4628f143ac1c 100644 (file)
@@ -920,6 +920,11 @@ rspamd_symbols_cache_destroy (struct symbols_cache *cache)
                g_ptr_array_free (cache->postfilters, TRUE);
                g_ptr_array_free (cache->composites, TRUE);
                REF_RELEASE (cache->items_by_order);
+
+               if (cache->peak_cb != -1) {
+                       luaL_unref (cache->cfg->lua_state, LUA_REGISTRYINDEX, cache->peak_cb);
+               }
+
                g_slice_free1 (sizeof (*cache), cache);
        }
 }
index 4f826901b0ea6e88676e8e784baeb43196c1c00d..06157f44eee118de8c0a5036b9d704ed543cb9d8 100644 (file)
@@ -572,6 +572,26 @@ LUA_FUNCTION_DEF (config, register_monitored);
  */
 LUA_FUNCTION_DEF (config, add_doc);
 
+/***
+ * @method rspamd_config:set_peak_cb(function)
+ * Sets a function that will be called when frequency of some symbol goes out of
+ * stddev * 2 over the last period of refreshment.
+ *
+ * @example
+rspamd_config:set_peak_cb(function(ev_base, sym, mean, stddev, value, error)
+  -- ev_base: event base for async events (e.g. redis)
+  -- sym: symbol's name
+  -- mean: mean frequency value
+  -- stddev: standard deviation of frequency
+  -- value: current frequency value
+  -- error: squared error
+  local logger = require "rspamd_logger"
+  logger.infox(rspamd_config, "symbol %s has changed frequency significantly: %s(%s) over %s(%s)",
+      sym, value, error, mean, stddev)
+end)
+ */
+LUA_FUNCTION_DEF (config, set_peak_cb);
+
 static const struct luaL_reg configlib_m[] = {
        LUA_INTERFACE_DEF (config, get_module_opt),
        LUA_INTERFACE_DEF (config, get_mempool),
@@ -613,6 +633,7 @@ static const struct luaL_reg configlib_m[] = {
        LUA_INTERFACE_DEF (config, register_finish_script),
        LUA_INTERFACE_DEF (config, register_monitored),
        LUA_INTERFACE_DEF (config, add_doc),
+       LUA_INTERFACE_DEF (config, set_peak_cb),
        {"__tostring", rspamd_lua_class_tostring},
        {"__newindex", lua_config_newindex},
        {NULL, NULL}
@@ -1887,6 +1908,22 @@ lua_config_add_condition (lua_State *L)
        return 1;
 }
 
+static gint
+lua_config_set_peak_cb (lua_State *L)
+{
+       struct rspamd_config *cfg = lua_check_config (L, 1);
+       gint condref;
+
+       if (cfg && lua_type (L, 2) == LUA_TFUNCTION) {
+               lua_pushvalue (L, 2);
+               condref = luaL_ref (L, LUA_REGISTRYINDEX);
+               rspamd_symbols_cache_set_peak_callback (cfg->cache,
+                               condref);
+       }
+
+       return 0;
+}
+
 static gint
 lua_config_enable_symbol (lua_State *L)
 {