From: tfheen Date: Wed, 17 Sep 2008 10:04:47 +0000 (+0000) Subject: Add string representation of backend X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3198c5eb4accde2145c9a3420cc217e413c2b51c;p=varnish Add string representation of backend This takes the name from the name assigned in the VCL. Partially fixes #294. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@3197 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/cache_backend.h b/varnish-cache/bin/varnishd/cache_backend.h index 780fdc4c..0b78dea5 100644 --- a/varnish-cache/bin/varnishd/cache_backend.h +++ b/varnish-cache/bin/varnishd/cache_backend.h @@ -85,6 +85,7 @@ struct director { unsigned magic; #define DIRECTOR_MAGIC 0x3336351d const char *name; + char *vcl_name; vdi_getfd_f *getfd; vdi_fini_f *fini; vdi_healthy *healthy; diff --git a/varnish-cache/bin/varnishd/cache_dir_random.c b/varnish-cache/bin/varnishd/cache_dir_random.c index bb386f79..35c08c92 100644 --- a/varnish-cache/bin/varnishd/cache_dir_random.c +++ b/varnish-cache/bin/varnishd/cache_dir_random.c @@ -152,6 +152,7 @@ vdi_random_fini(struct director *d) for (i = 0; i < vs->nhosts; i++, vh++) VBE_DropRef(vh->backend); free(vs->hosts); + free(vs->dir.vcl_name); vs->dir.magic = 0; FREE_OBJ(vs); } @@ -174,6 +175,7 @@ VRT_init_dir_random(struct cli *cli, struct director **bp, const struct vrt_dir_ vs->dir.magic = DIRECTOR_MAGIC; vs->dir.priv = vs; vs->dir.name = "random"; + REPLACE(vs->dir.vcl_name, t->name); vs->dir.getfd = vdi_random_getfd; vs->dir.fini = vdi_random_fini; vs->dir.healthy = vdi_random_healthy; diff --git a/varnish-cache/bin/varnishd/cache_dir_round_robin.c b/varnish-cache/bin/varnishd/cache_dir_round_robin.c index de7fc240..479657c4 100644 --- a/varnish-cache/bin/varnishd/cache_dir_round_robin.c +++ b/varnish-cache/bin/varnishd/cache_dir_round_robin.c @@ -116,6 +116,7 @@ vdi_round_robin_fini(struct director *d) for (i = 0; i < vs->nhosts; i++, vh++) VBE_DropRef(vh->backend); free(vs->hosts); + free(vs->dir.vcl_name); vs->dir.magic = 0; vs->next_host = 0; FREE_OBJ(vs); @@ -139,6 +140,7 @@ VRT_init_dir_round_robin(struct cli *cli, struct director **bp, const struct vrt vs->dir.magic = DIRECTOR_MAGIC; vs->dir.priv = vs; vs->dir.name = "round_robin"; + REPLACE(vs->dir.vcl_name, t->name); vs->dir.getfd = vdi_round_robin_getfd; vs->dir.fini = vdi_round_robin_fini; vs->dir.healthy = vdi_round_robin_healthy; diff --git a/varnish-cache/bin/varnishd/cache_dir_simple.c b/varnish-cache/bin/varnishd/cache_dir_simple.c index e0e8b81f..dc47fa0b 100644 --- a/varnish-cache/bin/varnishd/cache_dir_simple.c +++ b/varnish-cache/bin/varnishd/cache_dir_simple.c @@ -87,6 +87,7 @@ vdi_simple_fini(struct director *d) CAST_OBJ_NOTNULL(vs, d->priv, VDI_SIMPLE_MAGIC); VBE_DropRef(vs->backend); + free(vs->dir.vcl_name); vs->dir.magic = 0; FREE_OBJ(vs); } @@ -103,6 +104,7 @@ VRT_init_dir_simple(struct cli *cli, struct director **bp, const struct vrt_dir_ vs->dir.magic = DIRECTOR_MAGIC; vs->dir.priv = vs; vs->dir.name = "simple"; + REPLACE(vs->dir.vcl_name, t->host->vcl_name); vs->dir.getfd = vdi_simple_getfd; vs->dir.fini = vdi_simple_fini; vs->dir.healthy = vdi_simple_healthy; diff --git a/varnish-cache/bin/varnishd/cache_vrt.c b/varnish-cache/bin/varnishd/cache_vrt.c index c1e92224..dc08fb46 100644 --- a/varnish-cache/bin/varnishd/cache_vrt.c +++ b/varnish-cache/bin/varnishd/cache_vrt.c @@ -648,6 +648,14 @@ VRT_double_string(const struct sess *sp, double num) return (p); } +const char * +VRT_backend_string(struct sess *sp) +{ + CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); + CHECK_OBJ_NOTNULL(sp->director, DIRECTOR_MAGIC); + return (sp->director->vcl_name); +} + /*--------------------------------------------------------------------*/ void diff --git a/varnish-cache/bin/varnishtest/tests/b00016.vtc b/varnish-cache/bin/varnishtest/tests/b00016.vtc new file mode 100644 index 00000000..cf70f6a9 --- /dev/null +++ b/varnish-cache/bin/varnishtest/tests/b00016.vtc @@ -0,0 +1,73 @@ +# $Id$ + +test "Check naming of backends" + +varnish v1 -vcl { + backend foo { + .host = "127.0.0.2"; + .port = "9080"; + } + + sub vcl_recv { + error 200 "ok"; + } + + sub vcl_error { + set obj.http.X-Backend-Name = req.backend; + } +} -start + +client c1 { + txreq -url "/" + rxresp + expect resp.http.X-Backend-Name == "foo" +} -run + +varnish v1 -vcl { + director bar random { + { + .backend = { + .host = "127.0.0.2"; + .port = "9080"; + } + .weight = 1; + } + } + + sub vcl_recv { + error 200 "ok"; + } + + sub vcl_error { + set obj.http.X-Backend-Name = req.backend; + } +} + +client c1 { + txreq -url "/" + rxresp + expect resp.http.X-Backend-Name == "bar" +} -run + +varnish v1 -vcl { + director baz round-robin { + { .backend = { + .host = "127.0.0.2"; + .port = "9080"; + } } + } + + sub vcl_recv { + error 200 "ok"; + } + + sub vcl_error { + set obj.http.X-Backend-Name = req.backend; + } +} + +client c1 { + txreq -url "/" + rxresp + expect resp.http.X-Backend-Name == "baz" +} -run diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index e9c8e3db..121d9bf8 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -171,6 +171,7 @@ void VRT_fini_dir(struct cli *, struct director *); char *VRT_IP_string(const struct sess *sp, const struct sockaddr *sa); char *VRT_int_string(const struct sess *sp, int); char *VRT_double_string(const struct sess *sp, double); +const char *VRT_backend_string(struct sess *sp); #define VRT_done(sp, hand) \ do { \ diff --git a/varnish-cache/lib/libvcl/vcc_string.c b/varnish-cache/lib/libvcl/vcc_string.c index 005cbb8b..0d9641f1 100644 --- a/varnish-cache/lib/libvcl/vcc_string.c +++ b/varnish-cache/lib/libvcl/vcc_string.c @@ -150,6 +150,9 @@ vcc_StringVal(struct tokenlist *tl) case FLOAT: Fb(tl, 0, "VRT_double_string(sp, %s)", vp->rname); break; + case BACKEND: + Fb(tl, 0, "VRT_backend_string(sp)"); + break; default: vsb_printf(tl->sb, "String representation of '%s' not implemented yet.\n",