commit dbda6df50c6f3338854ddb70318b29b9705407ee
parent 9f8d5675a8d2c3f386f806c6ecc7fffcf40ecdbf
Author: Evgeny Grin (Karlson2k) <k2k@drgrin.dev>
Date: Wed, 24 Dec 2025 21:30:35 +0100
Moved connection timeout data to dedicated struct
Diffstat:
6 files changed, 57 insertions(+), 44 deletions(-)
diff --git a/src/mhd2/conn_get_info.c b/src/mhd2/conn_get_info.c
@@ -130,7 +130,7 @@ MHD_connection_get_info_dynamic_sz (
if (sizeof(output_buf->v_connection_timeout_uint32) <= output_buf_size)
{
output_buf->v_connection_timeout_uint32 =
- connection->connection_timeout_ms;
+ connection->timeout.milsec;
return MHD_SC_OK;
}
return MHD_SC_INFO_GET_BUFF_TOO_SMALL;
diff --git a/src/mhd2/conn_timeout.c b/src/mhd2/conn_timeout.c
@@ -62,7 +62,7 @@
MHD_INTERNAL MHD_FN_PAR_NONNULL_ALL_ bool
mhd_conn_is_timeout_expired (struct MHD_Connection *restrict c)
{
- const uint_fast64_t timeout = c->connection_timeout_ms;
+ const uint_fast64_t timeout = c->timeout.milsec;
uint_fast64_t now;
uint_fast64_t since_actv;
@@ -72,19 +72,19 @@ mhd_conn_is_timeout_expired (struct MHD_Connection *restrict c)
return false;
now = mhd_monotonic_msec_counter (); // TODO: Get and use timer value one time only per round
- since_actv = now - c->last_activity;
+ since_actv = now - c->timeout.last_act;
/* Keep the next lines in sync with #connection_get_wait() to avoid
* undesired side-effects like busy-waiting. */
if (timeout < since_actv)
{
- const uint_fast64_t jump_back = c->last_activity - now;
+ const uint_fast64_t jump_back = c->timeout.last_act - now;
if (jump_back < since_actv)
{
/* Very unlikely that it is more than quarter-million years pause.
* More likely that system clock jumps back. */
if (4000 >= jump_back)
{
- c->last_activity = now; /* Avoid repetitive messages.
+ c->timeout.last_act = now; /* Avoid repetitive messages.
Warn: the order of connections sorted
by timeout is not updated. */
mhd_LOG_PRINT (c->daemon, MHD_SC_SYS_CLOCK_JUMP_BACK_CORRECTED, \
@@ -116,10 +116,10 @@ mhd_conn_init_activity_timeout (struct MHD_Connection *restrict c,
mhd_assert (! c->suspended);
- c->connection_timeout_ms = timeout;
+ c->timeout.milsec = timeout;
if (0 != timeout)
- c->last_activity = mhd_monotonic_msec_counter (); // TODO: Get and use time value one time per round
+ c->timeout.last_act = mhd_monotonic_msec_counter (); // TODO: Get and use time value one time per round
if (mhd_D_HAS_THR_PER_CONN (d))
return;
@@ -127,11 +127,11 @@ mhd_conn_init_activity_timeout (struct MHD_Connection *restrict c,
if (timeout == d->conns.cfg.timeout_milsec)
mhd_DLINKEDL_INS_FIRST_D (&(d->conns.def_timeout),
c,
- by_timeout);
+ timeout.tmout_list);
else
mhd_DLINKEDL_INS_FIRST_D (&(d->conns.cust_timeout),
c,
- by_timeout);
+ timeout.tmout_list);
}
@@ -148,14 +148,14 @@ mhd_conn_deinit_activity_timeout (struct MHD_Connection *restrict c)
if (mhd_D_HAS_THR_PER_CONN (d))
return;
- if (c->connection_timeout_ms == d->conns.cfg.timeout_milsec)
+ if (c->timeout.milsec == d->conns.cfg.timeout_milsec)
mhd_DLINKEDL_DEL_D (&(d->conns.def_timeout),
c,
- by_timeout);
+ timeout.tmout_list);
else
mhd_DLINKEDL_DEL_D (&(d->conns.cust_timeout), \
c,
- by_timeout);
+ timeout.tmout_list);
}
@@ -169,18 +169,22 @@ mhd_conn_update_activity_mark (struct MHD_Connection *restrict c)
mhd_assert (! c->suspended);
- if (0 == c->connection_timeout_ms)
+ if (0 == c->timeout.milsec)
return; /* Skip update of activity for connections
without timeout timer. */
- c->last_activity = mhd_monotonic_msec_counter (); // TODO: Get and use time value one time per round
+ c->timeout.last_act = mhd_monotonic_msec_counter (); // TODO: Get and use time value one time per round
if (mhd_D_HAS_THR_PER_CONN (d))
return; /* each connection has personal timeout */
- if (c->connection_timeout_ms != d->conns.cfg.timeout_milsec)
+ if (c->timeout.milsec != d->conns.cfg.timeout_milsec)
return; /* custom timeout, no need to move it in "normal" DLL */
/* move connection to head of timeout list (by remove + add operation) */
- mhd_DLINKEDL_DEL_D (&(d->conns.def_timeout), c, by_timeout);
- mhd_DLINKEDL_INS_FIRST_D (&(d->conns.def_timeout), c, by_timeout);
+ mhd_DLINKEDL_DEL_D (&(d->conns.def_timeout),
+ c,
+ timeout.tmout_list);
+ mhd_DLINKEDL_INS_FIRST_D (&(d->conns.def_timeout),
+ c,
+ timeout.tmout_list);
}
diff --git a/src/mhd2/daemon_add_conn.c b/src/mhd2/daemon_add_conn.c
@@ -1150,8 +1150,8 @@ mhd_conn_remove_from_daemon (struct MHD_Connection *restrict c)
mhd_assert (0 && "Not implemented yet");
// TODO: Support "thread per connection"
}
- mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (c, by_timeout));
- mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (c, by_timeout));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (&(c->timeout), tmout_list));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (&(c->timeout), tmout_list));
mhd_assert (NULL == c->pool);
mhd_DLINKEDL_DEL (&(c->daemon->conns), c, all_conn);
@@ -1181,8 +1181,8 @@ mhd_conn_close_final (struct MHD_Connection *restrict c)
mhd_assert (c != mhd_DLINKEDL_GET_FIRST (&(c->daemon->events), proc_ready));
mhd_assert (c != mhd_DLINKEDL_GET_LAST (&(c->daemon->events), proc_ready));
- mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (c, by_timeout));
- mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (c, by_timeout));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (&(c->timeout), tmout_list));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (&(c->timeout), tmout_list));
mhd_assert (NULL == c->pool);
mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (c, all_conn));
diff --git a/src/mhd2/events_process.c b/src/mhd2/events_process.c
@@ -242,7 +242,7 @@ start_resuming_connection (struct MHD_Connection *restrict c,
#endif /* mhd_DEBUG_SUSPEND_RESUME */
c->suspended = false;
mhd_conn_init_activity_timeout (c,
- (uint_fast32_t) c->connection_timeout_ms);
+ c->timeout.milsec);
mhd_conn_mark_ready (c, d); /* Force processing connection in this round */
}
diff --git a/src/mhd2/mhd_connection.h b/src/mhd2/mhd_connection.h
@@ -542,6 +542,33 @@ struct mhd_ConnExtrEvents
};
+/**
+ * The helper struct for the connections list
+ */
+mhd_DLINKEDL_LINKS_DEF (MHD_Connection);
+
+/**
+ * Connection's activity timeout data
+ */
+struct mhd_ConnTimeoutData
+{
+ /**
+ * Connection's maximum idle time before closing by timeout.
+ * Zero for no timeout.
+ */
+ uint_fast32_t milsec;
+
+ /**
+ * The time of the the connection's last network activity
+ */
+ uint_fast64_t last_act;
+
+ /**
+ * The list of connections sorted by timeout
+ */
+ mhd_DLNKDL_LINKS (MHD_Connection,tmout_list);
+};
+
struct mhd_ConnDebugData
{
bool closing_started;
@@ -573,11 +600,6 @@ enum MHD_FIXED_ENUM_ mhd_ConnReuse
};
/**
- * The helper struct for the connections list
- */
-mhd_DLINKEDL_LINKS_DEF (MHD_Connection);
-
-/**
* State kept for HTTP network connection.
*/
struct MHD_Connection
@@ -642,9 +664,9 @@ struct MHD_Connection
mhd_DLNKDL_LINKS (MHD_Connection,proc_ready);
/**
- * The list of connections sorted by timeout
+ * Connection's activity timeout data
*/
- mhd_DLNKDL_LINKS (MHD_Connection,by_timeout);
+ struct mhd_ConnTimeoutData timeout;
#ifdef MHD_SUPPORT_UPGRADE
/**
@@ -772,19 +794,6 @@ struct MHD_Connection
size_t continue_message_write_offset;
/**
- * Last time this connection had any activity
- * (reading or writing).
- */
- uint_fast64_t last_activity;
-
- /**
- * After how many milliseconds of inactivity should
- * this connection time out?
- * Zero for no timeout.
- */
- uint_fast64_t connection_timeout_ms;
-
- /**
* Some error happens during processing the connection therefore this
* connection must be closed.
* The error may come from the client side (like wrong request format),
diff --git a/src/mhd2/upgrade_proc.c b/src/mhd2/upgrade_proc.c
@@ -118,8 +118,8 @@ mhd_upgrade_finish_switch_to_upgraded (struct MHD_Connection *restrict c)
c->stage = mhd_HTTP_STAGE_UPGRADED;
mhd_assert (! c->in_proc_ready);
- mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (c, by_timeout));
- mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (c, by_timeout));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_PREV (c, timeout.tmout_list));
+ mhd_assert (NULL == mhd_DLINKEDL_GET_NEXT (c, timeout.tmout_list));
mhd_assert (c != mhd_DLINKEDL_GET_FIRST (&(c->daemon->conns), def_timeout));
mhd_assert (c != mhd_DLINKEDL_GET_LAST (&(c->daemon->conns), def_timeout));
mhd_assert (c != mhd_DLINKEDL_GET_FIRST (&(c->daemon->conns), cust_timeout));