From: Christopher Faulet Date: Thu, 6 Dec 2018 20:59:39 +0000 (+0100) Subject: MINOR: cache: Register the cache as a data filter only if response is cacheable X-Git-Tag: v1.9-dev11~105 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=67658c9c9aa3f8e3b5c383da00018cd5220cb967;p=thirdparty%2Fhaproxy.git MINOR: cache: Register the cache as a data filter only if response is cacheable 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. --- diff --git a/src/cache.c b/src/cache.c index e0905eff65..437a9086c2 100644 --- a/src/cache.c +++ b/src/cache.c @@ -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) ||