Browse Source

[Minor] Disable internal history if handled by plugins

tags/1.5.3
Vsevolod Stakhov 7 years ago
parent
commit
fac6eb71e6
3 changed files with 46 additions and 9 deletions
  1. 42
    7
      src/libserver/roll_history.c
  2. 3
    1
      src/libserver/roll_history.h
  3. 1
    1
      src/rspamd.c

+ 42
- 7
src/libserver/roll_history.c View File

@@ -17,6 +17,7 @@
#include "rspamd.h"
#include "roll_history.h"
#include "ucl.h"
#include "lua/lua_common.h"
#include "unix-std.h"

static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'};
@@ -27,20 +28,43 @@ static const gchar rspamd_history_magic_old[] = {'r', 's', 'h', '1'};
* @return new structure
*/
struct roll_history *
rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows)
rspamd_roll_history_new (rspamd_mempool_t *pool, guint max_rows,
struct rspamd_config *cfg)
{
struct roll_history *new;
struct roll_history *history;
lua_State *L = cfg->lua_state;

if (pool == NULL || max_rows == 0) {
return NULL;
}

new = rspamd_mempool_alloc0_shared (pool, sizeof (struct roll_history));
new->rows = rspamd_mempool_alloc0_shared (pool,
sizeof (struct roll_history_row) * max_rows);
new->nrows = max_rows;
history = rspamd_mempool_alloc0_shared (pool, sizeof (struct roll_history));

return new;
/*
* Here, we check if there is any plugin that handles history,
* in this case, we disable this code completely
*/
lua_getglobal (L, "plugins");
if (lua_istable (L, -1)) {
lua_pushstring (L, "history");
lua_gettable (L, -2);

if (lua_istable (L, -1)) {
history->disabled = TRUE;
}

lua_pop (L, 1);
}

lua_pop (L, 1);

if (!history->disabled) {
history->rows = rspamd_mempool_alloc0_shared (pool,
sizeof (struct roll_history_row) * max_rows);
history->nrows = max_rows;
}

return history;
}

struct history_metric_callback_data {
@@ -76,6 +100,10 @@ rspamd_roll_history_update (struct roll_history *history,
struct rspamd_metric_result *metric_res;
struct history_metric_callback_data cbdata;

if (history->disabled) {
return;
}

/* First of all obtain check and obtain row number */
g_atomic_int_compare_and_exchange (&history->cur_row, history->nrows, 0);
#if ((GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION > 30))
@@ -163,6 +191,9 @@ rspamd_roll_history_load (struct roll_history *history, const gchar *filename)
guint n, i;

g_assert (history != NULL);
if (history->disabled) {
return TRUE;
}

if (stat (filename, &st) == -1) {
msg_info ("cannot load history from %s: %s", filename,
@@ -330,6 +361,10 @@ rspamd_roll_history_save (struct roll_history *history, const gchar *filename)

g_assert (history != NULL);

if (history->disabled) {
return TRUE;
}

if ((fd = open (filename, O_WRONLY | O_CREAT | O_TRUNC, 00600)) == -1) {
msg_info ("cannot save history to %s: %s", filename, strerror (errno));
return FALSE;

+ 3
- 1
src/libserver/roll_history.h View File

@@ -30,6 +30,7 @@
#define HISTORY_MAX_ADDR 32

struct rspamd_task;
struct rspamd_config;

struct roll_history_row {
struct timeval tv;
@@ -47,6 +48,7 @@ struct roll_history_row {

struct roll_history {
struct roll_history_row *rows;
gboolean disabled;
guint nrows;
guint cur_row;
};
@@ -57,7 +59,7 @@ struct roll_history {
* @return new structure
*/
struct roll_history * rspamd_roll_history_new (rspamd_mempool_t *pool,
guint max_rows);
guint max_rows, struct rspamd_config *cfg);

/**
* Update roll history with data from task

+ 1
- 1
src/rspamd.c View File

@@ -1259,7 +1259,7 @@ main (gint argc, gchar **argv, gchar **env)

/* Create rolling history */
rspamd_main->history = rspamd_roll_history_new (rspamd_main->server_pool,
rspamd_main->cfg->history_rows);
rspamd_main->cfg->history_rows, rspamd_main->cfg);

gperf_profiler_init (rspamd_main->cfg, "main");


Loading…
Cancel
Save