From 6799295cc023d1a829f9f5eab93be09501356e49 Mon Sep 17 00:00:00 2001 From: phk Date: Wed, 21 Jun 2006 10:21:14 +0000 Subject: [PATCH] Start to respect TTL git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@220 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache.h | 5 +++-- varnish-cache/bin/varnishd/cache_fetch.c | 2 +- varnish-cache/bin/varnishd/cache_pool.c | 11 ++++++----- varnish-cache/bin/varnishd/rfc2616.c | 18 ++++++++++++++---- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 7bf6ebd3..9014fa89 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -71,6 +71,7 @@ struct object { unsigned busy; unsigned len; + time_t ttl; char *header; @@ -105,6 +106,7 @@ struct sess { /* Various internal stuff */ struct event *rd_e; struct sessmem *mem; + time_t t0; }; struct backend { @@ -200,5 +202,4 @@ cli_func_t cli_func_config_use; #endif /* rfc2616.c */ -void RFC2616_Age(struct http *hp, time_t, time_t); - +time_t RFC2616_Ttl(struct http *hp, time_t, time_t); diff --git a/varnish-cache/bin/varnishd/cache_fetch.c b/varnish-cache/bin/varnishd/cache_fetch.c index fec88299..34e82001 100644 --- a/varnish-cache/bin/varnishd/cache_fetch.c +++ b/varnish-cache/bin/varnishd/cache_fetch.c @@ -223,7 +223,7 @@ FetchSession(struct worker *w, struct sess *sp) time(&t_resp); http_Dissect(hp, fd, 2); - RFC2616_Age(hp, t_req, t_resp); + sp->obj->ttl = RFC2616_Ttl(hp, t_req, t_resp); switch (http_GetStatus(hp)) { case 200: diff --git a/varnish-cache/bin/varnishd/cache_pool.c b/varnish-cache/bin/varnishd/cache_pool.c index 93e7d8e6..c10c021a 100644 --- a/varnish-cache/bin/varnishd/cache_pool.c +++ b/varnish-cache/bin/varnishd/cache_pool.c @@ -47,15 +47,15 @@ LookupSession(struct worker *w, struct sess *sp) MD5Final(key, &ctx); o = hash->lookup(key, w->nobj); sp->obj = o; - if (o == w->nobj) { - VSL(SLT_Debug, 0, "Lookup new %p %s", o, b); - w->nobj = NULL; - VCL_miss_method(sp); - } else { + if (o != w->nobj && o->ttl > sp->t0) { /* XXX: wait while obj->busy */ VSL(SLT_Debug, 0, "Lookup found %p %s", o, b); VCL_hit_method(sp); + return (0); } + VSL(SLT_Debug, 0, "Lookup new %p %s", o, b); + w->nobj = NULL; + VCL_miss_method(sp); return (0); } @@ -97,6 +97,7 @@ CacheWorker(void *priv) AZ(pthread_cond_wait(&shdcnd, &sessmtx)); } TAILQ_REMOVE(&shd, sp, list); + time(&sp->t0); sp->vcl = GetVCL(); AZ(pthread_mutex_unlock(&sessmtx)); diff --git a/varnish-cache/bin/varnishd/rfc2616.c b/varnish-cache/bin/varnishd/rfc2616.c index 51bdd944..18944736 100644 --- a/varnish-cache/bin/varnishd/rfc2616.c +++ b/varnish-cache/bin/varnishd/rfc2616.c @@ -8,6 +8,7 @@ #include "cache.h" #include "libvarnish.h" +#include "heritage.h" /*-------------------------------------------------------------------- * From RFC2616, 13.2.3 Age Calculations @@ -34,13 +35,13 @@ * */ -void -RFC2616_Age(struct http *hp, time_t t_req, time_t t_resp) +time_t +RFC2616_Ttl(struct http *hp, time_t t_req, time_t t_resp) { time_t h_date = 0, h_expires = 0, h_age = 0; time_t apparent_age = 0, corrected_received_age; time_t response_delay, corrected_initial_age; - time_t max_age = -1; + time_t max_age = -1, ttl; char *p; if (http_GetHdrField(hp, "Cache-Control", "max-age", &p)) @@ -67,10 +68,19 @@ RFC2616_Age(struct http *hp, time_t t_req, time_t t_resp) h_expires = TIM_parse(p); printf("Date: %d\n", h_date); + printf("Recv: %d\n", t_resp); printf("Expires: %d\n", h_expires); printf("Age: %d\n", h_age); printf("CIAge: %d\n", corrected_initial_age); printf("Max-Age: %d\n", max_age); + ttl = 0; + if (max_age >= 0) + ttl = t_resp + max_age - corrected_initial_age; + if (h_expires && h_expires < ttl) + ttl = h_expires; + if (ttl == 0) + ttl = t_resp + heritage.default_ttl; + printf("TTL: %d (+%d)\n", ttl, ttl - t_resp); - + return (ttl); } -- 2.39.5