On 10/28/2015 09:17 AM, Poul-Henning Kamp wrote:
> 
>>> My current thinking is that we'll name the backend whatever the 
>>> user/vcl/vmod writer likes (ie:  Backend name), and deal with the 
>>> fall-out.
>>
>> At the very least, VMODs should have a way to discover if a backend
>> name is already in use by a VCL -- the VCL_HasBackend(vcl,name) idea.
>> Then a VMOD can choose to raise an error for duplicate names.
> 
> Agreed.

This patch adds VCL_HasBackend().


Best,
Geoff
-- 
** * * UPLEX - Nils Goroll Systemoptimierung

Scheffelstraße 32
22301 Hamburg

Tel +49 40 2880 5731
Mob +49 176 636 90917
Fax +49 40 42949753

http://uplex.de

From 7172c2273d2db45e8cbe02c8ddf5e62f43e8943d Mon Sep 17 00:00:00 2001
From: Geoff Simmons <ge...@uplex.de>
Date: Sun, 1 Nov 2015 12:16:29 +0100
Subject: [PATCH] add VCL_HasBackend()

---
 bin/varnishd/cache/cache_backend.h |  1 +
 bin/varnishd/cache/cache_vcl.c     | 25 +++++++++++++++++++++++
 bin/varnishtest/tests/d00014.vtc   | 42 ++++++++++++++++++++++++++++++++++++++
 lib/libvmod_debug/vmod.vcc         |  4 ++++
 lib/libvmod_debug/vmod_debug_dyn.c |  8 ++++++++
 5 files changed, 80 insertions(+)
 create mode 100644 bin/varnishtest/tests/d00014.vtc

diff --git a/bin/varnishd/cache/cache_backend.h b/bin/varnishd/cache/cache_backend.h
index 17b90f0..db085d8 100644
--- a/bin/varnishd/cache/cache_backend.h
+++ b/bin/varnishd/cache/cache_backend.h
@@ -132,3 +132,4 @@ void VBT_Wait(struct worker *, struct vbc *);
 /* cache_vcl.c */
 void VCL_AddBackend(struct vcl *, struct backend *);
 void VCL_DelBackend(struct backend *);
+unsigned VCL_HasBackend(struct vcl *, const char *);
diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index c747ead..28125b6 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -224,6 +224,31 @@ VCL_DelBackend(struct backend *be)
 		VBE_Event(be, VCL_EVENT_COLD);
 }
 
+unsigned
+VCL_HasBackend(struct vcl *vcl, const char *name)
+{
+	struct backend *be;
+	struct vsb *vsb;
+	unsigned hasname = 0;
+
+	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
+	AN(name);
+
+	vsb = VSB_new_auto();
+	AN(vsb);
+	VSB_printf(vsb, "%s.%s", VCL_Name(vcl), name);
+	AZ(VSB_finish(vsb));
+
+	VTAILQ_FOREACH(be, &vcl->backend_list, vcl_list)
+		if (strcmp(be->display_name, VSB_data(vsb)) == 0) {
+			hasname = 1;
+			break;
+		}
+
+	VSB_delete(vsb);
+	return hasname;
+}
+
 static void
 vcl_BackendEvent(const struct vcl *vcl, enum vcl_event_e e)
 {
diff --git a/bin/varnishtest/tests/d00014.vtc b/bin/varnishtest/tests/d00014.vtc
new file mode 100644
index 0000000..cc2715b
--- /dev/null
+++ b/bin/varnishtest/tests/d00014.vtc
@@ -0,0 +1,42 @@
+varnishtest "Test checking for backend names in a VCL"
+
+varnish v1 -vcl {
+	import ${vmod_debug};
+
+	backend dummy { .host = "${bad_ip}"; }
+
+	sub vcl_recv {
+		if (debug.has_backend("dummy")) {
+			return(synth(200));
+		}
+		else {
+			return(synth(500));
+		}
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 200
+} -run
+
+varnish v1 -vcl {
+	import ${vmod_debug};
+
+	backend bigdummy { .host = "${bad_ip}"; }
+
+	sub vcl_recv {
+		if (debug.has_backend("dummy")) {
+			return(synth(503));
+		}
+		elsif (debug.has_backend("bigdummy")) {
+			return(synth(200));
+		}
+		else {
+			return(synth(500));
+		}
+	}
+}
+
+client c1 -run
diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index dad36ec..7f31f73 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -123,6 +123,10 @@ $Method VOID .refresh(STRING addr, STRING port)
 
 Dynamically refresh & (always!) replace the backend by a new one.
 
+$Function BOOL has_backend(STRING name)
+
+True if a backend with this name exists in the present VCL.
+
 $Function VOID workspace_allocate(ENUM { client, backend, session, thread }, INT SIZE)
 
 Allocate and zero out SIZE bytes from a workspace.
diff --git a/lib/libvmod_debug/vmod_debug_dyn.c b/lib/libvmod_debug/vmod_debug_dyn.c
index 7e4afea..1a3e5b7 100644
--- a/lib/libvmod_debug/vmod_debug_dyn.c
+++ b/lib/libvmod_debug/vmod_debug_dyn.c
@@ -163,3 +163,11 @@ vmod_dyn_refresh(VRT_CTX, struct vmod_debug_dyn *dyn,
 	CHECK_OBJ_NOTNULL(dyn, VMOD_DEBUG_DYN_MAGIC);
 	dyn_dir_init(ctx, dyn, addr, port);
 }
+
+VCL_BOOL __match_proto__()
+vmod_has_backend(VRT_CTX, VCL_STRING name)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	AN(name);
+	return (VCL_HasBackend(ctx->vcl, name));
+}
-- 
2.1.4

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
varnish-dev mailing list
varnish-dev@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to