r231 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Sat Jun 24 23:54:54 CEST 2006


Author: phk
Date: 2006-06-24 23:54:54 +0200 (Sat, 24 Jun 2006)
New Revision: 231

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_expire.c
   trunk/varnish-cache/bin/varnishd/cache_fetch.c
   trunk/varnish-cache/bin/varnishd/rfc2616.c
Log:
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




Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-06-24 21:42:27 UTC (rev 230)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-06-24 21:54:54 UTC (rev 231)
@@ -138,6 +138,7 @@
 void VBE_RecycleFd(void *ptr);
 
 /* cache_expiry.c */
+void EXP_Insert(struct object *o);
 void EXP_Init(void);
 
 /* cache_fetch.c */

Modified: trunk/varnish-cache/bin/varnishd/cache_expire.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_expire.c	2006-06-24 21:42:27 UTC (rev 230)
+++ trunk/varnish-cache/bin/varnishd/cache_expire.c	2006-06-24 21:54:54 UTC (rev 231)
@@ -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);
 }

Modified: trunk/varnish-cache/bin/varnishd/cache_fetch.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_fetch.c	2006-06-24 21:42:27 UTC (rev 230)
+++ trunk/varnish-cache/bin/varnishd/cache_fetch.c	2006-06-24 21:54:54 UTC (rev 231)
@@ -223,8 +223,6 @@
 	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 @@
 		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 @@
 	/* XXX: unbusy, and kick other sessions into action */
 	sp->obj->busy = 0;
 
+	/* XXX: if not cachable, destroy */
+
 	return (1);
 }

Modified: trunk/varnish-cache/bin/varnishd/rfc2616.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/rfc2616.c	2006-06-24 21:42:27 UTC (rev 230)
+++ trunk/varnish-cache/bin/varnishd/rfc2616.c	2006-06-24 21:54:54 UTC (rev 231)
@@ -82,7 +82,7 @@
 		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);
 }




More information about the varnish-commit mailing list