comparison src/http/ngx_http_upstream.c @ 6654:c131f20c9562

Upstream: the $upstream_bytes_received variable. Unlike $upstream_response_length that only counts the body size, the new variable also counts the size of response header and data received after switching protocols when proxying WebSockets.
author Vladimir Homutov <vl@nginx.com>
date Wed, 10 Aug 2016 16:46:39 +0300
parents 3c87b82b17d4
children 9cf2dce316e5
comparison
equal deleted inserted replaced
6653:7a6456398fc3 6654:c131f20c9562
389 389
390 { ngx_string("upstream_response_length"), NULL, 390 { ngx_string("upstream_response_length"), NULL,
391 ngx_http_upstream_response_length_variable, 0, 391 ngx_http_upstream_response_length_variable, 0,
392 NGX_HTTP_VAR_NOCACHEABLE, 0 }, 392 NGX_HTTP_VAR_NOCACHEABLE, 0 },
393 393
394 { ngx_string("upstream_bytes_received"), NULL,
395 ngx_http_upstream_response_length_variable, 1,
396 NGX_HTTP_VAR_NOCACHEABLE, 0 },
397
394 #if (NGX_HTTP_CACHE) 398 #if (NGX_HTTP_CACHE)
395 399
396 { ngx_string("upstream_cache_status"), NULL, 400 { ngx_string("upstream_cache_status"), NULL,
397 ngx_http_upstream_cache_status, 0, 401 ngx_http_upstream_cache_status, 0,
398 NGX_HTTP_VAR_NOCACHEABLE, 0 }, 402 NGX_HTTP_VAR_NOCACHEABLE, 0 },
2134 if (n == NGX_ERROR || n == 0) { 2138 if (n == NGX_ERROR || n == 0) {
2135 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR); 2139 ngx_http_upstream_next(r, u, NGX_HTTP_UPSTREAM_FT_ERROR);
2136 return; 2140 return;
2137 } 2141 }
2138 2142
2143 u->state->bytes_received += n;
2144
2139 u->buffer.last += n; 2145 u->buffer.last += n;
2140 2146
2141 #if 0 2147 #if 0
2142 u->valid_header_in = 0; 2148 u->valid_header_in = 0;
2143 2149
2640 if (n == 0 || n == NGX_ERROR) { 2646 if (n == 0 || n == NGX_ERROR) {
2641 ngx_http_upstream_finalize_request(r, u, n); 2647 ngx_http_upstream_finalize_request(r, u, n);
2642 return; 2648 return;
2643 } 2649 }
2644 2650
2651 u->state->bytes_received += n;
2645 u->state->response_length += n; 2652 u->state->response_length += n;
2646 2653
2647 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { 2654 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
2648 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); 2655 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
2649 return; 2656 return;
3213 3220
3214 if (n > 0) { 3221 if (n > 0) {
3215 do_write = 1; 3222 do_write = 1;
3216 b->last += n; 3223 b->last += n;
3217 3224
3225 if (from_upstream) {
3226 u->state->bytes_received += n;
3227 }
3228
3218 continue; 3229 continue;
3219 } 3230 }
3220 3231
3221 if (n == NGX_ERROR) { 3232 if (n == NGX_ERROR) {
3222 src->read->eof = 1; 3233 src->read->eof = 1;
3409 if (n == NGX_AGAIN) { 3420 if (n == NGX_AGAIN) {
3410 break; 3421 break;
3411 } 3422 }
3412 3423
3413 if (n > 0) { 3424 if (n > 0) {
3425 u->state->bytes_received += n;
3414 u->state->response_length += n; 3426 u->state->response_length += n;
3415 3427
3416 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) { 3428 if (u->input_filter(u->input_filter_ctx, n) == NGX_ERROR) {
3417 ngx_http_upstream_finalize_request(r, u, NGX_ERROR); 3429 ngx_http_upstream_finalize_request(r, u, NGX_ERROR);
3418 return; 3430 return;
4093 4105
4094 if (u->state && u->state->response_time) { 4106 if (u->state && u->state->response_time) {
4095 u->state->response_time = ngx_current_msec - u->state->response_time; 4107 u->state->response_time = ngx_current_msec - u->state->response_time;
4096 4108
4097 if (u->pipe && u->pipe->read_length) { 4109 if (u->pipe && u->pipe->read_length) {
4110 u->state->bytes_received += u->pipe->read_length
4111 - u->pipe->preread_size;
4098 u->state->response_length = u->pipe->read_length; 4112 u->state->response_length = u->pipe->read_length;
4099 } 4113 }
4100 } 4114 }
4101 4115
4102 u->finalize_request(r, rc); 4116 u->finalize_request(r, rc);
5240 5254
5241 i = 0; 5255 i = 0;
5242 state = r->upstream_states->elts; 5256 state = r->upstream_states->elts;
5243 5257
5244 for ( ;; ) { 5258 for ( ;; ) {
5245 p = ngx_sprintf(p, "%O", state[i].response_length); 5259
5260 if (data == 1) {
5261 p = ngx_sprintf(p, "%O", state[i].bytes_received);
5262
5263 } else {
5264 p = ngx_sprintf(p, "%O", state[i].response_length);
5265 }
5246 5266
5247 if (++i == r->upstream_states->nelts) { 5267 if (++i == r->upstream_states->nelts) {
5248 break; 5268 break;
5249 } 5269 }
5250 5270