]> git.ipfire.org Git - thirdparty/rspamd.git/commitdiff
[Minor] Optimize fuzzy_tcp_refresh_timeout
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 21 Mar 2026 13:40:21 +0000 (13:40 +0000)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 21 Mar 2026 13:40:21 +0000 (13:40 +0000)
Use ev_timer_again instead of stop/set/start triple, reducing three
libev calls to one. Move timeout refresh out of the write loop so it
is called once after draining the queue rather than on every write().

src/plugins/fuzzy_check.c

index f1788cfd2c533051cbb83473a06ec45b9650b293..e66b399a5d423b4a9a41fad6579626a608266578 100644 (file)
@@ -708,16 +708,16 @@ fuzzy_tcp_connection_unref(gpointer conn_ptr)
 
 /**
  * Refresh TCP connection timeout after successful I/O activity
- * This prevents active connections from timing out during data transfer
+ * This prevents active connections from timing out during data transfer.
+ * Uses ev_timer_again with a repeat interval for a single syscall instead
+ * of the stop/set/start triple.
  */
 static void
 fuzzy_tcp_refresh_timeout(struct fuzzy_tcp_connection *conn)
 {
        if (conn->rule->tcp_timeout > 0 && ev_is_active(&conn->ev.tm)) {
-               /* Stop and restart timer to extend timeout */
-               ev_timer_stop(conn->event_loop, &conn->ev.tm);
-               ev_timer_set(&conn->ev.tm, conn->rule->tcp_timeout, 0.0);
-               ev_timer_start(conn->event_loop, &conn->ev.tm);
+               conn->ev.tm.repeat = conn->rule->tcp_timeout;
+               ev_timer_again(conn->event_loop, &conn->ev.tm);
        }
 }
 
@@ -1191,6 +1191,7 @@ fuzzy_tcp_write_handler(struct fuzzy_tcp_connection *conn)
 {
        struct fuzzy_tcp_write_buf *buf;
        ssize_t r;
+       gboolean did_write = FALSE;
 
        while ((buf = g_queue_peek_head(conn->write_queue)) != NULL) {
                /* Write remaining data */
@@ -1246,9 +1247,7 @@ fuzzy_tcp_write_handler(struct fuzzy_tcp_connection *conn)
                }
 
                buf->bytes_written += r;
-
-               /* Refresh timeout after successful write - keeps connection alive during active data transfer */
-               fuzzy_tcp_refresh_timeout(conn);
+               did_write = TRUE;
 
                if (buf->bytes_written >= buf->total_len) {
                        /* Buffer fully sent, remove from queue and free */
@@ -1262,6 +1261,11 @@ fuzzy_tcp_write_handler(struct fuzzy_tcp_connection *conn)
                }
        }
 
+       /* Refresh timeout once after all writes, not per-buffer */
+       if (did_write) {
+               fuzzy_tcp_refresh_timeout(conn);
+       }
+
        /* Queue is empty, switch to read-only to avoid busy-looping on EV_WRITE */
        rspamd_ev_watcher_reschedule(conn->event_loop, &conn->ev, EV_READ);
 }