[PATCH] Upstream: add $upstream_cache_age variable

Roman Arutyunyan arut at nginx.com
Mon Oct 6 10:10:18 UTC 2014


On 03 Oct 2014, at 21:24, Piotr Sikora <piotr at cloudflare.com> wrote:

> # HG changeset patch
> # User Piotr Sikora <piotr at cloudflare.com>
> # Date 1412356980 25200
> #      Fri Oct 03 10:23:00 2014 -0700
> # Node ID f343adae412afce435a18384b0aad052405a393b
> # Parent  6bbad2e732458bf53771e80c63a654b3d7f61963
> Upstream: add $upstream_cache_age variable.
> 
> This variable represents the amount of time that passed since the response
> was fetched (or successfully revalidated) from the upstream server.
> 
> Signed-off-by: Piotr Sikora <piotr at cloudflare.com>
> 
> diff -r 6bbad2e73245 -r f343adae412a src/http/ngx_http_upstream.c
> --- a/src/http/ngx_http_upstream.c	Wed Aug 27 20:51:01 2014 +0400
> +++ b/src/http/ngx_http_upstream.c	Fri Oct 03 10:23:00 2014 -0700
> @@ -21,6 +21,8 @@ static ngx_int_t ngx_http_upstream_cache
>     ngx_http_variable_value_t *v, uintptr_t data);
> static ngx_int_t ngx_http_upstream_cache_etag(ngx_http_request_t *r,
>     ngx_http_variable_value_t *v, uintptr_t data);
> +static ngx_int_t ngx_http_upstream_cache_age(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data);
> #endif
> 
> static void ngx_http_upstream_init_request(ngx_http_request_t *r);
> @@ -373,6 +375,10 @@ static ngx_http_variable_t  ngx_http_ups
>       ngx_http_upstream_cache_etag, 0,
>       NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
> 
> +    { ngx_string("upstream_cache_age"), NULL,
> +      ngx_http_upstream_cache_age, 0,
> +      NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
> +
> #endif
> 
>     { ngx_null_string, NULL, NULL, 0, 0, 0 }
> @@ -4851,6 +4857,37 @@ ngx_http_upstream_cache_etag(ngx_http_re
>     return NGX_OK;
> }
> 
> +
> +static ngx_int_t
> +ngx_http_upstream_cache_age(ngx_http_request_t *r,
> +    ngx_http_variable_value_t *v, uintptr_t data)
> +{
> +    if (r->upstream == NULL) {
> +        v->not_found = 1;
> +        return NGX_OK;
> +    }
> +
> +    if (r->upstream->cache_status != NGX_HTTP_CACHE_STALE
> +        && r->upstream->cache_status != NGX_HTTP_CACHE_UPDATING
> +        && r->upstream->cache_status != NGX_HTTP_CACHE_HIT)
> +    {
> +        v->not_found = 1;
> +        return NGX_OK;
> +    }
> +
> +    v->data = ngx_pnalloc(r->pool, NGX_TIME_T_LEN);
> +    if (v->data == NULL) {
> +        return NGX_ERROR;
> +    }
> +
> +    v->len = ngx_sprintf(v->data, "%T", ngx_time() - r->cache->date) - v->data;
> +    v->valid = 1;
> +    v->no_cacheable = 0;
> +    v->not_found = 0;
> +
> +    return NGX_OK;
> +}
> +
> #endif
> 
> 
> 
> _______________________________________________
> nginx-devel mailing list
> nginx-devel at nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx-devel
> 

Another issue i’ve found reading the RFC.

If the reason for $upstream_cache_age is adding HTTP Age response header then it makes
sense to improve age calculation algorithm.

According to RFC 7234 / 4.2.3. "Calculating Age” the local cache age should be added
to the Age value received from backend (“age_value").

response_delay = response_time - request_time;
corrected_age_value = age_value + response_delay;



More information about the nginx-devel mailing list