From b10c2ec3d823b2e7fde560b786944b9754bb5e02 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 29 Jun 2006 17:09:24 +0000 Subject: [PATCH] Add the ability to instantly ban/purge all cached objects matching a given regexp. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@268 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/Makefile.am | 1 + varnish-cache/bin/varnishd/cache.h | 9 +++ varnish-cache/bin/varnishd/cache_ban.c | 87 +++++++++++++++++++++++++ varnish-cache/bin/varnishd/cache_hash.c | 11 +++- varnish-cache/bin/varnishd/cache_main.c | 2 + 5 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 varnish-cache/bin/varnishd/cache_ban.c diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index 070ed754..60d663ea 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -7,6 +7,7 @@ bin_PROGRAMS = varnishd varnishd_SOURCES = \ cache_acceptor.c \ cache_backend.c \ + cache_ban.c \ cache_expire.c \ cache_fetch.c \ cache_hash.c \ diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index 797c3719..54507eb3 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -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 object { struct objhead *objhead; pthread_cond_t cv; + unsigned ban_seq; + unsigned valid; unsigned cacheable; @@ -149,6 +152,12 @@ int VBE_GetFd(struct backend *bp, void **ptr, unsigned xid); 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); diff --git a/varnish-cache/bin/varnishd/cache_ban.c b/varnish-cache/bin/varnishd/cache_ban.c new file mode 100644 index 00000000..e162a67e --- /dev/null +++ b/varnish-cache/bin/varnishd/cache_ban.c @@ -0,0 +1,87 @@ +/* + * $Id$ + * + * Ban processing + */ + +#include +#include +#include +#include + +#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"); +} diff --git a/varnish-cache/bin/varnishd/cache_hash.c b/varnish-cache/bin/varnishd/cache_hash.c index 620dc661..1ad72244 100644 --- a/varnish-cache/bin/varnishd/cache_hash.c +++ b/varnish-cache/bin/varnishd/cache_hash.c @@ -12,6 +12,7 @@ #include #include "libvarnish.h" +#include "shmlog.h" #include "cache.h" static struct hash_slinger *hash; @@ -48,8 +49,13 @@ HSH_Lookup(struct worker *w, struct http *h) 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 @@ HSH_Lookup(struct worker *w, struct http *h) 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); } diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index ada0e103..236fe7d7 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -83,6 +83,7 @@ cli_func_ping(struct cli *cli, char **av, void *priv) 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 @@ child_main(void) VCA_Init(); EXP_Init(); HSH_Init(); + BAN_Init(); eb = event_init(); assert(eb != NULL); -- 2.39.5