Browse Source

[Feature] Allow to set peak callbacks from Lua

tags/1.5.0
Vsevolod Stakhov 7 years ago
parent
commit
896cef3359
2 changed files with 42 additions and 0 deletions
  1. 5
    0
      src/libserver/symbols_cache.c
  2. 37
    0
      src/lua/lua_config.c

+ 5
- 0
src/libserver/symbols_cache.c View 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);
}
}

+ 37
- 0
src/lua/lua_config.c View 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)
{

Loading…
Cancel
Save