From 37d6c7f23fd44283d1853141d39ecc3f3a2c54e6 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 28 Mar 2011 21:00:38 +0400 Subject: [PATCH] * Add more timeouts: for fuzzy operations, for worker task operations Handle miliseconds using a common macro. --- src/dns.c | 7 ++---- src/lmtp.c | 2 +- src/main.h | 3 +-- src/plugins/fuzzy_check.c | 16 +++++++++----- src/smtp.c | 9 +++----- src/util.h | 5 +++++ src/worker.c | 46 +++++++++++++++++++++------------------ 7 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/dns.c b/src/dns.c index 6e69b174a..07ad97ce8 100644 --- a/src/dns.c +++ b/src/dns.c @@ -1198,10 +1198,8 @@ make_dns_request (struct rspamd_dns_resolver *resolver, } /* Fill timeout */ - req->tv.tv_sec = resolver->request_timeout / 1000; - req->tv.tv_usec = (resolver->request_timeout - req->tv.tv_sec * 1000) * 1000; + msec_to_tv (resolver->request_timeout, &req->tv); - /* Now send request to server */ r = send_dns_request (req); @@ -1287,8 +1285,7 @@ dns_resolver_init (struct config_file *cfg) new->request_timeout = cfg->dns_timeout; new->max_retransmits = cfg->dns_retransmits; new->max_errors = cfg->dns_throttling_errors; - new->throttling_time.tv_sec = cfg->dns_throttling_time / 1000; - new->throttling_time.tv_usec = (cfg->dns_throttling_time - new->throttling_time.tv_sec * 1000) * 1000; + msec_to_tv (cfg->dns_throttling_time, &new->throttling_time); if (cfg->nameservers == NULL) { /* Parse resolv.conf */ diff --git a/src/lmtp.c b/src/lmtp.c index d080b7d95..d92656fad 100644 --- a/src/lmtp.c +++ b/src/lmtp.c @@ -303,7 +303,7 @@ start_lmtp_worker (struct rspamd_worker *worker) hostbuf[hostmax - 1] = '\0'; rspamd_snprintf (greetingbuf, sizeof (greetingbuf), "%d rspamd version %s LMTP on %s Ready\r\n", LMTP_OK, RVERSION, hostbuf); - io_tv.tv_sec = WORKER_IO_TIMEOUT; + io_tv.tv_sec = 60000; io_tv.tv_usec = 0; gperf_profiler_init (worker->srv->cfg, "lmtp"); diff --git a/src/main.h b/src/main.h index 02bebf89a..aac10529e 100644 --- a/src/main.h +++ b/src/main.h @@ -26,8 +26,7 @@ #define SOFT_SHUTDOWN_TIME 10 /* Default metric name */ #define DEFAULT_METRIC "default" -/* 60 seconds for worker's IO */ -#define WORKER_IO_TIMEOUT 60 + /* Spam subject */ #define SPAM_SUBJECT "*** SPAM *** " diff --git a/src/plugins/fuzzy_check.c b/src/plugins/fuzzy_check.c index f2b11c8f4..3f068ed99 100644 --- a/src/plugins/fuzzy_check.c +++ b/src/plugins/fuzzy_check.c @@ -57,7 +57,7 @@ #define DEFAULT_UPSTREAM_DEAD_TIME 300 #define DEFAULT_UPSTREAM_MAXERRORS 10 -#define IO_TIMEOUT 5 +#define DEFAULT_IO_TIMEOUT 500 #define DEFAULT_PORT 11335 struct storage_server { @@ -92,6 +92,7 @@ struct fuzzy_ctx { gint32 min_bytes; gint32 min_height; gint32 min_width; + guint32 io_timeout; }; struct fuzzy_client_session { @@ -346,6 +347,7 @@ fuzzy_check_module_init (struct config_file *cfg, struct module_ctx **ctx) register_module_opt ("fuzzy_check", "min_height", MODULE_OPT_TYPE_UINT); register_module_opt ("fuzzy_check", "min_width", MODULE_OPT_TYPE_UINT); register_module_opt ("fuzzy_check", "min_symbols", MODULE_OPT_TYPE_UINT); + register_module_opt ("fuzzy_check", "timeout", MODULE_OPT_TYPE_TIME); return 0; } @@ -393,6 +395,12 @@ fuzzy_check_module_config (struct config_file *cfg) else { fuzzy_module_ctx->min_width = 0; } + if ((value = get_module_opt (cfg, "fuzzy_check", "timeout")) != NULL) { + fuzzy_module_ctx->io_timeout = parse_time (value, TIME_SECONDS); + } + else { + fuzzy_module_ctx->io_timeout = DEFAULT_IO_TIMEOUT; + } if ((value = get_module_opt (cfg, "fuzzy_check", "mime_types")) != NULL) { fuzzy_module_ctx->mime_types = parse_mime_types (value); } @@ -625,8 +633,7 @@ register_fuzzy_call (struct worker_task *task, fuzzy_hash_t *h) /* Create session for a socket */ session = memory_pool_alloc (task->task_pool, sizeof (struct fuzzy_client_session)); event_set (&session->ev, sock, EV_WRITE, fuzzy_io_callback, session); - session->tv.tv_sec = IO_TIMEOUT; - session->tv.tv_usec = 0; + msec_to_tv (fuzzy_module_ctx->io_timeout, &session->tv); session->state = 0; session->h = h; session->task = task; @@ -753,8 +760,7 @@ register_fuzzy_controller_call (struct controller_session *session, struct worke /* Socket is made, create session */ s = memory_pool_alloc (session->session_pool, sizeof (struct fuzzy_learn_session)); event_set (&s->ev, sock, EV_WRITE, fuzzy_learn_callback, s); - s->tv.tv_sec = IO_TIMEOUT; - s->tv.tv_usec = 0; + msec_to_tv (fuzzy_module_ctx->io_timeout, &s->tv); s->task = task; s->h = memory_pool_alloc (session->session_pool, sizeof (fuzzy_hash_t)); memcpy (s->h, h, sizeof (fuzzy_hash_t)); diff --git a/src/smtp.c b/src/smtp.c index 793f33e3a..390e73117 100644 --- a/src/smtp.c +++ b/src/smtp.c @@ -539,12 +539,10 @@ smtp_make_delay (struct smtp_session *session) tv = memory_pool_alloc (session->pool, sizeof (struct timeval)); if (session->ctx->delay_jitter != 0) { jitter = g_random_int_range (0, session->ctx->delay_jitter); - tv->tv_sec = (session->ctx->smtp_delay + jitter) / 1000; - tv->tv_usec = (session->ctx->smtp_delay + jitter - tv->tv_sec * 1000) * 1000; + msec_to_tv (session->ctx->smtp_delay + jitter, tv); } else { - tv->tv_sec = session->ctx->smtp_delay / 1000; - tv->tv_usec = (session->ctx->smtp_delay - tv->tv_sec * 1000) * 1000; + msec_to_tv (session->ctx->smtp_delay, tv); } evtimer_set (tev, smtp_delay_handler, session); @@ -944,8 +942,7 @@ config_smtp_worker (struct rspamd_worker *worker) gchar *value; /* Init timeval */ - ctx->smtp_timeout.tv_sec = ctx->smtp_timeout_raw / 1000; - ctx->smtp_timeout.tv_usec = (ctx->smtp_timeout_raw - ctx->smtp_timeout.tv_sec * 1000) * 1000; + msec_to_tv (ctx->smtp_timeout_raw, &ctx->smtp_timeout); /* Init upstreams */ if ((value = ctx->upstreams_str) != NULL) { diff --git a/src/util.h b/src/util.h index 2ba01d05e..f4eea0bd6 100644 --- a/src/util.h +++ b/src/util.h @@ -173,4 +173,9 @@ const gchar * process_to_str (enum process_type type); */ enum process_type str_to_process (const gchar *str); +/* + * Convert milliseconds to timeval fields + */ +#define msec_to_tv(msec, tv) do { (tv)->tv_sec = (msec) / 1000; (tv)->tv_usec = ((msec) - (tv)->tv_sec * 1000) * 1000; } while(0) + #endif diff --git a/src/worker.c b/src/worker.c index 8f1177326..f9a6f936e 100644 --- a/src/worker.c +++ b/src/worker.c @@ -52,6 +52,9 @@ extern PerlInterpreter *perl_interpreter; # include #endif +/* 60 seconds for worker's IO */ +#define DEFAULT_WORKER_IO_TIMEOUT 60000 + #ifndef BUILD_STATIC #define MODULE_INIT_FUNC "module_init" @@ -60,15 +63,14 @@ extern PerlInterpreter *perl_interpreter; #define MODULE_AFTER_CONNECT_FUNC "after_connect" #define MODULE_PARSE_LINE_FUNC "parse_line" -struct custom_filter -{ - gchar *filename; /*< filename */ - GModule *handle; /*< returned by dlopen */ - void (*init_func) (struct config_file * cfg); /*< called at start of worker */ - void *(*before_connect) (void); /*< called when clients connects */ - gboolean (*process_line) (const gchar * line, size_t len, gchar ** output, void *user_data); /*< called when client send data line */ - void (*after_connect) (gchar ** output, gchar ** log_line, void *user_data); /*< called when client disconnects */ - void (*fin_func) (void); +struct custom_filter { + gchar *filename; /*< filename */ + GModule *handle; /*< returned by dlopen */ + void (*init_func) (struct config_file * cfg); /*< called at start of worker */ + void *(*before_connect) (void); /*< called when clients connects */ + gboolean (*process_line) (const gchar * line, size_t len, gchar ** output, void *user_data); /*< called when client send data line */ + void (*after_connect) (gchar ** output, gchar ** log_line, void *user_data); /*< called when client disconnects */ + void (*fin_func) (void); }; #endif @@ -76,16 +78,16 @@ struct custom_filter /* * Worker's context */ -struct rspamd_worker_ctx -{ - struct timeval io_tv; - /* Detect whether this worker is mime worker */ - gboolean is_mime; - /* Detect whether this worker is mime worker */ - gboolean is_custom; - GList *custom_filters; - /* DNS resolver */ - struct rspamd_dns_resolver *resolver; +struct rspamd_worker_ctx { + guint32 timeout; + struct timeval io_tv; + /* Detect whether this worker is mime worker */ + gboolean is_mime; + /* Detect whether this worker is mime worker */ + gboolean is_custom; + GList *custom_filters; + /* DNS resolver */ + struct rspamd_dns_resolver *resolver; }; static gboolean write_socket (void *arg); @@ -603,8 +605,7 @@ accept_socket (gint fd, short what, void *arg) new_task->is_mime = ctx->is_mime; worker->srv->stat->connections_count++; new_task->resolver = ctx->resolver; - ctx->io_tv.tv_sec = WORKER_IO_TIMEOUT; - ctx->io_tv.tv_usec = 0; + msec_to_tv (ctx->timeout, &ctx->io_tv); /* Set up dispatcher */ new_task->dispatcher = @@ -737,7 +738,10 @@ init_worker (void) ctx = g_malloc0 (sizeof (struct rspamd_worker_ctx)); ctx->is_mime = TRUE; + ctx->timeout = DEFAULT_WORKER_IO_TIMEOUT; + register_worker_opt (TYPE_WORKER, "mime", xml_handle_boolean, ctx, G_STRUCT_OFFSET (struct rspamd_worker_ctx, is_mime)); + register_worker_opt (TYPE_WORKER, "timeout", xml_handle_seconds, ctx, G_STRUCT_OFFSET (struct rspamd_worker_ctx, timeout)); return ctx; } -- 2.39.5