changeset 6147:74b6ef56ea56

Upstream: $upstream_connect_time. The variable keeps time spent on establishing a connection with the upstream server.
author Ruslan Ermilov <ru@nginx.com>
date Sat, 16 May 2015 01:32:27 +0300
parents 59fc60585f1e
children bf8b6534db3a
files src/http/ngx_http_upstream.c src/http/ngx_http_upstream.h
diffstat 2 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -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;
             }
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -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;