comparison src/http/ngx_http_upstream.c @ 1168:4d0d12446c3b

store the upstream states before X-Accel-Redirect to a next upstream
author Igor Sysoev <igor@sysoev.ru>
date Wed, 18 Apr 2007 20:23:19 +0000
parents bde5e4134759
children ea5ecb1aae88
comparison
equal deleted inserted replaced
1167:b82cfb1c94ac 1168:4d0d12446c3b
326 u->output.output_filter = ngx_chain_writer; 326 u->output.output_filter = ngx_chain_writer;
327 u->output.filter_ctx = &u->writer; 327 u->output.filter_ctx = &u->writer;
328 328
329 u->writer.pool = r->pool; 329 u->writer.pool = r->pool;
330 330
331 if (ngx_array_init(&u->states, r->pool, 1, 331 if (r->upstream_states == NULL) {
332 sizeof(ngx_http_upstream_state_t)) 332
333 != NGX_OK) 333 r->upstream_states = ngx_array_create(r->pool, 1,
334 { 334 sizeof(ngx_http_upstream_state_t));
335 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 335 if (r->upstream_states == NULL) {
336 return; 336 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
337 return;
338 }
339
340 } else {
341
342 u->state = ngx_array_push(r->upstream_states);
343 if (u->state == NULL) {
344 ngx_http_upstream_finalize_request(r, u,
345 NGX_HTTP_INTERNAL_SERVER_ERROR);
346 return;
347 }
348
349 ngx_memzero(u->state, sizeof(ngx_http_upstream_state_t));
337 } 350 }
338 351
339 cln = ngx_http_cleanup_add(r, 0); 352 cln = ngx_http_cleanup_add(r, 0);
340 if (cln == NULL) { 353 if (cln == NULL) {
341 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR); 354 ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
507 tp = ngx_timeofday(); 520 tp = ngx_timeofday();
508 u->state->response_sec = tp->sec - u->state->response_sec; 521 u->state->response_sec = tp->sec - u->state->response_sec;
509 u->state->response_msec = tp->msec - u->state->response_msec; 522 u->state->response_msec = tp->msec - u->state->response_msec;
510 } 523 }
511 524
512 u->state = ngx_array_push(&u->states); 525 u->state = ngx_array_push(r->upstream_states);
513 if (u->state == NULL) { 526 if (u->state == NULL) {
514 ngx_http_upstream_finalize_request(r, u, 527 ngx_http_upstream_finalize_request(r, u,
515 NGX_HTTP_INTERNAL_SERVER_ERROR); 528 NGX_HTTP_INTERNAL_SERVER_ERROR);
516 return; 529 return;
517 } 530 }
2500 ngx_http_variable_value_t *v, uintptr_t data) 2513 ngx_http_variable_value_t *v, uintptr_t data)
2501 { 2514 {
2502 u_char *p; 2515 u_char *p;
2503 size_t len; 2516 size_t len;
2504 ngx_uint_t i; 2517 ngx_uint_t i;
2505 ngx_http_upstream_t *u;
2506 ngx_http_upstream_state_t *state; 2518 ngx_http_upstream_state_t *state;
2507 2519
2508 v->valid = 1; 2520 v->valid = 1;
2509 v->no_cachable = 0; 2521 v->no_cachable = 0;
2510 v->not_found = 0; 2522 v->not_found = 0;
2511 2523
2512 u = r->upstream; 2524 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2513
2514 if (u == NULL || u->states.nelts == 0) {
2515 v->not_found = 1; 2525 v->not_found = 1;
2516 return NGX_OK; 2526 return NGX_OK;
2517 } 2527 }
2518 2528
2519 len = u->states.nelts * (3 + 2); 2529 len = r->upstream_states->nelts * (3 + 2);
2520 2530
2521 p = ngx_palloc(r->pool, len); 2531 p = ngx_palloc(r->pool, len);
2522 if (p == NULL) { 2532 if (p == NULL) {
2523 return NGX_ERROR; 2533 return NGX_ERROR;
2524 } 2534 }
2525 2535
2526 v->data = p; 2536 v->data = p;
2527 2537
2528 i = 0; 2538 i = 0;
2529 state = u->states.elts; 2539 state = r->upstream_states->elts;
2530 2540
2531 for ( ;; ) { 2541 for ( ;; ) {
2532 if (state[i].status == 0) { 2542 if (state[i].status) {
2543 p = ngx_sprintf(p, "%ui", state[i].status);
2544
2545 } else {
2533 *p++ = '-'; 2546 *p++ = '-';
2547 }
2548
2549 if (++i == r->upstream_states->nelts) {
2550 break;
2551 }
2552
2553 if (state[i].peer) {
2554 *p++ = ',';
2555 *p++ = ' ';
2534 2556
2535 } else { 2557 } else {
2536 p = ngx_sprintf(p, "%ui", state[i].status); 2558 *p++ = ' ';
2537 } 2559 *p++ = ':';
2538 2560 *p++ = ' ';
2539 if (++i == u->states.nelts) { 2561
2540 break; 2562 if (++i == r->upstream_states->nelts) {
2541 } 2563 break;
2542 2564 }
2543 *p++ = ','; 2565
2544 *p++ = ' '; 2566 continue;
2567 }
2545 } 2568 }
2546 2569
2547 v->len = p - v->data; 2570 v->len = p - v->data;
2548 2571
2549 return NGX_OK; 2572 return NGX_OK;
2556 { 2579 {
2557 u_char *p; 2580 u_char *p;
2558 size_t len; 2581 size_t len;
2559 ngx_uint_t i; 2582 ngx_uint_t i;
2560 ngx_msec_int_t ms; 2583 ngx_msec_int_t ms;
2561 ngx_http_upstream_t *u;
2562 ngx_http_upstream_state_t *state; 2584 ngx_http_upstream_state_t *state;
2563 2585
2564 v->valid = 1; 2586 v->valid = 1;
2565 v->no_cachable = 0; 2587 v->no_cachable = 0;
2566 v->not_found = 0; 2588 v->not_found = 0;
2567 2589
2568 u = r->upstream; 2590 if (r->upstream_states == NULL || r->upstream_states->nelts == 0) {
2569
2570 if (u == NULL || u->states.nelts == 0) {
2571 v->not_found = 1; 2591 v->not_found = 1;
2572 return NGX_OK; 2592 return NGX_OK;
2573 } 2593 }
2574 2594
2575 len = u->states.nelts * (NGX_TIME_T_LEN + 4 + 2); 2595 len = r->upstream_states->nelts * (NGX_TIME_T_LEN + 4 + 2);
2576 2596
2577 p = ngx_palloc(r->pool, len); 2597 p = ngx_palloc(r->pool, len);
2578 if (p == NULL) { 2598 if (p == NULL) {
2579 return NGX_ERROR; 2599 return NGX_ERROR;
2580 } 2600 }
2581 2601
2582 v->data = p; 2602 v->data = p;
2583 2603
2584 i = 0; 2604 i = 0;
2585 state = u->states.elts; 2605 state = r->upstream_states->elts;
2586 2606
2587 for ( ;; ) { 2607 for ( ;; ) {
2588 if (state[i].status == 0) { 2608 if (state[i].status) {
2589 *p++ = '-';
2590
2591 } else {
2592 ms = state[i].response_sec * 1000 + state[i].response_msec; 2609 ms = state[i].response_sec * 1000 + state[i].response_msec;
2593 ms = (ms >= 0) ? ms : 0; 2610 ms = (ms >= 0) ? ms : 0;
2594 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000); 2611 p = ngx_sprintf(p, "%d.%03d", ms / 1000, ms % 1000);
2595 } 2612
2596 2613 } else {
2597 if (++i == u->states.nelts) { 2614 *p++ = '-';
2615 }
2616
2617 if (++i == r->upstream_states->nelts) {
2598 break; 2618 break;
2599 } 2619 }
2600 2620
2601 *p++ = ','; 2621 if (state[i].peer) {
2602 *p++ = ' '; 2622 *p++ = ',';
2623 *p++ = ' ';
2624
2625 } else {
2626 *p++ = ' ';
2627 *p++ = ':';
2628 *p++ = ' ';
2629
2630 if (++i == r->upstream_states->nelts) {
2631 break;
2632 }
2633
2634 continue;
2635 }
2603 } 2636 }
2604 2637
2605 v->len = p - v->data; 2638 v->len = p - v->data;
2606 2639
2607 return NGX_OK; 2640 return NGX_OK;