From: phk Date: Sat, 24 Jun 2006 21:54:54 +0000 (+0000) Subject: Use ttl=0 as a "invalid TTL" flag. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d7c2bd100ee49215d30dcb7fab78db33b81284f3;p=varnish Use ttl=0 as a "invalid TTL" flag. Mark objects with ttl=0 uncachable. Add cacheable objects to the expiry heap Start an expiry thread which polls the root element once per second git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@231 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index df3e0ad4..60af0aa5 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -138,6 +138,7 @@ void VBE_ClosedFd(void *ptr); void VBE_RecycleFd(void *ptr); /* cache_expiry.c */ +void EXP_Insert(struct object *o); void EXP_Init(void); /* cache_fetch.c */ diff --git a/varnish-cache/bin/varnishd/cache_expire.c b/varnish-cache/bin/varnishd/cache_expire.c index 0ff4e6cf..ab43a137 100644 --- a/varnish-cache/bin/varnishd/cache_expire.c +++ b/varnish-cache/bin/varnishd/cache_expire.c @@ -4,7 +4,73 @@ * Expiry of cached objects and execution of prefetcher */ +#include +#include +#include +#include + +#include "libvarnish.h" +#include "binary_heap.h" +#include "cache.h" + +static pthread_t exp_thread; +static struct binheap *exp_heap; +static pthread_mutex_t expmtx; + +/*--------------------------------------------------------------------*/ + +void +EXP_Insert(struct object *o) +{ + + AZ(pthread_mutex_lock(&expmtx)); + binheap_insert(exp_heap, o); + AZ(pthread_mutex_unlock(&expmtx)); +} + +/*--------------------------------------------------------------------*/ + +static void * +exp_main(void *arg) +{ + struct object *o; + time_t t; + + while (1) { + time(&t); + AZ(pthread_mutex_lock(&expmtx)); + o = binheap_root(exp_heap); + AZ(pthread_mutex_unlock(&expmtx)); + if (o != NULL) { + printf("Root: %p %d (%d)\n", + (void*)o, o->ttl, o->ttl - t); + } + sleep(1); + } + + return ("FOOBAR"); +} + +/*--------------------------------------------------------------------*/ + +static int +object_cmp(void *priv, void *a, void *b) +{ + struct object *aa, *bb; + + aa = a; + bb = b; + return (aa->ttl < bb->ttl); +} + +/*--------------------------------------------------------------------*/ + void EXP_Init(void) { + + AZ(pthread_create(&exp_thread, NULL, exp_main, NULL)); + AZ(pthread_mutex_init(&expmtx, NULL)); + exp_heap = binheap_new(NULL, object_cmp, NULL); + assert(exp_heap != NULL); } diff --git a/varnish-cache/bin/varnishd/cache_fetch.c b/varnish-cache/bin/varnishd/cache_fetch.c index ed1518f1..2966496b 100644 --- a/varnish-cache/bin/varnishd/cache_fetch.c +++ b/varnish-cache/bin/varnishd/cache_fetch.c @@ -223,8 +223,6 @@ FetchSession(struct worker *w, struct sess *sp) time(&t_resp); http_Dissect(hp, fd, 2); - sp->obj->ttl = RFC2616_Ttl(hp, t_req, t_resp); - switch (http_GetStatus(hp)) { case 200: case 301: @@ -242,8 +240,16 @@ FetchSession(struct worker *w, struct sess *sp) break; } + sp->obj->ttl = RFC2616_Ttl(hp, t_req, t_resp); + if (sp->obj->ttl == 0) { + sp->obj->cacheable = 0; + } + VCL_fetch_method(sp); + if (sp->obj->cacheable) + EXP_Insert(sp->obj); + if (http_GetHdr(hp, "Content-Length", &b)) cls = fetch_straight(w, sp, fd, hp, b); else if (http_HdrIs(hp, "Transfer-Encoding", "chunked")) @@ -264,5 +270,7 @@ FetchSession(struct worker *w, struct sess *sp) /* XXX: unbusy, and kick other sessions into action */ sp->obj->busy = 0; + /* XXX: if not cachable, destroy */ + return (1); } diff --git a/varnish-cache/bin/varnishd/rfc2616.c b/varnish-cache/bin/varnishd/rfc2616.c index 3e3c9d17..1ddc24fb 100644 --- a/varnish-cache/bin/varnishd/rfc2616.c +++ b/varnish-cache/bin/varnishd/rfc2616.c @@ -82,7 +82,7 @@ RFC2616_Ttl(struct http *hp, time_t t_req, time_t t_resp) ttl = t_resp + heritage.default_ttl; printf("TTL: %d (%+d)\n", ttl, ttl - t_resp); if (ttl < t_resp) - return (t_resp); + return (0); return (ttl); }