[master] c7b96c31b add directors.lookup()

Nils Goroll nils.goroll at uplex.de
Tue Feb 26 14:42:03 UTC 2019


commit c7b96c31b1a0db1a54eaabc4b0ec0b6d27fb0da1
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue Feb 26 15:38:18 2019 +0100

    add directors.lookup()

diff --git a/bin/varnishtest/tests/b00016.vtc b/bin/varnishtest/tests/b00016.vtc
index 80d28d8cb..af3627f52 100644
--- a/bin/varnishtest/tests/b00016.vtc
+++ b/bin/varnishtest/tests/b00016.vtc
@@ -6,7 +6,12 @@ server s1 -repeat 2 -keepalive {
 } -start
 
 varnish v1 -vcl+backend {
+	import directors;
+
 	sub vcl_recv {
+		if (req.url == "/lookup") {
+			set req.http.foo = directors.lookup("s1");
+		}
 		return (pass);
 	}
 
@@ -19,6 +24,10 @@ client c1 {
 	txreq -url "/"
 	rxresp
 	expect resp.http.X-Backend-Name == "s1"
+	txreq -url "/lookup"
+	rxresp
+	expect resp.status == 503
+	expect resp.reason == "VCL failed"
 } -run
 
 varnish v1 -vcl+backend {
@@ -26,7 +35,7 @@ varnish v1 -vcl+backend {
 
 	sub vcl_init {
 		new bar = directors.random();
-		bar.add_backend(s1, 1);
+		bar.add_backend(directors.lookup("s1"), 1);
 	}
 
 	sub vcl_recv {
diff --git a/doc/changes.rst b/doc/changes.rst
index 55177809a..7173b6316 100644
--- a/doc/changes.rst
+++ b/doc/changes.rst
@@ -104,6 +104,11 @@ VCL
 * Added ``req.is_hitmiss`` and ``req.is_hitpass`` (2743_)
 
 
+bundled vmods
+-------------
+
+* Added ``directors.lookup()``
+
 bundled tools
 -------------
 
diff --git a/lib/libvmod_directors/Makefile.am b/lib/libvmod_directors/Makefile.am
index f4a59e2a5..706e24a30 100644
--- a/lib/libvmod_directors/Makefile.am
+++ b/lib/libvmod_directors/Makefile.am
@@ -5,6 +5,7 @@ libvmod_directors_la_SOURCES = \
 	vdir.h \
 	fall_back.c \
 	hash.c \
+	misc.c \
 	random.c \
 	round_robin.c \
 	vmod_shard.c \
diff --git a/lib/libvmod_directors/misc.c b/lib/libvmod_directors/misc.c
new file mode 100644
index 000000000..c72799558
--- /dev/null
+++ b/lib/libvmod_directors/misc.c
@@ -0,0 +1,47 @@
+/*-
+ * Copyright 2019 UPLEX - Nils Goroll Systemoptimierung
+ * All rights reserved.
+ *
+ * Author: Nils Goroll <nils.goroll at uplex.de>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#include "vdef.h"
+#include "vrt.h"
+#include "vcl.h"
+
+#include "vcc_if.h"
+
+VCL_BACKEND
+VPFX(lookup)(VRT_CTX, VCL_STRING name)
+{
+	if ((ctx->method & VCL_MET_TASK_H) == 0) {
+		VRT_fail(ctx,
+		    "lookup() may only be called from vcl_init / vcl_fini");
+		return (NULL);
+	}
+
+	return (VRT_LookupDirector(ctx, name));
+}
diff --git a/lib/libvmod_directors/vmod.vcc b/lib/libvmod_directors/vmod.vcc
index 1727f1f67..f30f2b2a1 100644
--- a/lib/libvmod_directors/vmod.vcc
+++ b/lib/libvmod_directors/vmod.vcc
@@ -687,6 +687,12 @@ This method may only be used in backend context.
 For use with the `param` argument of `vmod_directors.shard.backend`_ to associate
 this shard parameter set with a shard director.
 
+$Function BACKEND lookup(STRING)
+
+Lookup a backend by its name.
+
+This function can only be used from ``vcl_init{}`` and  ``vcl_fini{}``.
+
 ACKNOWLEDGEMENTS
 ================
 


More information about the varnish-commit mailing list