Skip to content

Commit

Permalink
[Minor] Add utility function for task timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
vstakhov committed Aug 19, 2022
1 parent 256a3e0 commit bfd6f0d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
11 changes: 2 additions & 9 deletions src/controller.c
Expand Up @@ -2189,7 +2189,7 @@ rspamd_controller_handle_scan (struct rspamd_http_connection_entry *conn_ent,
goto end;
}

if (ctx->task_timeout > 0.0) {
if (!isnan(ctx->task_timeout) && ctx->task_timeout > 0.0) {
task->timeout_ev.data = task;
ev_timer_init (&task->timeout_ev, rspamd_task_timeout,
ctx->task_timeout, ctx->task_timeout);
Expand Down Expand Up @@ -4080,14 +4080,7 @@ start_controller_worker (struct rspamd_worker *worker)
rspamd_ftok_icase_equal, rspamd_fstring_mapped_ftok_free,
rspamd_plugin_cbdata_dtor);

if (isnan (ctx->task_timeout)) {
if (isnan (ctx->cfg->task_timeout)) {
ctx->task_timeout = 0;
}
else {
ctx->task_timeout = ctx->cfg->task_timeout;
}
}
ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, ctx->task_timeout);

if (ctx->secure_ip != NULL) {
rspamd_config_radix_from_ucl (ctx->cfg, ctx->secure_ip,
Expand Down
29 changes: 29 additions & 0 deletions src/libserver/worker_util.c
Expand Up @@ -57,6 +57,8 @@

#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#include <math.h>

#endif

#include "contrib/libev/ev.h"
Expand Down Expand Up @@ -2221,3 +2223,30 @@ rspamd_worker_init_controller (struct rspamd_worker *worker,
ctx->resolver, worker, RSPAMD_MAP_WATCH_SCANNER);
}
}

gdouble
rspamd_worker_check_and_adjust_timeout (struct rspamd_config *cfg, gdouble timeout)
{
if (isnan (timeout)) {
/* Use implicit timeout from cfg->task_timeout */
timeout = cfg->task_timeout;
}

if (isnan (timeout)) {
return timeout;
}

struct rspamd_symcache_timeout_result *tres = rspamd_symcache_get_max_timeout (cfg->cache);
g_assert (tres != 0);

if (tres->max_timeout > timeout) {
msg_info_config("configured task_timeout %.2f is less than maximum symbols cache timeout %.2f, so"
"some symbols could be terminated early", timeout, tres->max_timeout);
/* TODO: list timeouts for top symbols */
}

rspamd_symcache_timeout_result_free (tres);

/* TODO: maybe adjust timeout */
return timeout;
}
9 changes: 9 additions & 0 deletions src/libserver/worker_util.h
Expand Up @@ -173,6 +173,15 @@ void rspamd_hard_terminate (struct rspamd_main *rspamd_main) G_GNUC_NORETURN;
*/
gboolean rspamd_worker_is_scanner (struct rspamd_worker *w);

/**
* Checks
* @param cfg
* @param timeout
* @return
*/
gdouble rspamd_worker_check_and_adjust_timeout (struct rspamd_config *cfg,
gdouble timeout);

/**
* Returns TRUE if a specific worker is a primary controller
* @param w
Expand Down
5 changes: 4 additions & 1 deletion src/rspamd_proxy.c
Expand Up @@ -157,6 +157,7 @@ struct rspamd_proxy_ctx {
struct rspamd_milter_context milter_ctx;
/* Language detector */
struct rspamd_lang_detector *lang_det;
gdouble task_timeout;
};

enum rspamd_backend_flags {
Expand Down Expand Up @@ -1886,7 +1887,7 @@ rspamd_proxy_self_scan (struct rspamd_proxy_session *session)

}
else if (session->ctx->has_self_scan) {
if (session->ctx->cfg->task_timeout > 0) {
if (!isnan(session->ctx->task_timeout) && session->ctx->task_timeout > 0) {
task->timeout_ev.data = task;
ev_timer_init (&task->timeout_ev, rspamd_task_timeout,
session->ctx->cfg->task_timeout,
Expand Down Expand Up @@ -2374,6 +2375,8 @@ start_rspamd_proxy (struct rspamd_worker *worker)
/* Additional initialisation needed */
rspamd_worker_init_scanner (worker, ctx->event_loop, ctx->resolver,
&ctx->lang_det);
/* Always yse cfg->task_timeout */
ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, NAN);

if (worker->index == 0) {
/*
Expand Down
11 changes: 2 additions & 9 deletions src/worker.c
Expand Up @@ -186,7 +186,7 @@ rspamd_worker_body_handler (struct rspamd_http_connection *conn,
}

/* Set global timeout for the task */
if (ctx->task_timeout > 0.0) {
if (!isnan(ctx->task_timeout) && ctx->task_timeout > 0.0) {
task->timeout_ev.data = task;
ev_timer_init (&task->timeout_ev, rspamd_task_timeout,
ctx->task_timeout,
Expand Down Expand Up @@ -493,14 +493,7 @@ start_worker (struct rspamd_worker *worker)
rspamd_symcache_start_refresh (worker->srv->cfg->cache, ctx->event_loop,
worker);

if (isnan (ctx->task_timeout)) {
if (isnan (ctx->cfg->task_timeout)) {
ctx->task_timeout = 0;
}
else {
ctx->task_timeout = ctx->cfg->task_timeout;
}
}
ctx->task_timeout = rspamd_worker_check_and_adjust_timeout(ctx->cfg, ctx->task_timeout);

ctx->resolver = rspamd_dns_resolver_init (worker->srv->logger,
ctx->event_loop,
Expand Down

0 comments on commit bfd6f0d

Please sign in to comment.