[nginx] Upstream: $upstream_header_time variable.

Homutov Vladimir vl at nginx.com
Thu Jan 15 13:52:27 UTC 2015


details:   http://hg.nginx.org/nginx/rev/0a198a517eaf
branches:  
changeset: 5964:0a198a517eaf
user:      Vladimir Homutov <vl at nginx.com>
date:      Wed Jan 14 09:03:35 2015 +0300
description:
Upstream: $upstream_header_time variable.

Keeps time spent on obtaining the header from an upstream server.  The value is
formatted similar to the $upstream_response_time variable.

diffstat:

 src/http/ngx_http_upstream.c |  24 ++++++++++++++++++++++--
 src/http/ngx_http_upstream.h |   2 ++
 2 files changed, 24 insertions(+), 2 deletions(-)

diffs (74 lines):

diff -r e0920ea61632 -r 0a198a517eaf src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Tue Jan 13 20:13:16 2015 +0300
+++ b/src/http/ngx_http_upstream.c	Wed Jan 14 09:03:35 2015 +0300
@@ -359,6 +359,10 @@ static ngx_http_variable_t  ngx_http_ups
       ngx_http_upstream_status_variable, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
+    { ngx_string("upstream_header_time"), NULL,
+      ngx_http_upstream_response_time_variable, 1,
+      NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
     { ngx_string("upstream_response_time"), NULL,
       ngx_http_upstream_response_time_variable, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -1315,6 +1319,7 @@ ngx_http_upstream_connect(ngx_http_reque
     tp = ngx_timeofday();
     u->state->response_sec = tp->sec;
     u->state->response_msec = tp->msec;
+    u->state->header_sec = (time_t) NGX_ERROR;
 
     rc = ngx_event_connect_peer(&u->peer);
 
@@ -1836,6 +1841,7 @@ ngx_http_upstream_process_header(ngx_htt
 {
     ssize_t            n;
     ngx_int_t          rc;
+    ngx_time_t        *tp;
     ngx_connection_t  *c;
 
     c = u->peer.connection;
@@ -1956,6 +1962,10 @@ ngx_http_upstream_process_header(ngx_htt
 
     /* rc == NGX_OK */
 
+    tp = ngx_timeofday();
+    u->state->header_sec = tp->sec - u->state->response_sec;
+    u->state->header_msec = tp->msec - u->state->response_msec;
+
     if (u->headers_in.status_n >= NGX_HTTP_SPECIAL_RESPONSE) {
 
         if (ngx_http_upstream_test_next(r, u) == NGX_OK) {
@@ -4822,8 +4832,18 @@ ngx_http_upstream_response_time_variable
 
     for ( ;; ) {
         if (state[i].status) {
-            ms = (ngx_msec_int_t)
-                     (state[i].response_sec * 1000 + state[i].response_msec);
+
+            if (data
+                && state[i].header_sec != (time_t) NGX_ERROR)
+            {
+                ms = (ngx_msec_int_t)
+                      (state[i].header_sec * 1000 + state[i].header_msec);
+
+            } else {
+                ms = (ngx_msec_int_t)
+                      (state[i].response_sec * 1000 + state[i].response_msec);
+            }
+
             ms = ngx_max(ms, 0);
             p = ngx_sprintf(p, "%T.%03M", (time_t) ms / 1000, ms % 1000);
 
diff -r e0920ea61632 -r 0a198a517eaf src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h	Tue Jan 13 20:13:16 2015 +0300
+++ b/src/http/ngx_http_upstream.h	Wed Jan 14 09:03:35 2015 +0300
@@ -60,6 +60,8 @@ typedef struct {
     ngx_uint_t                       status;
     time_t                           response_sec;
     ngx_uint_t                       response_msec;
+    time_t                           header_sec;
+    ngx_uint_t                       header_msec;
     off_t                            response_length;
 
     ngx_str_t                       *peer;



More information about the nginx-devel mailing list