3 * DEBUG: section 22 Refresh Calculation
4 * AUTHOR: Harvest Derived
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #ifndef USE_POSIX_REGEX
35 #define USE_POSIX_REGEX /* put before includes; always use POSIX */
39 #include "HttpHdrCc.h"
40 #include "HttpReply.h"
41 #include "HttpRequest.h"
42 #include "MemObject.h"
43 #include "mgr/Registration.h"
44 #include "RefreshPattern.h"
45 #include "SquidConfig.h"
46 #include "SquidTime.h"
71 * This enumerated list assigns specific values, ala HTTP/FTP status
72 * codes. All Fresh codes are in the range 100-199 and all stale
73 * codes are 200-299. We might want to use these codes in logging,
74 * so best to keep them consistent over time.
77 FRESH_REQUEST_MAX_STALE_ALL
= 100,
78 FRESH_REQUEST_MAX_STALE_VALUE
,
82 FRESH_OVERRIDE_EXPIRES
,
83 FRESH_OVERRIDE_LASTMOD
,
84 STALE_MUST_REVALIDATE
= 200,
85 STALE_RELOAD_INTO_IMS
,
87 STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
,
95 static struct RefreshCounts
{
98 int status
[STALE_DEFAULT
+ 1];
101 refreshCounts
[rcCount
];
109 #define REFRESH_DEFAULT_MIN (time_t)0
110 #define REFRESH_DEFAULT_PCT 0.20
111 #define REFRESH_DEFAULT_MAX (time_t)259200
113 static const RefreshPattern
*refreshUncompiledPattern(const char *);
114 static OBJH refreshStats
;
115 static int refreshStaleness(const StoreEntry
* entry
, time_t check_time
, const time_t age
, const RefreshPattern
* R
, stale_flags
* sf
);
117 static RefreshPattern DefaultRefresh
;
119 const RefreshPattern
*
120 refreshLimits(const char *url
)
122 const RefreshPattern
*R
;
124 for (R
= Config
.Refresh
; R
; R
= R
->next
) {
125 if (!regexec(&(R
->compiled_pattern
), url
, 0, 0, 0))
132 static const RefreshPattern
*
133 refreshUncompiledPattern(const char *pat
)
135 const RefreshPattern
*R
;
137 for (R
= Config
.Refresh
; R
; R
= R
->next
) {
138 if (0 == strcmp(R
->pattern
, pat
))
146 * Calculate how stale the response is (or will be at the check_time).
147 * Staleness calculation is based on the following: (1) response
148 * expiration time, (2) age greater than configured maximum, (3)
149 * last-modified factor, and (4) age less than configured minimum.
151 * \retval -1 If the response is fresh.
152 * \retval >0 Otherwise return it's staleness.
153 * \retval 0 NOTE return value of 0 means the response is stale.
155 * The 'stale_flags' structure is used to tell the calling function
156 * _why_ this response is fresh or stale. Its used, for example,
157 * when the admin wants to override expiration and last-modified
161 refreshStaleness(const StoreEntry
* entry
, time_t check_time
, const time_t age
, const RefreshPattern
* R
, stale_flags
* sf
)
164 * Check for an explicit expiration time (Expires: header).
166 if (entry
->expires
> -1) {
169 if (entry
->expires
> check_time
) {
170 debugs(22, 3, "FRESH: expires " << entry
->expires
<<
171 " >= check_time " << check_time
<< " ");
175 debugs(22, 3, "STALE: expires " << entry
->expires
<<
176 " < check_time " << check_time
<< " ");
178 return (check_time
- entry
->expires
);
183 * Use local heuristics to determine staleness. Start with the
184 * max age from the refresh_pattern rule.
187 debugs(22, 3, "STALE: age " << age
<< " > max " << R
->max
<< " ");
189 return (age
- R
->max
);
193 * Try the last-modified factor algorithm: refresh_pattern n% percentage of Last-Modified: age.
195 if (entry
->lastmod
> -1 && entry
->timestamp
> entry
->lastmod
) {
197 * stale_age is the Age of the response when it became/becomes
198 * stale according to the last-modified factor algorithm.
200 time_t stale_age
= static_cast<time_t>((entry
->timestamp
- entry
->lastmod
) * R
->pct
);
203 if (age
>= stale_age
) {
204 debugs(22, 3, "STALE: age " << age
<< " > stale_age " << stale_age
);
205 return (age
- stale_age
);
207 debugs(22, 3, "FRESH: age " << age
<< " <= stale_age " << stale_age
);
213 * Finally, if all else fails; staleness is determined by the refresh_pattern
214 * configured minimum age.
217 debugs(22, 3, "FRESH: age " << age
<< " < min " << R
->min
);
222 debugs(22, 3, "STALE: age " << age
<< " >= min " << R
->min
);
223 return (age
- R
->min
);
227 * \retval 1 if the entry must be revalidated within delta seconds
228 * \retval 0 otherwise
230 * note: request maybe null (e.g. for cache digests build)
233 refreshCheck(const StoreEntry
* entry
, HttpRequest
* request
, time_t delta
)
235 const RefreshPattern
*R
;
236 const char *uri
= NULL
;
238 time_t check_time
= squid_curtime
+ delta
;
243 uri
= entry
->mem_obj
->storeId();
245 uri
= urlCanonical(request
);
247 debugs(22, 3, "refreshCheck: '" << (uri
? uri
: "<none>") << "'");
249 if (check_time
> entry
->timestamp
)
250 age
= check_time
- entry
->timestamp
;
252 // FIXME: what to do when age < 0 or counter overflow?
255 R
= uri
? refreshLimits(uri
) : refreshUncompiledPattern(".");
260 debugs(22, 3, "refreshCheck: Matched '" << R
->pattern
<< " " <<
261 (int) R
->min
<< " " << (int) (100.0 * R
->pct
) << "%% " <<
262 (int) R
->max
<< "'");
264 debugs(22, 3, "\tage:\t" << age
);
266 debugs(22, 3, "\tcheck_time:\t" << mkrfc1123(check_time
));
268 debugs(22, 3, "\tentry->timestamp:\t" << mkrfc1123(entry
->timestamp
));
270 if (request
&& !request
->flags
.ignoreCc
) {
271 const HttpHdrCc
*const cc
= request
->cache_control
;
272 if (cc
&& cc
->hasMinFresh()) {
273 const int32_t minFresh
=cc
->minFresh();
274 debugs(22, 3, "\tage + min-fresh:\t" << age
<< " + " <<
275 minFresh
<< " = " << age
+ minFresh
);
276 debugs(22, 3, "\tcheck_time + min-fresh:\t" << check_time
<< " + "
277 << minFresh
<< " = " <<
278 mkrfc1123(check_time
+ minFresh
));
280 check_time
+= minFresh
;
284 memset(&sf
, '\0', sizeof(sf
));
286 staleness
= refreshStaleness(entry
, check_time
, age
, R
, &sf
);
288 debugs(22, 3, "Staleness = " << staleness
);
290 // stale-if-error requires any failure be passed thru when its period is over.
291 if (request
&& entry
->mem_obj
&& entry
->mem_obj
->getReply() && entry
->mem_obj
->getReply()->cache_control
&&
292 entry
->mem_obj
->getReply()->cache_control
->hasStaleIfError() &&
293 entry
->mem_obj
->getReply()->cache_control
->staleIfError() < staleness
) {
295 debugs(22, 3, "refreshCheck: stale-if-error period expired.");
296 request
->flags
.failOnValidationError
= true;
299 if (EBIT_TEST(entry
->flags
, ENTRY_REVALIDATE
) && staleness
> -1
300 #if USE_HTTP_VIOLATIONS
301 && !R
->flags
.ignore_must_revalidate
304 debugs(22, 3, "refreshCheck: YES: Must revalidate stale response");
306 request
->flags
.failOnValidationError
= true;
307 return STALE_MUST_REVALIDATE
;
310 /* request-specific checks */
311 if (request
&& !request
->flags
.ignoreCc
) {
312 HttpHdrCc
*cc
= request
->cache_control
;
314 if (request
->flags
.ims
&& (R
->flags
.refresh_ims
|| Config
.onoff
.refresh_all_ims
)) {
315 /* The clients no-cache header is changed into a IMS query */
316 debugs(22, 3, "refreshCheck: YES: refresh-ims");
317 return STALE_FORCED_RELOAD
;
320 #if USE_HTTP_VIOLATIONS
322 if (!request
->flags
.noCacheHack()) {
324 } else if (R
->flags
.ignore_reload
) {
325 /* The clients no-cache header is ignored */
326 debugs(22, 3, "refreshCheck: MAYBE: ignore-reload");
327 } else if (R
->flags
.reload_into_ims
|| Config
.onoff
.reload_into_ims
) {
328 /* The clients no-cache header is changed into a IMS query */
329 debugs(22, 3, "refreshCheck: YES: reload-into-ims");
330 return STALE_RELOAD_INTO_IMS
;
332 /* The clients no-cache header is not overridden on this request */
333 debugs(22, 3, "refreshCheck: YES: client reload");
334 request
->flags
.noCache
= true;
335 return STALE_FORCED_RELOAD
;
340 if (cc
->hasMaxAge()) {
341 #if USE_HTTP_VIOLATIONS
342 if (R
->flags
.ignore_reload
&& cc
->maxAge() == 0) {
343 debugs(22, 3, "refreshCheck: MAYBE: client-max-age = 0 and ignore-reload");
347 if (cc
->maxAge() == 0) {
348 debugs(22, 3, "refreshCheck: YES: client-max-age = 0");
349 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
;
352 if (age
> cc
->maxAge()) {
353 debugs(22, 3, "refreshCheck: YES: age > client-max-age");
354 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
;
359 if (cc
->hasMaxStale() && staleness
> -1) {
360 if (cc
->maxStale()==HttpHdrCc::MAX_STALE_ANY
) {
361 /* max-stale directive without a value */
362 debugs(22, 3, "refreshCheck: NO: max-stale wildcard");
363 return FRESH_REQUEST_MAX_STALE_ALL
;
364 } else if (staleness
< cc
->maxStale()) {
365 debugs(22, 3, "refreshCheck: NO: staleness < max-stale");
366 return FRESH_REQUEST_MAX_STALE_VALUE
;
372 if (-1 == staleness
) {
373 debugs(22, 3, "refreshCheck: object isn't stale..");
375 debugs(22, 3, "refreshCheck: returning FRESH_EXPIRES");
376 return FRESH_EXPIRES
;
382 debugs(22, 3, "refreshCheck: returning FRESH_LMFACTOR_RULE");
383 return FRESH_LMFACTOR_RULE
;
388 debugs(22, 3, "refreshCheck: returning FRESH_MIN_RULE");
389 return FRESH_MIN_RULE
;
393 * At this point the response is stale, unless one of
394 * the override options kicks in.
395 * NOTE: max-stale config blocks the overrides.
397 int max_stale
= (R
->max_stale
>= 0 ? R
->max_stale
: Config
.maxStale
);
398 if ( max_stale
>= 0 && staleness
> max_stale
) {
399 debugs(22, 3, "refreshCheck: YES: max-stale limit");
401 request
->flags
.failOnValidationError
= true;
402 return STALE_MAX_STALE
;
406 #if USE_HTTP_VIOLATIONS
408 if (R
->flags
.override_expire
&& age
< R
->min
) {
409 debugs(22, 3, "refreshCheck: NO: age < min && override-expire");
410 return FRESH_OVERRIDE_EXPIRES
;
414 return STALE_EXPIRES
;
418 return STALE_MAX_RULE
;
421 #if USE_HTTP_VIOLATIONS
423 if (R
->flags
.override_lastmod
&& age
< R
->min
) {
424 debugs(22, 3, "refreshCheck: NO: age < min && override-lastmod");
425 return FRESH_OVERRIDE_LASTMOD
;
429 return STALE_LMFACTOR_RULE
;
432 debugs(22, 3, "refreshCheck: returning STALE_DEFAULT");
433 return STALE_DEFAULT
;
437 refreshIsCachable(const StoreEntry
* entry
)
440 * Don't look at the request to avoid no-cache and other nuisances.
441 * the object should have a mem_obj so the URL will be found there.
442 * minimum_expiry_time seconds delta (defaults to 60 seconds), to
443 * avoid objects which expire almost immediately, and which can't
446 int reason
= refreshCheck(entry
, NULL
, Config
.minimum_expiry_time
);
447 ++ refreshCounts
[rcStore
].total
;
448 ++ refreshCounts
[rcStore
].status
[reason
];
450 if (reason
< STALE_MUST_REVALIDATE
)
451 /* Does not need refresh. This is certainly cachable */
454 if (entry
->lastmod
< 0)
455 /* Last modified is needed to do a refresh */
458 if (entry
->mem_obj
== NULL
)
462 if (entry
->getReply() == NULL
)
466 if (entry
->getReply()->content_length
== 0)
467 /* No use refreshing (caching?) 0 byte objects */
470 /* This seems to be refreshable. Cache it */
474 /// whether reply is stale if it is a hit
476 refreshIsStaleIfHit(const int reason
)
480 case FRESH_LMFACTOR_RULE
:
488 /* refreshCheck... functions below are protocol-specific wrappers around
489 * refreshCheck() function above */
492 refreshCheckHTTP(const StoreEntry
* entry
, HttpRequest
* request
)
494 int reason
= refreshCheck(entry
, request
, 0);
495 ++ refreshCounts
[rcHTTP
].total
;
496 ++ refreshCounts
[rcHTTP
].status
[reason
];
497 request
->flags
.staleIfHit
= refreshIsStaleIfHit(reason
);
498 return (Config
.onoff
.offline
|| reason
< 200) ? 0 : 1;
502 refreshCheckICP(const StoreEntry
* entry
, HttpRequest
* request
)
504 int reason
= refreshCheck(entry
, request
, 30);
505 ++ refreshCounts
[rcICP
].total
;
506 ++ refreshCounts
[rcICP
].status
[reason
];
507 return (reason
< 200) ? 0 : 1;
512 refreshCheckHTCP(const StoreEntry
* entry
, HttpRequest
* request
)
514 int reason
= refreshCheck(entry
, request
, 10);
515 ++ refreshCounts
[rcHTCP
].total
;
516 ++ refreshCounts
[rcHTCP
].status
[reason
];
517 return (reason
< 200) ? 0 : 1;
522 #if USE_CACHE_DIGESTS
524 refreshCheckDigest(const StoreEntry
* entry
, time_t delta
)
526 int reason
= refreshCheck(entry
,
527 entry
->mem_obj
? entry
->mem_obj
->request
: NULL
,
529 ++ refreshCounts
[rcCDigest
].total
;
530 ++ refreshCounts
[rcCDigest
].status
[reason
];
531 return (reason
< 200) ? 0 : 1;
537 getMaxAge(const char *url
)
539 const RefreshPattern
*R
;
540 debugs(22, 3, "getMaxAge: '" << url
<< "'");
542 if ((R
= refreshLimits(url
)))
545 return REFRESH_DEFAULT_MAX
;
550 refreshCountsStats(StoreEntry
* sentry
, struct RefreshCounts
*rc
)
555 storeAppendPrintf(sentry
, "\n\n%s histogram:\n", rc
->proto
);
556 storeAppendPrintf(sentry
, "Count\t%%Total\tCategory\n");
558 #define refreshCountsStatsEntry(code,desc) { \
559 storeAppendPrintf(sentry, "%6d\t%6.2f\t%s\n", \
560 rc->status[code], xpercent(rc->status[code], tot), desc); \
561 sum += rc->status[code]; \
564 refreshCountsStatsEntry(FRESH_REQUEST_MAX_STALE_ALL
,
565 "Fresh: request max-stale wildcard");
566 refreshCountsStatsEntry(FRESH_REQUEST_MAX_STALE_VALUE
,
567 "Fresh: request max-stale value");
568 refreshCountsStatsEntry(FRESH_EXPIRES
,
569 "Fresh: expires time not reached");
570 refreshCountsStatsEntry(FRESH_LMFACTOR_RULE
,
571 "Fresh: refresh_pattern last-mod factor percentage");
572 refreshCountsStatsEntry(FRESH_MIN_RULE
,
573 "Fresh: refresh_pattern min value");
574 refreshCountsStatsEntry(FRESH_OVERRIDE_EXPIRES
,
575 "Fresh: refresh_pattern override expires");
576 refreshCountsStatsEntry(FRESH_OVERRIDE_LASTMOD
,
577 "Fresh: refresh_pattern override lastmod");
578 refreshCountsStatsEntry(STALE_MUST_REVALIDATE
,
579 "Stale: response has must-revalidate");
580 refreshCountsStatsEntry(STALE_RELOAD_INTO_IMS
,
581 "Stale: changed reload into IMS");
582 refreshCountsStatsEntry(STALE_FORCED_RELOAD
,
583 "Stale: request has no-cache directive");
584 refreshCountsStatsEntry(STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
,
585 "Stale: age exceeds request max-age value");
586 refreshCountsStatsEntry(STALE_EXPIRES
,
587 "Stale: expires time reached");
588 refreshCountsStatsEntry(STALE_MAX_RULE
,
589 "Stale: refresh_pattern max age rule");
590 refreshCountsStatsEntry(STALE_LMFACTOR_RULE
,
591 "Stale: refresh_pattern last-mod factor percentage");
592 refreshCountsStatsEntry(STALE_DEFAULT
,
593 "Stale: by default");
595 tot
= sum
; /* paranoid: "total" line shows 100% if we forgot nothing */
596 storeAppendPrintf(sentry
, "%6d\t%6.2f\tTOTAL\n",
597 rc
->total
, xpercent(rc
->total
, tot
));
599 storeAppendPrintf(sentry
, "\n");
603 refreshStats(StoreEntry
* sentry
)
608 /* get total usage count */
610 for (i
= 0; i
< rcCount
; ++i
)
611 total
+= refreshCounts
[i
].total
;
613 /* protocol usage histogram */
614 storeAppendPrintf(sentry
, "\nRefreshCheck calls per protocol\n\n");
616 storeAppendPrintf(sentry
, "Protocol\t#Calls\t%%Calls\n");
618 for (i
= 0; i
< rcCount
; ++i
)
619 storeAppendPrintf(sentry
, "%10s\t%6d\t%6.2f\n",
620 refreshCounts
[i
].proto
,
621 refreshCounts
[i
].total
,
622 xpercent(refreshCounts
[i
].total
, total
));
624 /* per protocol histograms */
625 storeAppendPrintf(sentry
, "\n\nRefreshCheck histograms for various protocols\n");
627 for (i
= 0; i
< rcCount
; ++i
)
628 refreshCountsStats(sentry
, &refreshCounts
[i
]);
632 refreshRegisterWithCacheManager(void)
634 Mgr::RegisterAction("refresh", "Refresh Algorithm Statistics", refreshStats
, 0, 1);
640 memset(refreshCounts
, 0, sizeof(refreshCounts
));
641 refreshCounts
[rcHTTP
].proto
= "HTTP";
642 refreshCounts
[rcICP
].proto
= "ICP";
645 refreshCounts
[rcHTCP
].proto
= "HTCP";
648 refreshCounts
[rcStore
].proto
= "On Store";
649 #if USE_CACHE_DIGESTS
651 refreshCounts
[rcCDigest
].proto
= "Cache Digests";
654 memset(&DefaultRefresh
, '\0', sizeof(DefaultRefresh
));
655 DefaultRefresh
.pattern
= "<none>";
656 DefaultRefresh
.min
= REFRESH_DEFAULT_MIN
;
657 DefaultRefresh
.pct
= REFRESH_DEFAULT_PCT
;
658 DefaultRefresh
.max
= REFRESH_DEFAULT_MAX
;
660 refreshRegisterWithCacheManager();