]> err.no Git - varnish/commitdiff
Use ttl=0 as a "invalid TTL" flag.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 24 Jun 2006 21:54:54 +0000 (21:54 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 24 Jun 2006 21:54:54 +0000 (21:54 +0000)
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

varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_expire.c
varnish-cache/bin/varnishd/cache_fetch.c
varnish-cache/bin/varnishd/rfc2616.c

index df3e0ad41f8fcbd48d2f2d51e46bd77aac08a6e1..60af0aa59c969415504b799dcb3c025698699f4d 100644 (file)
@@ -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 */
index 0ff4e6cfdeb165df4c9233c0ba4d811e28b360e9..ab43a137b97d8baecc03d007e067784faea84801 100644 (file)
@@ -4,7 +4,73 @@
  * Expiry of cached objects and execution of prefetcher
  */
 
+#include <pthread.h>
+#include <unistd.h>
+#include <assert.h>
+#include <stdio.h>
+
+#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);
 }
index ed1518f10a96045d7bd34c0781f2cb577b7702e1..2966496bb4a714a57b1ad650c3a3b2c85139bcc7 100644 (file)
@@ -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);
 }
index 3e3c9d1781968c4dd1283c307db5d689f25dd39e..1ddc24fbb4fda368a9ac46563e3544b7cd0a6bff 100644 (file)
@@ -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);
 }