From e80283ac81f3677ed70f084275f157781d0ebba1 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 12 Mar 2015 09:52:33 +0000 Subject: [PATCH] Save some space inside rspamd task by using bit flags. --- src/libmime/filter.c | 8 ++++---- src/libmime/message.c | 2 +- src/libmime/smtp_utils.c | 2 +- src/libserver/protocol.c | 43 +++++++++++++++++++++++----------------- src/libserver/task.c | 22 +++++++++++--------- src/libserver/task.h | 23 ++++++++++++--------- src/worker.c | 7 ++++++- 7 files changed, 64 insertions(+), 43 deletions(-) diff --git a/src/libmime/filter.c b/src/libmime/filter.c index 89bbfad10..7736ba4cf 100644 --- a/src/libmime/filter.c +++ b/src/libmime/filter.c @@ -367,7 +367,7 @@ rspamd_process_filters (struct rspamd_task *task) wl = ucl_object_find_key (task->settings, "whitelist"); if (wl != NULL) { msg_info ("<%s> is whitelisted", task->message_id); - task->is_skipped = TRUE; + task->flags |= RSPAMD_TASK_FLAG_SKIP; return 0; } } @@ -378,7 +378,7 @@ rspamd_process_filters (struct rspamd_task *task) cur = task->cfg->metrics_list; while (cur) { metric = cur->data; - if (!task->pass_all_filters && + if (!(task->flags & RSPAMD_TASK_FLAG_PASS_ALL) && metric->actions[METRIC_ACTION_REJECT].score > 0 && check_metric_is_spam (task, metric)) { msg_info ("<%s> has already scored more than %.2f, so do not " @@ -648,7 +648,7 @@ struct classifiers_cbdata { void rspamd_process_statistics (struct rspamd_task *task) { - if (task->is_skipped) { + if (RSPAMD_TASK_IS_SKIPPED (task)) { return; } @@ -665,7 +665,7 @@ rspamd_process_statistic_threaded (gpointer data, gpointer user_data) struct rspamd_task *task = (struct rspamd_task *)data; struct lua_locked_state *nL = user_data; - if (task->is_skipped) { + if (RSPAMD_TASK_IS_SKIPPED (task)) { remove_async_thread (task->s); return; } diff --git a/src/libmime/message.c b/src/libmime/message.c index 3786219ef..f59fe4671 100644 --- a/src/libmime/message.c +++ b/src/libmime/message.c @@ -1525,7 +1525,7 @@ process_message (struct rspamd_task *task) */ g_mime_stream_mem_set_owner (GMIME_STREAM_MEM (stream), FALSE); - if (task->is_mime) { + if (task->flags & RSPAMD_TASK_FLAG_MIME) { debug_task ("construct mime parser from string length %d", (gint)task->msg.len); diff --git a/src/libmime/smtp_utils.c b/src/libmime/smtp_utils.c index a5a2f26ef..5a8683d7b 100644 --- a/src/libmime/smtp_utils.c +++ b/src/libmime/smtp_utils.c @@ -179,7 +179,7 @@ smtp_metric_callback (gpointer key, gpointer value, gpointer ud) cd->res = metric_res; } - if (!task->is_skipped) { + if (!RSPAMD_TASK_IS_SKIPPED (task)) { cd->log_offset += rspamd_snprintf (cd->log_buf + cd->log_offset, cd->log_size - cd->log_offset, "(%s: %c (%s): [%.2f/%.2f/%.2f] [", diff --git a/src/libserver/protocol.c b/src/libserver/protocol.c index 3a35efba3..ac6cd4077 100644 --- a/src/libserver/protocol.c +++ b/src/libserver/protocol.c @@ -232,7 +232,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, struct rspamd_http_message *msg) { gchar *headern, *tmp; - gboolean res = TRUE, validh; + gboolean res = TRUE, validh, fl; struct rspamd_http_header *h; LL_FOREACH (msg->headers, h) @@ -284,7 +284,13 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, case 'j': case 'J': if (g_ascii_strcasecmp (headern, JSON_HEADER) == 0) { - task->is_json = rspamd_config_parse_flag (h->value->str); + fl = rspamd_config_parse_flag (h->value->str); + if (fl) { + task->flags |= RSPAMD_TASK_FLAG_JSON; + } + else { + task->flags &= ~RSPAMD_TASK_FLAG_JSON; + } } else { debug_task ("wrong header: %s", headern); @@ -336,7 +342,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, if (g_ascii_strcasecmp (headern, PASS_HEADER) == 0) { if (h->value->len == sizeof ("all") - 1 && g_ascii_strcasecmp (h->value->str, "all") == 0) { - task->pass_all_filters = TRUE; + task->flags |= RSPAMD_TASK_FLAG_PASS_ALL; debug_task ("pass all filters"); } } @@ -361,7 +367,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, if (g_ascii_strcasecmp (headern, URLS_HEADER) == 0) { if (h->value->len == sizeof ("extended") - 1 && g_ascii_strcasecmp (h->value->str, "extended") == 0) { - task->extended_urls = TRUE; + task->flags |= RSPAMD_TASK_FLAG_EXT_URLS; debug_task ("extended urls information"); } } @@ -373,7 +379,7 @@ rspamd_protocol_handle_headers (struct rspamd_task *task, case 'L': if (g_ascii_strcasecmp (headern, NO_LOG_HEADER) == 0) { if (g_ascii_strcasecmp (h->value->str, "no") == 0) { - task->no_log = TRUE; + task->flags |= RSPAMD_TASK_FLAG_NO_LOG; } } else { @@ -418,19 +424,20 @@ rspamd_protocol_handle_request (struct rspamd_task *task, if (msg->method == HTTP_SYMBOLS) { task->cmd = CMD_SYMBOLS; - task->is_json = FALSE; + task->flags &= ~RSPAMD_TASK_FLAG_JSON; } else if (msg->method == HTTP_CHECK) { task->cmd = CMD_CHECK; - task->is_json = FALSE; + task->flags &= ~RSPAMD_TASK_FLAG_JSON; } else { - task->is_json = TRUE; + task->flags |= RSPAMD_TASK_FLAG_JSON; ret = rspamd_protocol_handle_url (task, msg); } if (msg->flags & RSPAMD_HTTP_FLAG_SPAMC) { - task->is_spamc = TRUE; + task->flags &= ~RSPAMD_TASK_FLAG_JSON; + task->flags |= RSPAMD_TASK_FLAG_SPAMC; } return ret; @@ -478,7 +485,7 @@ urls_protocol_cb (gpointer key, gpointer value, gpointer ud) struct rspamd_url *url = value; ucl_object_t *obj, *elt; - if (!cb->task->extended_urls) { + if (!(cb->task->flags & RSPAMD_TASK_FLAG_EXT_URLS)) { obj = ucl_object_fromlstring (url->host, url->hostlen); } else { @@ -670,7 +677,7 @@ rspamd_metric_result_ucl (struct rspamd_task *task, action = mres->action; is_spam = (action == METRIC_ACTION_REJECT); - if (task->is_skipped) { + if (RSPAMD_TASK_IS_SKIPPED (task)) { action_char = 'S'; } else if (is_spam) { @@ -688,7 +695,7 @@ rspamd_metric_result_ucl (struct rspamd_task *task, obj = ucl_object_typed_new (UCL_OBJECT); ucl_object_insert_key (obj, ucl_object_frombool (is_spam), "is_spam", 0, false); - ucl_object_insert_key (obj, ucl_object_frombool (task->is_skipped), + ucl_object_insert_key (obj, ucl_object_frombool (RSPAMD_TASK_IS_SKIPPED (task)), "is_skipped", 0, false); ucl_object_insert_key (obj, ucl_object_fromdouble (mres->score), "score", 0, false); @@ -847,7 +854,7 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, rspamd_printf_gstring (logbuf, "user: %s, ", task->user); } - if (!task->no_log) { + if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) { rspamd_roll_history_update (task->worker->srv->history, task); } @@ -886,18 +893,18 @@ rspamd_protocol_http_reply (struct rspamd_http_message *msg, "message-id", 0, false); write_hashes_to_log (task, logbuf); - if (!task->no_log) { + if (!(task->flags & RSPAMD_TASK_FLAG_NO_LOG)) { msg_info ("%v", logbuf); } g_string_free (logbuf, TRUE); msg->body = g_string_sized_new (BUFSIZ); - if (msg->method < HTTP_SYMBOLS && !task->is_spamc) { + if (msg->method < HTTP_SYMBOLS && !RSPAMD_TASK_IS_SPAMC (task)) { rspamd_ucl_emit_gstring (top, UCL_EMIT_JSON_COMPACT, msg->body); } else { - if (task->is_spamc) { + if (RSPAMD_TASK_IS_SPAMC (task)) { rspamd_ucl_tospamc_output (task, top, msg->body); } else { @@ -932,11 +939,11 @@ rspamd_protocol_write_reply (struct rspamd_task *task) msg->peer_key = rspamd_http_connection_key_ref (task->peer_key); msg_info ("<%s> writing encrypted reply", task->message_id); } - if (!task->is_json) { + if (!RSPAMD_TASK_IS_JSON (task)) { /* Turn compatibility on */ msg->method = HTTP_SYMBOLS; } - if (task->is_spamc) { + if (RSPAMD_TASK_IS_SPAMC (task)) { msg->flags |= RSPAMD_HTTP_FLAG_SPAMC; } diff --git a/src/libserver/task.c b/src/libserver/task.c index b43438a27..e926897be 100644 --- a/src/libserver/task.c +++ b/src/libserver/task.c @@ -50,7 +50,9 @@ rspamd_task_new (struct rspamd_worker *worker) new_task->state = READ_MESSAGE; if (worker) { new_task->cfg = worker->srv->cfg; - new_task->pass_all_filters = new_task->cfg->check_all_filters; + if (new_task->cfg->check_all_filters) { + new_task->flags |= RSPAMD_TASK_FLAG_PASS_ALL; + } } #ifdef HAVE_CLOCK_GETTIME # ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID @@ -99,8 +101,7 @@ rspamd_task_new (struct rspamd_worker *worker) (rspamd_mempool_destruct_t) g_tree_destroy, new_task->urls); new_task->sock = -1; - new_task->is_mime = TRUE; - new_task->is_json = TRUE; + new_task->flags |= (RSPAMD_TASK_FLAG_MIME|RSPAMD_TASK_FLAG_JSON); new_task->pre_result.action = METRIC_ACTION_NOACTION; new_task->message_id = new_task->queue_id = "undef"; @@ -182,7 +183,7 @@ rspamd_task_fin (void *arg) return TRUE; } /* Add task to classify to classify pool */ - if (!task->is_skipped && task->classify_pool) { + if (!RSPAMD_TASK_IS_SKIPPED (task) && task->classify_pool) { register_async_thread (task->s); g_thread_pool_push (task->classify_pool, task, &err); if (err != NULL) { @@ -191,7 +192,7 @@ rspamd_task_fin (void *arg) g_error_free (err); } } - if (task->is_skipped) { + if (RSPAMD_TASK_IS_SKIPPED (task)) { rspamd_task_reply (task); } else { @@ -212,7 +213,8 @@ rspamd_task_restore (void *arg) struct rspamd_task *task = (struct rspamd_task *) arg; /* Call post filters */ - if (task->state == WAIT_POST_FILTER && !task->skip_extra_filters) { + if (task->state == WAIT_POST_FILTER && + !(task->flags & RSPAMD_TASK_FLAG_SKIP_EXTRA)) { rspamd_lua_call_post_filters (task); } task->s->wanna_die = TRUE; @@ -320,7 +322,9 @@ rspamd_task_process (struct rspamd_task *task, task->state = WRITE_REPLY; return FALSE; } - task->skip_extra_filters = !process_extra_filters; + if (!process_extra_filters) { + task->flags |= RSPAMD_TASK_FLAG_SKIP_EXTRA; + } if (!process_extra_filters || task->cfg->pre_filters == NULL) { r = rspamd_process_filters (task); if (r == -1) { @@ -330,7 +334,7 @@ rspamd_task_process (struct rspamd_task *task, return FALSE; } /* Add task to classify to classify pool */ - if (!task->is_skipped && classify_pool) { + if (!RSPAMD_TASK_IS_SKIPPED (task) && classify_pool) { register_async_thread (task->s); g_thread_pool_push (classify_pool, task, &err); if (err != NULL) { @@ -342,7 +346,7 @@ rspamd_task_process (struct rspamd_task *task, task->classify_pool = classify_pool; } } - if (task->is_skipped) { + if (RSPAMD_TASK_IS_SKIPPED (task)) { /* Call write_socket to write reply and exit */ task->state = WRITE_REPLY; } diff --git a/src/libserver/task.h b/src/libserver/task.h index 917ef8bcc..b989d3541 100644 --- a/src/libserver/task.h +++ b/src/libserver/task.h @@ -51,6 +51,19 @@ enum rspamd_metric_action { METRIC_ACTION_MAX }; +#define RSPAMD_TASK_FLAG_MIME (1 << 0) +#define RSPAMD_TASK_FLAG_JSON (1 << 1) +#define RSPAMD_TASK_FLAG_SKIP_EXTRA (1 << 2) +#define RSPAMD_TASK_FLAG_SKIP (1 << 3) +#define RSPAMD_TASK_FLAG_EXT_URLS (1 << 4) +#define RSPAMD_TASK_FLAG_SPAMC (1 << 5) +#define RSPAMD_TASK_FLAG_PASS_ALL (1 << 6) +#define RSPAMD_TASK_FLAG_NO_LOG (1 << 7) + +#define RSPAMD_TASK_IS_SKIPPED(task) (((task)->flags & RSPAMD_TASK_FLAG_SKIP)) +#define RSPAMD_TASK_IS_JSON(task) (((task)->flags & RSPAMD_TASK_FLAG_JSON)) +#define RSPAMD_TASK_IS_SPAMC(task) (((task)->flags & RSPAMD_TASK_FLAG_SPAMC)) + typedef gint (*protocol_reply_func)(struct rspamd_task *task); struct custom_command { @@ -75,13 +88,7 @@ struct rspamd_task { enum rspamd_command cmd; /**< command */ struct custom_command *custom_cmd; /**< custom command if any */ gint sock; /**< socket descriptor */ - /* TODO: all these fields should be converted to flags */ - gboolean is_mime; /**< if this task is mime task */ - gboolean is_json; /**< output is JSON */ - gboolean skip_extra_filters; /**< skip pre and post filters */ - gboolean is_skipped; /**< whether message was skipped by configuration */ - gboolean extended_urls; /**< output URLs in details */ - gboolean is_spamc; /**< need legacy spamc output */ + gint flags; gchar *helo; /**< helo header value */ gchar *queue_id; /**< queue id if specified */ @@ -133,8 +140,6 @@ struct rspamd_task { #endif struct timeval tv; /**< time of connection */ guint32 scan_milliseconds; /**< how much milliseconds passed */ - gboolean pass_all_filters; /**< pass task throught every rule */ - gboolean no_log; /**< do not log or write this task to the history */ guint32 parser_recursion; /**< for avoiding recursion stack overflow */ gboolean (*fin_callback)(void *arg); /**< calback for filters finalizing */ void *fin_arg; /**< argument for fin callback */ diff --git a/src/worker.c b/src/worker.c index 7a94ee77e..9c8b52ae2 100644 --- a/src/worker.c +++ b/src/worker.c @@ -228,8 +228,13 @@ accept_socket (gint fd, short what, void *arg) rspamd_inet_address_get_port (&addr)); /* Copy some variables */ + if (ctx->is_mime) { + new_task->flags |= RSPAMD_TASK_FLAG_MIME; + } + else { + new_task->flags &= ~RSPAMD_TASK_FLAG_MIME; + } new_task->sock = nfd; - new_task->is_mime = ctx->is_mime; memcpy (&new_task->client_addr, &addr, sizeof (addr)); worker->srv->stat->connections_count++; -- 2.39.5