[nginx] Upstream: $upstream_connect_time.

Ruslan Ermilov ru at nginx.com
Fri May 15 22:32:45 UTC 2015


details:   http://hg.nginx.org/nginx/rev/74b6ef56ea56
branches:  
changeset: 6147:74b6ef56ea56
user:      Ruslan Ermilov <ru at nginx.com>
date:      Sat May 16 01:32:27 2015 +0300
description:
Upstream: $upstream_connect_time.

The variable keeps time spent on establishing a connection with
the upstream server.

diffstat:

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

diffs (58 lines):

diff -r 59fc60585f1e -r 74b6ef56ea56 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Sat May 16 01:31:04 2015 +0300
+++ b/src/http/ngx_http_upstream.c	Sat May 16 01:32:27 2015 +0300
@@ -363,6 +363,10 @@ static ngx_http_variable_t  ngx_http_ups
       ngx_http_upstream_status_variable, 0,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
 
+    { ngx_string("upstream_connect_time"), NULL,
+      ngx_http_upstream_response_time_variable, 2,
+      NGX_HTTP_VAR_NOCACHEABLE, 0 },
+
     { ngx_string("upstream_header_time"), NULL,
       ngx_http_upstream_response_time_variable, 1,
       NGX_HTTP_VAR_NOCACHEABLE, 0 },
@@ -1318,6 +1322,7 @@ ngx_http_upstream_connect(ngx_http_reque
     ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
 
     u->state->response_time = ngx_current_msec;
+    u->state->connect_time = (ngx_msec_t) -1;
     u->state->header_time = (ngx_msec_t) -1;
 
     rc = ngx_event_connect_peer(&u->peer);
@@ -1760,6 +1765,10 @@ ngx_http_upstream_send_request(ngx_http_
     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
                    "http upstream send request");
 
+    if (u->state->connect_time == (ngx_msec_t) -1) {
+        u->state->connect_time = ngx_current_msec - u->state->response_time;
+    }
+
     if (!u->request_sent && ngx_http_upstream_test_connect(c) != NGX_OK) {
         ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
         return;
@@ -5009,9 +5018,12 @@ ngx_http_upstream_response_time_variable
     for ( ;; ) {
         if (state[i].status) {
 
-            if (data && state[i].header_time != (ngx_msec_t) -1) {
+            if (data == 1 && state[i].header_time != (ngx_msec_t) -1) {
                 ms = state[i].header_time;
 
+            } else if (data == 2 && state[i].connect_time != (ngx_msec_t) -1) {
+                ms = state[i].connect_time;
+
             } else {
                 ms = state[i].response_time;
             }
diff -r 59fc60585f1e -r 74b6ef56ea56 src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h	Sat May 16 01:31:04 2015 +0300
+++ b/src/http/ngx_http_upstream.h	Sat May 16 01:32:27 2015 +0300
@@ -59,6 +59,7 @@ typedef struct {
 
     ngx_uint_t                       status;
     ngx_msec_t                       response_time;
+    ngx_msec_t                       connect_time;
     ngx_msec_t                       header_time;
     off_t                            response_length;
 



More information about the nginx-devel mailing list