Forum: NGINX Move calculation $upstream_response_time before send header to client.

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ca88eb9481b38f1e4e202bdf34efb676?d=identicon&s=25 unknown (Guest)
on 2009-04-21 20:08
(Received via mailing list)
From: Kirill A. Korinskiy <catap@catap.ru>

Now calculation $upstream_response_time happen after send header to
client and we have a 0 ms response to not error (last) upstream.
---
 src/http/ngx_http_upstream.c |   23 +++++++++++------------
 1 files changed, 11 insertions(+), 12 deletions(-)

diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
index 7ae15cb..0771388 100644
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -1843,10 +1843,21 @@
ngx_http_upstream_send_response(ngx_http_request_t *r,
ngx_http_upstream_t *u)
     int                        tcp_nodelay;
     ssize_t                    n;
     ngx_int_t                  rc;
+    ngx_time_t                *tp;
     ngx_event_pipe_t          *p;
     ngx_connection_t          *c;
     ngx_http_core_loc_conf_t  *clcf;

+    if (u->state && u->state->response_sec) {
+        tp = ngx_timeofday();
+        u->state->response_sec = tp->sec - u->state->response_sec;
+        u->state->response_msec = tp->msec - u->state->response_msec;
+
+        if (u->pipe) {
+            u->state->response_length = u->pipe->read_length;
+        }
+    }
+
     rc = ngx_http_send_header(r);

     if (rc == NGX_ERROR || rc > NGX_OK || r->post_action) {
@@ -2735,8 +2746,6 @@ static void
 ngx_http_upstream_finalize_request(ngx_http_request_t *r,
     ngx_http_upstream_t *u, ngx_int_t rc)
 {
-    ngx_time_t  *tp;
-
     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                    "finalize http upstream request: %i", rc);

@@ -2744,16 +2753,6 @@
ngx_http_upstream_finalize_request(ngx_http_request_t *r,
         *u->cleanup = NULL;
     }

-    if (u->state && u->state->response_sec) {
-        tp = ngx_timeofday();
-        u->state->response_sec = tp->sec - u->state->response_sec;
-        u->state->response_msec = tp->msec - u->state->response_msec;
-
-        if (u->pipe) {
-            u->state->response_length = u->pipe->read_length;
-        }
-    }
-
     u->finalize_request(r, rc);

     if (u->peer.free) {
A8108a0961c6087c43cda32c8616dcba?d=identicon&s=25 Maxim Dounin (Guest)
on 2009-04-22 10:36
(Received via mailing list)
Hello!

On Tue, Apr 21, 2009 at 09:59:21PM +0400, catap+nginx@catap.ru wrote:

> From: Kirill A. Korinskiy <catap@catap.ru>
>
> Now calculation $upstream_response_time happen after send header to
> client and we have a 0 ms response to not error (last) upstream.

The patch itself is clearly wrong.  But could you please repeat
once again (in russian if you prefer to) what problem you are
trying to solve?

From our IRC discussion yesterday it looks like you are trying to
get $upstream_response_time in reply header.  No, this not work as
$upstream_response_time is time taken for the whole upstream
request processing (not only headers), and it's usually known only
after sending response headers.

Probably we may introduce something like $upstream_header_time
with the meaning you assumed, but I'm not really sure we need to.

Maxim Dounin
This topic is locked and can not be replied to.