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 "mgr/Registration.h"
40 #include "HttpHdrCc.h"
41 #include "HttpRequest.h"
42 #include "HttpReply.h"
43 #include "MemObject.h"
44 #include "SquidTime.h"
45 #include "SquidConfig.h"
70 * This enumerated list assigns specific values, ala HTTP/FTP status
71 * codes. All Fresh codes are in the range 100-199 and all stale
72 * codes are 200-299. We might want to use these codes in logging,
73 * so best to keep them consistent over time.
76 FRESH_REQUEST_MAX_STALE_ALL
= 100,
77 FRESH_REQUEST_MAX_STALE_VALUE
,
81 FRESH_OVERRIDE_EXPIRES
,
82 FRESH_OVERRIDE_LASTMOD
,
83 STALE_MUST_REVALIDATE
= 200,
84 STALE_RELOAD_INTO_IMS
,
86 STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
,
94 static struct RefreshCounts
{
97 int status
[STALE_DEFAULT
+ 1];
100 refreshCounts
[rcCount
];
108 #define REFRESH_DEFAULT_MIN (time_t)0
109 #define REFRESH_DEFAULT_PCT 0.20
110 #define REFRESH_DEFAULT_MAX (time_t)259200
112 static const refresh_t
*refreshUncompiledPattern(const char *);
113 static OBJH refreshStats
;
114 static int refreshStaleness(const StoreEntry
* entry
, time_t check_time
, const time_t age
, const refresh_t
* R
, stale_flags
* sf
);
116 static refresh_t DefaultRefresh
;
119 refreshLimits(const char *url
)
123 for (R
= Config
.Refresh
; R
; R
= R
->next
) {
124 if (!regexec(&(R
->compiled_pattern
), url
, 0, 0, 0))
131 static const refresh_t
*
132 refreshUncompiledPattern(const char *pat
)
136 for (R
= Config
.Refresh
; R
; R
= R
->next
) {
137 if (0 == strcmp(R
->pattern
, pat
))
145 * Calculate how stale the response is (or will be at the check_time).
146 * Staleness calculation is based on the following: (1) response
147 * expiration time, (2) age greater than configured maximum, (3)
148 * last-modified factor, and (4) age less than configured minimum.
150 * \retval -1 If the response is fresh.
151 * \retval >0 Otherwise return it's staleness.
152 * \retval 0 NOTE return value of 0 means the response is stale.
154 * The 'stale_flags' structure is used to tell the calling function
155 * _why_ this response is fresh or stale. Its used, for example,
156 * when the admin wants to override expiration and last-modified
160 refreshStaleness(const StoreEntry
* entry
, time_t check_time
, const time_t age
, const refresh_t
* R
, stale_flags
* sf
)
163 * Check for an explicit expiration time (Expires: header).
165 if (entry
->expires
> -1) {
168 if (entry
->expires
> check_time
) {
169 debugs(22, 3, "FRESH: expires " << entry
->expires
<<
170 " >= check_time " << check_time
<< " ");
174 debugs(22, 3, "STALE: expires " << entry
->expires
<<
175 " < check_time " << check_time
<< " ");
177 return (check_time
- entry
->expires
);
182 * Use local heuristics to determine staleness. Start with the
183 * max age from the refresh_pattern rule.
186 debugs(22, 3, "STALE: age " << age
<< " > max " << R
->max
<< " ");
188 return (age
- R
->max
);
192 * Try the last-modified factor algorithm: refresh_pattern n% percentage of Last-Modified: age.
194 if (entry
->lastmod
> -1 && entry
->timestamp
> entry
->lastmod
) {
196 * stale_age is the Age of the response when it became/becomes
197 * stale according to the last-modified factor algorithm.
199 time_t stale_age
= static_cast<time_t>((entry
->timestamp
- entry
->lastmod
) * R
->pct
);
202 if (age
>= stale_age
) {
203 debugs(22, 3, "STALE: age " << age
<< " > stale_age " << stale_age
);
204 return (age
- stale_age
);
206 debugs(22, 3, "FRESH: age " << age
<< " <= stale_age " << stale_age
);
212 * Finally, if all else fails; staleness is determined by the refresh_pattern
213 * configured minimum age.
216 debugs(22, 3, "FRESH: age " << age
<< " < min " << R
->min
);
221 debugs(22, 3, "STALE: age " << age
<< " >= min " << R
->min
);
222 return (age
- R
->min
);
226 * \retval 1 if the entry must be revalidated within delta seconds
227 * \retval 0 otherwise
229 * note: request maybe null (e.g. for cache digests build)
232 refreshCheck(const StoreEntry
* entry
, HttpRequest
* request
, time_t delta
)
235 const char *uri
= NULL
;
237 time_t check_time
= squid_curtime
+ delta
;
242 uri
= entry
->mem_obj
->url
;
244 uri
= urlCanonical(request
);
246 debugs(22, 3, "refreshCheck: '" << (uri
? uri
: "<none>") << "'");
248 if (check_time
> entry
->timestamp
)
249 age
= check_time
- entry
->timestamp
;
251 // FIXME: what to do when age < 0 or counter overflow?
254 R
= uri
? refreshLimits(uri
) : refreshUncompiledPattern(".");
259 debugs(22, 3, "refreshCheck: Matched '" << R
->pattern
<< " " <<
260 (int) R
->min
<< " " << (int) (100.0 * R
->pct
) << "%% " <<
261 (int) R
->max
<< "'");
263 debugs(22, 3, "\tage:\t" << age
);
265 debugs(22, 3, "\tcheck_time:\t" << mkrfc1123(check_time
));
267 debugs(22, 3, "\tentry->timestamp:\t" << mkrfc1123(entry
->timestamp
));
269 if (request
&& !request
->flags
.ignore_cc
) {
270 const HttpHdrCc
*const cc
= request
->cache_control
;
271 if (cc
&& cc
->hasMinFresh()) {
272 const int32_t minFresh
=cc
->minFresh();
273 debugs(22, 3, "\tage + min-fresh:\t" << age
<< " + " <<
274 minFresh
<< " = " << age
+ minFresh
);
275 debugs(22, 3, "\tcheck_time + min-fresh:\t" << check_time
<< " + "
276 << minFresh
<< " = " <<
277 mkrfc1123(check_time
+ minFresh
));
279 check_time
+= minFresh
;
283 memset(&sf
, '\0', sizeof(sf
));
285 staleness
= refreshStaleness(entry
, check_time
, age
, R
, &sf
);
287 debugs(22, 3, "Staleness = " << staleness
);
289 // stale-if-error requires any failure be passed thru when its period is over.
290 if (request
&& entry
->mem_obj
&& entry
->mem_obj
->getReply() && entry
->mem_obj
->getReply()->cache_control
&&
291 entry
->mem_obj
->getReply()->cache_control
->hasStaleIfError() &&
292 entry
->mem_obj
->getReply()->cache_control
->staleIfError() < staleness
) {
294 debugs(22, 3, "refreshCheck: stale-if-error period expired.");
295 request
->flags
.fail_on_validation_err
= 1;
298 if (EBIT_TEST(entry
->flags
, ENTRY_REVALIDATE
) && staleness
> -1
299 #if USE_HTTP_VIOLATIONS
300 && !R
->flags
.ignore_must_revalidate
303 debugs(22, 3, "refreshCheck: YES: Must revalidate stale response");
305 request
->flags
.fail_on_validation_err
= 1;
306 return STALE_MUST_REVALIDATE
;
309 /* request-specific checks */
310 if (request
&& !request
->flags
.ignore_cc
) {
311 HttpHdrCc
*cc
= request
->cache_control
;
313 if (request
->flags
.ims
&& (R
->flags
.refresh_ims
|| Config
.onoff
.refresh_all_ims
)) {
314 /* The clients no-cache header is changed into a IMS query */
315 debugs(22, 3, "refreshCheck: YES: refresh-ims");
316 return STALE_FORCED_RELOAD
;
319 #if USE_HTTP_VIOLATIONS
321 if (!request
->flags
.nocache_hack
) {
323 } else if (R
->flags
.ignore_reload
) {
324 /* The clients no-cache header is ignored */
325 debugs(22, 3, "refreshCheck: MAYBE: ignore-reload");
326 } else if (R
->flags
.reload_into_ims
|| Config
.onoff
.reload_into_ims
) {
327 /* The clients no-cache header is changed into a IMS query */
328 debugs(22, 3, "refreshCheck: YES: reload-into-ims");
329 return STALE_RELOAD_INTO_IMS
;
331 /* The clients no-cache header is not overridden on this request */
332 debugs(22, 3, "refreshCheck: YES: client reload");
333 request
->flags
.nocache
= 1;
334 return STALE_FORCED_RELOAD
;
339 if (cc
->hasMaxAge()) {
340 #if USE_HTTP_VIOLATIONS
341 if (R
->flags
.ignore_reload
&& cc
->maxAge() == 0) {
342 debugs(22, 3, "refreshCheck: MAYBE: client-max-age = 0 and ignore-reload");
346 if (cc
->maxAge() == 0) {
347 debugs(22, 3, "refreshCheck: YES: client-max-age = 0");
348 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
;
351 if (age
> cc
->maxAge()) {
352 debugs(22, 3, "refreshCheck: YES: age > client-max-age");
353 return STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
;
358 if (cc
->hasMaxStale() && staleness
> -1) {
359 if (cc
->maxStale()==HttpHdrCc::MAX_STALE_ANY
) {
360 /* max-stale directive without a value */
361 debugs(22, 3, "refreshCheck: NO: max-stale wildcard");
362 return FRESH_REQUEST_MAX_STALE_ALL
;
363 } else if (staleness
< cc
->maxStale()) {
364 debugs(22, 3, "refreshCheck: NO: staleness < max-stale");
365 return FRESH_REQUEST_MAX_STALE_VALUE
;
371 if (-1 == staleness
) {
372 debugs(22, 3, "refreshCheck: object isn't stale..");
374 debugs(22, 3, "refreshCheck: returning FRESH_EXPIRES");
375 return FRESH_EXPIRES
;
381 debugs(22, 3, "refreshCheck: returning FRESH_LMFACTOR_RULE");
382 return FRESH_LMFACTOR_RULE
;
387 debugs(22, 3, "refreshCheck: returning FRESH_MIN_RULE");
388 return FRESH_MIN_RULE
;
392 * At this point the response is stale, unless one of
393 * the override options kicks in.
394 * NOTE: max-stale config blocks the overrides.
396 int max_stale
= (R
->max_stale
>= 0 ? R
->max_stale
: Config
.maxStale
);
397 if ( max_stale
>= 0 && staleness
> max_stale
) {
398 debugs(22, 3, "refreshCheck: YES: max-stale limit");
400 request
->flags
.fail_on_validation_err
= 1;
401 return STALE_MAX_STALE
;
405 #if USE_HTTP_VIOLATIONS
407 if (R
->flags
.override_expire
&& age
< R
->min
) {
408 debugs(22, 3, "refreshCheck: NO: age < min && override-expire");
409 return FRESH_OVERRIDE_EXPIRES
;
413 return STALE_EXPIRES
;
417 return STALE_MAX_RULE
;
420 #if USE_HTTP_VIOLATIONS
422 if (R
->flags
.override_lastmod
&& age
< R
->min
) {
423 debugs(22, 3, "refreshCheck: NO: age < min && override-lastmod");
424 return FRESH_OVERRIDE_LASTMOD
;
428 return STALE_LMFACTOR_RULE
;
431 debugs(22, 3, "refreshCheck: returning STALE_DEFAULT");
432 return STALE_DEFAULT
;
436 refreshIsCachable(const StoreEntry
* entry
)
439 * Don't look at the request to avoid no-cache and other nuisances.
440 * the object should have a mem_obj so the URL will be found there.
441 * minimum_expiry_time seconds delta (defaults to 60 seconds), to
442 * avoid objects which expire almost immediately, and which can't
445 int reason
= refreshCheck(entry
, NULL
, Config
.minimum_expiry_time
);
446 ++ refreshCounts
[rcStore
].total
;
447 ++ refreshCounts
[rcStore
].status
[reason
];
449 if (reason
< STALE_MUST_REVALIDATE
)
450 /* Does not need refresh. This is certainly cachable */
453 if (entry
->lastmod
< 0)
454 /* Last modified is needed to do a refresh */
457 if (entry
->mem_obj
== NULL
)
461 if (entry
->getReply() == NULL
)
465 if (entry
->getReply()->content_length
== 0)
466 /* No use refreshing (caching?) 0 byte objects */
469 /* This seems to be refreshable. Cache it */
473 /// whether reply is stale if it is a hit
475 refreshIsStaleIfHit(const int reason
)
479 case FRESH_LMFACTOR_RULE
:
487 /* refreshCheck... functions below are protocol-specific wrappers around
488 * refreshCheck() function above */
491 refreshCheckHTTP(const StoreEntry
* entry
, HttpRequest
* request
)
493 int reason
= refreshCheck(entry
, request
, 0);
494 ++ refreshCounts
[rcHTTP
].total
;
495 ++ refreshCounts
[rcHTTP
].status
[reason
];
496 request
->flags
.stale_if_hit
= refreshIsStaleIfHit(reason
);
497 return (Config
.onoff
.offline
|| reason
< 200) ? 0 : 1;
501 refreshCheckICP(const StoreEntry
* entry
, HttpRequest
* request
)
503 int reason
= refreshCheck(entry
, request
, 30);
504 ++ refreshCounts
[rcICP
].total
;
505 ++ refreshCounts
[rcICP
].status
[reason
];
506 return (reason
< 200) ? 0 : 1;
511 refreshCheckHTCP(const StoreEntry
* entry
, HttpRequest
* request
)
513 int reason
= refreshCheck(entry
, request
, 10);
514 ++ refreshCounts
[rcHTCP
].total
;
515 ++ refreshCounts
[rcHTCP
].status
[reason
];
516 return (reason
< 200) ? 0 : 1;
521 #if USE_CACHE_DIGESTS
523 refreshCheckDigest(const StoreEntry
* entry
, time_t delta
)
525 int reason
= refreshCheck(entry
,
526 entry
->mem_obj
? entry
->mem_obj
->request
: NULL
,
528 ++ refreshCounts
[rcCDigest
].total
;
529 ++ refreshCounts
[rcCDigest
].status
[reason
];
530 return (reason
< 200) ? 0 : 1;
536 getMaxAge(const char *url
)
539 debugs(22, 3, "getMaxAge: '" << url
<< "'");
541 if ((R
= refreshLimits(url
)))
544 return REFRESH_DEFAULT_MAX
;
549 refreshCountsStats(StoreEntry
* sentry
, struct RefreshCounts
*rc
)
554 storeAppendPrintf(sentry
, "\n\n%s histogram:\n", rc
->proto
);
555 storeAppendPrintf(sentry
, "Count\t%%Total\tCategory\n");
557 #define refreshCountsStatsEntry(code,desc) { \
558 storeAppendPrintf(sentry, "%6d\t%6.2f\t%s\n", \
559 rc->status[code], xpercent(rc->status[code], tot), desc); \
560 sum += rc->status[code]; \
563 refreshCountsStatsEntry(FRESH_REQUEST_MAX_STALE_ALL
,
564 "Fresh: request max-stale wildcard");
565 refreshCountsStatsEntry(FRESH_REQUEST_MAX_STALE_VALUE
,
566 "Fresh: request max-stale value");
567 refreshCountsStatsEntry(FRESH_EXPIRES
,
568 "Fresh: expires time not reached");
569 refreshCountsStatsEntry(FRESH_LMFACTOR_RULE
,
570 "Fresh: refresh_pattern last-mod factor percentage");
571 refreshCountsStatsEntry(FRESH_MIN_RULE
,
572 "Fresh: refresh_pattern min value");
573 refreshCountsStatsEntry(FRESH_OVERRIDE_EXPIRES
,
574 "Fresh: refresh_pattern override expires");
575 refreshCountsStatsEntry(FRESH_OVERRIDE_LASTMOD
,
576 "Fresh: refresh_pattern override lastmod");
577 refreshCountsStatsEntry(STALE_MUST_REVALIDATE
,
578 "Stale: response has must-revalidate");
579 refreshCountsStatsEntry(STALE_RELOAD_INTO_IMS
,
580 "Stale: changed reload into IMS");
581 refreshCountsStatsEntry(STALE_FORCED_RELOAD
,
582 "Stale: request has no-cache directive");
583 refreshCountsStatsEntry(STALE_EXCEEDS_REQUEST_MAX_AGE_VALUE
,
584 "Stale: age exceeds request max-age value");
585 refreshCountsStatsEntry(STALE_EXPIRES
,
586 "Stale: expires time reached");
587 refreshCountsStatsEntry(STALE_MAX_RULE
,
588 "Stale: refresh_pattern max age rule");
589 refreshCountsStatsEntry(STALE_LMFACTOR_RULE
,
590 "Stale: refresh_pattern last-mod factor percentage");
591 refreshCountsStatsEntry(STALE_DEFAULT
,
592 "Stale: by default");
594 tot
= sum
; /* paranoid: "total" line shows 100% if we forgot nothing */
595 storeAppendPrintf(sentry
, "%6d\t%6.2f\tTOTAL\n",
596 rc
->total
, xpercent(rc
->total
, tot
));
598 storeAppendPrintf(sentry
, "\n");
602 refreshStats(StoreEntry
* sentry
)
607 /* get total usage count */
609 for (i
= 0; i
< rcCount
; ++i
)
610 total
+= refreshCounts
[i
].total
;
612 /* protocol usage histogram */
613 storeAppendPrintf(sentry
, "\nRefreshCheck calls per protocol\n\n");
615 storeAppendPrintf(sentry
, "Protocol\t#Calls\t%%Calls\n");
617 for (i
= 0; i
< rcCount
; ++i
)
618 storeAppendPrintf(sentry
, "%10s\t%6d\t%6.2f\n",
619 refreshCounts
[i
].proto
,
620 refreshCounts
[i
].total
,
621 xpercent(refreshCounts
[i
].total
, total
));
623 /* per protocol histograms */
624 storeAppendPrintf(sentry
, "\n\nRefreshCheck histograms for various protocols\n");
626 for (i
= 0; i
< rcCount
; ++i
)
627 refreshCountsStats(sentry
, &refreshCounts
[i
]);
631 refreshRegisterWithCacheManager(void)
633 Mgr::RegisterAction("refresh", "Refresh Algorithm Statistics", refreshStats
, 0, 1);
639 memset(refreshCounts
, 0, sizeof(refreshCounts
));
640 refreshCounts
[rcHTTP
].proto
= "HTTP";
641 refreshCounts
[rcICP
].proto
= "ICP";
644 refreshCounts
[rcHTCP
].proto
= "HTCP";
647 refreshCounts
[rcStore
].proto
= "On Store";
648 #if USE_CACHE_DIGESTS
650 refreshCounts
[rcCDigest
].proto
= "Cache Digests";
653 memset(&DefaultRefresh
, '\0', sizeof(DefaultRefresh
));
654 DefaultRefresh
.pattern
= "<none>";
655 DefaultRefresh
.min
= REFRESH_DEFAULT_MIN
;
656 DefaultRefresh
.pct
= REFRESH_DEFAULT_PCT
;
657 DefaultRefresh
.max
= REFRESH_DEFAULT_MAX
;
659 refreshRegisterWithCacheManager();