r268 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Thu Jun 29 19:09:24 CEST 2006


Author: phk
Date: 2006-06-29 19:09:24 +0200 (Thu, 29 Jun 2006)
New Revision: 268

Added:
   trunk/varnish-cache/bin/varnishd/cache_ban.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_hash.c
   trunk/varnish-cache/bin/varnishd/cache_main.c
Log:
Add the ability to instantly ban/purge all cached objects matching
a given regexp.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2006-06-29 15:14:15 UTC (rev 267)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2006-06-29 17:09:24 UTC (rev 268)
@@ -7,6 +7,7 @@
 varnishd_SOURCES = \
 	cache_acceptor.c \
 	cache_backend.c \
+	cache_ban.c \
 	cache_expire.c \
 	cache_fetch.c \
 	cache_hash.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-06-29 15:14:15 UTC (rev 267)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-06-29 17:09:24 UTC (rev 268)
@@ -9,6 +9,7 @@
 #define VCA_ADDRBUFSIZE		32	/* Sizeof ascii network address */
 
 struct event_base;
+struct cli;
 struct sbuf;
 struct sess;
 struct object;
@@ -69,6 +70,8 @@
 	struct objhead		*objhead;
 	pthread_cond_t		cv;
 
+	unsigned		ban_seq;
+
 	unsigned		valid;
 	unsigned		cacheable;
 
@@ -149,6 +152,12 @@
 void VBE_ClosedFd(void *ptr);
 void VBE_RecycleFd(void *ptr);
 
+/* cache_ban.c */
+void BAN_Init(void);
+void cli_func_url_purge(struct cli *cli, char **av, void *priv);
+void BAN_NewObj(struct object *o);
+int BAN_CheckObject(struct object *o, const char *url);
+
 /* cache_expiry.c */
 void EXP_Insert(struct object *o);
 void EXP_Init(void);

Added: trunk/varnish-cache/bin/varnishd/cache_ban.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_ban.c	2006-06-29 15:14:15 UTC (rev 267)
+++ trunk/varnish-cache/bin/varnishd/cache_ban.c	2006-06-29 17:09:24 UTC (rev 268)
@@ -0,0 +1,87 @@
+/*
+ * $Id$
+ *
+ * Ban processing
+ */
+
+#include <assert.h>
+#include <stdlib.h>
+#include <string.h>
+#include <regex.h>
+
+#include "shmlog.h"
+#include "cli_priv.h"
+#include "cache.h"
+
+struct ban {
+	TAILQ_ENTRY(ban)	list;
+	unsigned		gen;
+	regex_t			regexp;
+	char			*ban;
+};
+
+static TAILQ_HEAD(,ban) ban_head = TAILQ_HEAD_INITIALIZER(ban_head);
+static unsigned ban_next;
+static struct ban *ban_start;
+
+static void
+AddBan(const char *regexp)
+{
+	struct ban *b;
+	int i;
+
+	b = calloc(sizeof *b, 1);
+	assert(b != NULL);
+
+	i = regcomp(&b->regexp, regexp, REG_EXTENDED | REG_NOSUB);
+	if (i) {
+		char buf[512];
+	
+		regerror(i, &b->regexp, buf, sizeof buf);
+		VSL(SLT_Debug, 0, "REGEX: <%s>", buf);
+	}
+	b->gen = ++ban_next;
+	b->ban = strdup(regexp);
+	TAILQ_INSERT_HEAD(&ban_head, b, list);
+	ban_start = b;
+}
+
+void
+BAN_NewObj(struct object *o)
+{
+
+	o->ban_seq = ban_next;
+}
+
+int
+BAN_CheckObject(struct object *o, const char *url)
+{
+	struct ban *b, *b0;
+	int i;
+
+	b0 = ban_start;
+	for (b = b0;
+	    b != NULL && b->gen > o->ban_seq;
+	    b = TAILQ_NEXT(b, list)) {
+		i = regexec(&b->regexp, url, 0, NULL, 0);
+		if (!i)
+			return (1);
+	} 
+	o->ban_seq = b0->gen;
+	return (0);
+}
+
+void
+cli_func_url_purge(struct cli *cli, char **av, void *priv)
+{
+
+	AddBan(av[2]);
+	cli_out(cli, "PURGE %s\n", av[2]);
+}
+
+void
+BAN_Init(void)
+{
+
+	AddBan("a");
+}

Modified: trunk/varnish-cache/bin/varnishd/cache_hash.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_hash.c	2006-06-29 15:14:15 UTC (rev 267)
+++ trunk/varnish-cache/bin/varnishd/cache_hash.c	2006-06-29 17:09:24 UTC (rev 268)
@@ -12,6 +12,7 @@
 #include <pthread.h>
 
 #include "libvarnish.h"
+#include "shmlog.h"
 #include "cache.h"
 
 static struct hash_slinger      *hash;
@@ -48,8 +49,13 @@
 		o->refcnt++;
 		if (o->busy)
 			AZ(pthread_cond_wait(&o->cv, &oh->mtx));
-		/* XXX: do Vary: comparison */
-		if (1)
+		/* XXX: check TTL */
+		if (o->ttl == 0) {
+			VSL(SLT_Debug, 0, "Object %p had 0 ttl", o);
+		} else if (BAN_CheckObject(o, b)) {
+			o->ttl = 0;
+			VSL(SLT_Debug, 0, "Object %p was banned", o);
+		} else 
 			break;
 		o->refcnt--;
 	}
@@ -67,6 +73,7 @@
 	TAILQ_INSERT_TAIL(&oh->objects, o, list);
 	/* NB: do not deref objhead the new object inherits our reference */
 	AZ(pthread_mutex_unlock(&oh->mtx));
+	BAN_NewObj(o);
 	return (o);
 }
 

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2006-06-29 15:14:15 UTC (rev 267)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2006-06-29 17:09:24 UTC (rev 268)
@@ -83,6 +83,7 @@
 
 static struct cli_proto cli_proto[] = {
 	{ CLI_URL_QUERY,	cli_func_url_query },
+	{ CLI_URL_PURGE,	cli_func_url_purge },
 	{ CLI_CONFIG_LOAD,	cli_func_config_load },
 	{ CLI_CONFIG_LIST,	cli_func_config_list },
 	{ CLI_CONFIG_UNLOAD,	cli_func_config_unload },
@@ -115,6 +116,7 @@
 	VCA_Init();
 	EXP_Init();
 	HSH_Init();
+	BAN_Init();
 
 	eb = event_init();
 	assert(eb != NULL);




More information about the varnish-commit mailing list