[PATCH 1 of 2] Switched to ngx_current_msec for lingering_time tracking

Maxim Dounin mdounin at mdounin.ru
Thu Jun 26 20:53:07 UTC 2025


# HG changeset patch
# User Maxim Dounin <mdounin at mdounin.ru>
# Date 1750962077 -10800
#      Thu Jun 26 21:21:17 2025 +0300
# Node ID 38d84f18b138ae4989d071fcb5cabda2f846b219
# Parent  121d6685362076da8b261e92674b17c2a7ca145e
Switched to ngx_current_msec for lingering_time tracking.

This ensures that lingering time is not affected by system time changes
on platforms with monotonic time available.

diff --git a/src/http/ngx_http_request.c b/src/http/ngx_http_request.c
--- a/src/http/ngx_http_request.c
+++ b/src/http/ngx_http_request.c
@@ -2772,8 +2772,7 @@ ngx_http_finalize_connection(ngx_http_re
             ngx_add_timer(r->connection->read, clcf->lingering_timeout);
 
             if (r->lingering_time == 0) {
-                r->lingering_time = ngx_time()
-                                      + (time_t) (clcf->lingering_time / 1000);
+                r->lingering_time = ngx_current_msec + clcf->lingering_time;
             }
         }
 
@@ -3538,7 +3537,7 @@ ngx_http_set_lingering_close(ngx_connect
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
     if (r->lingering_time == 0) {
-        r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
+        r->lingering_time = ngx_current_msec + clcf->lingering_time;
     }
 
 #if (NGX_HTTP_SSL)
@@ -3618,7 +3617,7 @@ ngx_http_lingering_close_handler(ngx_eve
         return;
     }
 
-    timer = (ngx_msec_t) r->lingering_time - (ngx_msec_t) ngx_time();
+    timer = r->lingering_time - ngx_current_msec;
     if ((ngx_msec_int_t) timer <= 0) {
         ngx_http_close_request(r, 0);
         return;
@@ -3647,8 +3646,6 @@ ngx_http_lingering_close_handler(ngx_eve
 
     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
-    timer *= 1000;
-
     if (timer > clcf->lingering_timeout) {
         timer = clcf->lingering_timeout;
     }
diff --git a/src/http/ngx_http_request.h b/src/http/ngx_http_request.h
--- a/src/http/ngx_http_request.h
+++ b/src/http/ngx_http_request.h
@@ -411,7 +411,7 @@ struct ngx_http_request_s {
 
     ngx_http_request_body_t          *request_body;
 
-    time_t                            lingering_time;
+    ngx_msec_t                        lingering_time;
     ngx_msec_t                        start_time;
 
     ngx_uint_t                        method;
diff --git a/src/http/ngx_http_request_body.c b/src/http/ngx_http_request_body.c
--- a/src/http/ngx_http_request_body.c
+++ b/src/http/ngx_http_request_body.c
@@ -833,7 +833,7 @@ ngx_http_discarded_request_body_handler(
     }
 
     if (r->lingering_time) {
-        timer = (ngx_msec_t) r->lingering_time - (ngx_msec_t) ngx_time();
+        timer = r->lingering_time - ngx_current_msec;
 
         if ((ngx_msec_int_t) timer <= 0) {
             r->discarding_body = 0;
@@ -874,8 +874,6 @@ ngx_http_discarded_request_body_handler(
 
         clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
 
-        timer *= 1000;
-
         if (timer > clcf->lingering_timeout) {
             timer = clcf->lingering_timeout;
         }
diff --git a/src/http/v2/ngx_http_v2.c b/src/http/v2/ngx_http_v2.c
--- a/src/http/v2/ngx_http_v2.c
+++ b/src/http/v2/ngx_http_v2.c
@@ -765,8 +765,7 @@ ngx_http_v2_lingering_close(ngx_connecti
     }
 
     if (h2c->lingering_time == 0) {
-        h2c->lingering_time = ngx_time()
-                              + (time_t) (clcf->lingering_time / 1000);
+        h2c->lingering_time = ngx_current_msec + clcf->lingering_time;
     }
 
 #if (NGX_HTTP_SSL)
@@ -844,7 +843,7 @@ ngx_http_v2_lingering_close_handler(ngx_
         return;
     }
 
-    timer = (ngx_msec_t) h2c->lingering_time - (ngx_msec_t) ngx_time();
+    timer = h2c->lingering_time - ngx_current_msec;
     if ((ngx_msec_int_t) timer <= 0) {
         ngx_http_close_connection(c);
         return;
@@ -873,7 +872,6 @@ ngx_http_v2_lingering_close_handler(ngx_
 
     clcf = ngx_http_get_module_loc_conf(h2c->http_connection->conf_ctx,
                                         ngx_http_core_module);
-    timer *= 1000;
 
     if (timer > clcf->lingering_timeout) {
         timer = clcf->lingering_timeout;
diff --git a/src/http/v2/ngx_http_v2.h b/src/http/v2/ngx_http_v2.h
--- a/src/http/v2/ngx_http_v2.h
+++ b/src/http/v2/ngx_http_v2.h
@@ -162,7 +162,7 @@ struct ngx_http_v2_connection_s {
     ngx_uint_t                       closed_nodes;
     ngx_uint_t                       last_sid;
 
-    time_t                           lingering_time;
+    ngx_msec_t                       lingering_time;
 
     ngx_msec_t                       send_min_last;
     off_t                            send_min_excess;



More information about the nginx-devel mailing list