]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cache: Register the cache as a data filter only if response is cacheable
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 6 Dec 2018 20:59:39 +0000 (21:59 +0100)
committerWilly Tarreau <w@1wt.eu>
Tue, 11 Dec 2018 16:09:31 +0000 (17:09 +0100)
Instead of calling register_data_filter() when the stream analyze starts, we now
call it when we are sure the response is cacheable. It is done in the
http_headers callback, just before the body analyzis, and only if the headers
was already been cached. And during the body analyzis, if an error occurred or
if the response is too big, we unregistered the cache immediatly.

This patch may be backported in 1.8. It is not a bug but a significant
improvement.

src/cache.c

index e0905eff65216b1a649d931338ec68534358714d..437a9086c20299d4ef4cd91a5db229c0092052f2 100644 (file)
@@ -144,8 +144,6 @@ cache_store_chn_start_analyze(struct stream *s, struct filter *filter, struct ch
                filter->ctx     = st;
        }
 
-       register_data_filter(s, chn, filter);
-
        return 1;
 }
 
@@ -186,8 +184,10 @@ cache_store_http_headers(struct stream *s, struct filter *filter, struct http_ms
        if (!(msg->chn->flags & CF_ISRESP) || !st)
                return 1;
 
-       st->hdrs_len = msg->sov;
-
+       if (st->first_block) {
+               register_data_filter(s, msg->chn, filter);
+               st->hdrs_len = msg->sov;
+       }
        return 1;
 }
 
@@ -219,8 +219,11 @@ cache_store_http_forward_data(struct stream *s, struct filter *filter,
         * We need to skip the HTTP headers first, because we saved them in the
         * http-response action.
         */
-       if (!(msg->chn->flags & CF_ISRESP) || !st)
+       if (!(msg->chn->flags & CF_ISRESP) || !st) {
+               /* should never happen */
+               unregister_data_filter(s, msg->chn, filter);
                return len;
+       }
 
        if (!len) {
                /* Nothing to forward */
@@ -233,7 +236,7 @@ cache_store_http_forward_data(struct stream *s, struct filter *filter,
        }
        else {
                /* Forward data */
-               if (filter->ctx && st->first_block) {
+               if (st->first_block) {
                        int to_append, append;
                        struct shared_block *fb;
 
@@ -244,6 +247,7 @@ cache_store_http_forward_data(struct stream *s, struct filter *filter,
                        if (!fb) {
                                shctx_unlock(shctx);
                                disable_cache_entry(st, filter, shctx);
+                               unregister_data_filter(s, msg->chn, filter);
                                return len;
                        }
                        shctx_unlock(shctx);
@@ -256,11 +260,16 @@ cache_store_http_forward_data(struct stream *s, struct filter *filter,
                        /* Rewind the buffer to forward all data */
                        c_rew(msg->chn, st->hdrs_len);
                        st->hdrs_len = 0;
-                       if (ret < 0)
+                       if (ret < 0) {
                                disable_cache_entry(st, filter, shctx);
+                               unregister_data_filter(s, msg->chn, filter);
+                       }
                }
-               else
+               else {
+                       /* should never happen */
+                       unregister_data_filter(s, msg->chn, filter);
                        ret = len;
+               }
        }
 
        if ((ret != len) ||