Skip to content

Commit 3329aa9

Browse files
committed
Switched to ngx_current_msec for $request_time tracking.
This ensures that $request_time (and $session_time in stream) is not affected by system time changes on platforms with monotonic time available. Also, it is now tracked similarly to $upstream_response_time, and there should be no unexpected differences as previously observed with clock_gettime(CLOCK_MONOTONIC_COARSE) (see 7939:9e7de0547f09).
1 parent 72efb40 commit 3329aa9

File tree

8 files changed

+9
-32
lines changed

8 files changed

+9
-32
lines changed

src/http/modules/ngx_http_log_module.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -825,13 +825,9 @@ static u_char *
825825
ngx_http_log_request_time(ngx_http_request_t *r, u_char *buf,
826826
ngx_http_log_op_t *op)
827827
{
828-
ngx_time_t *tp;
829-
ngx_msec_int_t ms;
828+
ngx_msec_int_t ms;
830829

831-
tp = ngx_timeofday();
832-
833-
ms = (ngx_msec_int_t)
834-
((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
830+
ms = (ngx_msec_int_t) (ngx_current_msec - r->start_time);
835831
ms = ngx_max(ms, 0);
836832

837833
return ngx_sprintf(buf, "%T.%03M", (time_t) ms / 1000, ms % 1000);

src/http/ngx_http_core_module.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,7 +2323,6 @@ ngx_http_subrequest(ngx_http_request_t *r,
23232323
ngx_str_t *uri, ngx_str_t *args, ngx_http_request_t **psr,
23242324
ngx_http_post_subrequest_t *ps, ngx_uint_t flags)
23252325
{
2326-
ngx_time_t *tp;
23272326
ngx_connection_t *c;
23282327
ngx_http_request_t *sr;
23292328
ngx_http_core_srv_conf_t *cscf;
@@ -2470,9 +2469,7 @@ ngx_http_subrequest(ngx_http_request_t *r,
24702469
sr->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
24712470
sr->subrequests = r->subrequests - 1;
24722471

2473-
tp = ngx_timeofday();
2474-
sr->start_sec = tp->sec;
2475-
sr->start_msec = tp->msec;
2472+
sr->start_time = ngx_current_msec;
24762473

24772474
r->main->count++;
24782475

src/http/ngx_http_request.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,6 @@ static ngx_http_request_t *
560560
ngx_http_alloc_request(ngx_connection_t *c)
561561
{
562562
ngx_pool_t *pool;
563-
ngx_time_t *tp;
564563
ngx_http_request_t *r;
565564
ngx_http_connection_t *hc;
566565
ngx_http_core_srv_conf_t *cscf;
@@ -635,9 +634,7 @@ ngx_http_alloc_request(ngx_connection_t *c)
635634
r->main = r;
636635
r->count = 1;
637636

638-
tp = ngx_timeofday();
639-
r->start_sec = tp->sec;
640-
r->start_msec = tp->msec;
637+
r->start_time = ngx_current_msec;
641638

642639
r->method = NGX_HTTP_UNKNOWN;
643640
r->http_version = NGX_HTTP_VERSION_10;

src/http/ngx_http_request.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -410,8 +410,7 @@ struct ngx_http_request_s {
410410
ngx_http_request_body_t *request_body;
411411

412412
time_t lingering_time;
413-
time_t start_sec;
414-
ngx_msec_t start_msec;
413+
ngx_msec_t start_time;
415414

416415
ngx_uint_t method;
417416
ngx_uint_t http_version;

src/http/ngx_http_variables.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,18 +2216,14 @@ ngx_http_variable_request_time(ngx_http_request_t *r,
22162216
ngx_http_variable_value_t *v, uintptr_t data)
22172217
{
22182218
u_char *p;
2219-
ngx_time_t *tp;
22202219
ngx_msec_int_t ms;
22212220

22222221
p = ngx_pnalloc(r->pool, NGX_TIME_T_LEN + 4);
22232222
if (p == NULL) {
22242223
return NGX_ERROR;
22252224
}
22262225

2227-
tp = ngx_timeofday();
2228-
2229-
ms = (ngx_msec_int_t)
2230-
((tp->sec - r->start_sec) * 1000 + (tp->msec - r->start_msec));
2226+
ms = (ngx_msec_int_t) (ngx_current_msec - r->start_time);
22312227
ms = ngx_max(ms, 0);
22322228

22332229
v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;

src/stream/ngx_stream.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ struct ngx_stream_session_s {
202202
ngx_connection_t *connection;
203203

204204
off_t received;
205-
time_t start_sec;
206-
ngx_msec_t start_msec;
205+
ngx_msec_t start_time;
207206

208207
ngx_log_handler_pt log_handler;
209208

src/stream/ngx_stream_handler.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ ngx_stream_init_connection(ngx_connection_t *c)
2323
u_char text[NGX_SOCKADDR_STRLEN];
2424
size_t len;
2525
ngx_uint_t i;
26-
ngx_time_t *tp;
2726
ngx_event_t *rev;
2827
struct sockaddr *sa;
2928
ngx_stream_port_t *port;
@@ -169,9 +168,7 @@ ngx_stream_init_connection(ngx_connection_t *c)
169168
return;
170169
}
171170

172-
tp = ngx_timeofday();
173-
s->start_sec = tp->sec;
174-
s->start_msec = tp->msec;
171+
s->start_time = ngx_current_msec;
175172

176173
rev = c->read;
177174
rev->handler = ngx_stream_session_handler;

src/stream/ngx_stream_variables.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -753,18 +753,14 @@ ngx_stream_variable_session_time(ngx_stream_session_t *s,
753753
ngx_stream_variable_value_t *v, uintptr_t data)
754754
{
755755
u_char *p;
756-
ngx_time_t *tp;
757756
ngx_msec_int_t ms;
758757

759758
p = ngx_pnalloc(s->connection->pool, NGX_TIME_T_LEN + 4);
760759
if (p == NULL) {
761760
return NGX_ERROR;
762761
}
763762

764-
tp = ngx_timeofday();
765-
766-
ms = (ngx_msec_int_t)
767-
((tp->sec - s->start_sec) * 1000 + (tp->msec - s->start_msec));
763+
ms = (ngx_msec_int_t) (ngx_current_msec - s->start_time);
768764
ms = ngx_max(ms, 0);
769765

770766
v->len = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000) - p;

0 commit comments

Comments
 (0)