diff src/http/ngx_http_upstream.c @ 6906:1aeaae6e9446

Upstream: proxy_cache_background_update and friends. The directives enable cache updates in subrequests.
author Roman Arutyunyan <arut@nginx.com>
date Fri, 10 Feb 2017 15:13:41 +0300
parents 9a9e13686869
children 5850fed24639
line wrap: on
line diff
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -17,6 +17,8 @@ static ngx_int_t ngx_http_upstream_cache
     ngx_http_upstream_t *u, ngx_http_file_cache_t **cache);
 static ngx_int_t ngx_http_upstream_cache_send(ngx_http_request_t *r,
     ngx_http_upstream_t *u);
+static ngx_int_t ngx_http_upstream_cache_background_update(
+    ngx_http_request_t *r, ngx_http_upstream_t *u);
 static ngx_int_t ngx_http_upstream_cache_check_range(ngx_http_request_t *r,
     ngx_http_upstream_t *u);
 static ngx_int_t ngx_http_upstream_cache_status(ngx_http_request_t *r,
@@ -578,6 +580,10 @@ ngx_http_upstream_init_request(ngx_http_
                 rc = NGX_DECLINED;
                 r->cached = 0;
             }
+
+            if (ngx_http_upstream_cache_background_update(r, u) != NGX_OK) {
+                rc = NGX_ERROR;
+            }
         }
 
         if (rc != NGX_DECLINED) {
@@ -869,10 +875,23 @@ ngx_http_upstream_cache(ngx_http_request
 
     switch (rc) {
 
+    case NGX_HTTP_CACHE_STALE:
+
+        if (((u->conf->cache_use_stale & NGX_HTTP_UPSTREAM_FT_UPDATING)
+             || c->stale_updating) && !r->cache_updater
+            && u->conf->cache_background_update)
+        {
+            r->cache->background = 1;
+            u->cache_status = rc;
+            rc = NGX_OK;
+        }
+
+        break;
+
     case NGX_HTTP_CACHE_UPDATING:
 
-        if ((u->conf->cache_use_stale & NGX_HTTP_UPSTREAM_FT_UPDATING)
-            || c->stale_updating)
+        if (((u->conf->cache_use_stale & NGX_HTTP_UPSTREAM_FT_UPDATING)
+             || c->stale_updating) && !r->cache_updater)
         {
             u->cache_status = rc;
             rc = NGX_OK;
@@ -1046,6 +1065,30 @@ ngx_http_upstream_cache_send(ngx_http_re
 
 
 static ngx_int_t
+ngx_http_upstream_cache_background_update(ngx_http_request_t *r,
+    ngx_http_upstream_t *u)
+{
+    ngx_http_request_t  *sr;
+
+    if (!r->cached || !r->cache->background) {
+        return NGX_OK;
+    }
+
+    if (ngx_http_subrequest(r, &r->uri, &r->args, &sr, NULL,
+                            NGX_HTTP_SUBREQUEST_CLONE)
+        != NGX_OK)
+    {
+        return NGX_ERROR;
+    }
+
+    sr->header_only = 1;
+    sr->cache_updater = 1;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_upstream_cache_check_range(ngx_http_request_t *r,
     ngx_http_upstream_t *u)
 {