[master] b51bc4a78 Warn about too many (>100) VCLs.

Poul-Henning Kamp phk at FreeBSD.org
Wed Aug 15 12:39:08 UTC 2018


commit b51bc4a787709d340d6753aa02025837183371c0
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed Aug 15 12:37:22 2018 +0000

    Warn about too many (>100) VCLs.
    
    Two new paramters allow the limit and than handling (ignore, warn, fail)
    to be configured.
    
    Fixes #2713

diff --git a/bin/varnishd/mgt/mgt_vcl.c b/bin/varnishd/mgt/mgt_vcl.c
index 5974b589a..2b0e88cf1 100644
--- a/bin/varnishd/mgt/mgt_vcl.c
+++ b/bin/varnishd/mgt/mgt_vcl.c
@@ -52,6 +52,7 @@ static const char * const VCL_STATE_COLD = "cold";
 static const char * const VCL_STATE_WARM = "warm";
 static const char * const VCL_STATE_AUTO = "auto";
 static const char * const VCL_STATE_LABEL = "label";
+static int vcl_count;
 
 struct vclprog;
 struct vmodfile;
@@ -234,6 +235,8 @@ mgt_vcl_add(const char *name, const char *state)
 		vp->warm = 1;
 
 	VTAILQ_INSERT_TAIL(&vclhead, vp, list);
+	if (vp->state != VCL_STATE_LABEL)
+		vcl_count++;
 	return (vp);
 }
 
@@ -251,6 +254,8 @@ mgt_vcl_del(struct vclprog *vp)
 		mgt_vcl_dep_del(VTAILQ_FIRST(&vp->dfrom));
 
 	VTAILQ_REMOVE(&vclhead, vp, list);
+	if (vp->state != VCL_STATE_LABEL)
+		vcl_count--;
 	if (vp->fname != NULL) {
 		AZ(unlink(vp->fname));
 		p = strrchr(vp->fname, '/');
@@ -465,6 +470,12 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
 	if (C_flag) {
 		bprintf(buf, ".CflagTest.%d", (int)getpid());
 		vclname = buf;
+	} else if (vcl_count >= mgt_param.max_vcl &&
+	    mgt_param.max_vcl_handling == 2) {
+		VCLI_Out(cli, "Too many (%d) VCLs already loaded\n", vcl_count);
+		VCLI_Out(cli, "(See max_vcl and max_vcl_handling parameters)");
+		VCLI_SetResult(cli, CLIS_CANT);
+		return;
 	}
 
 	if (state == NULL)
@@ -494,6 +505,14 @@ mgt_new_vcl(struct cli *cli, const char *vclname, const char *vclsrc,
 	if (active_vcl == NULL)
 		active_vcl = vp;
 
+	if (cli->result == CLIS_OK &&
+	    vcl_count > mgt_param.max_vcl &&
+	    mgt_param.max_vcl_handling == 1) {
+		VCLI_Out(cli, "%d VCLs loaded\n", vcl_count);
+		VCLI_Out(cli, "Remember to vcl.discard the old/unused VCLs.\n");
+		VCLI_Out(cli, "(See max_vcl and max_vcl_handling parameters)");
+	}
+
 	if (!MCH_Running())
 		return;
 
diff --git a/bin/varnishtest/tests/v00025.vtc b/bin/varnishtest/tests/v00025.vtc
index 699137b0f..d3d0cf684 100644
--- a/bin/varnishtest/tests/v00025.vtc
+++ b/bin/varnishtest/tests/v00025.vtc
@@ -7,7 +7,23 @@ server s1 {
 	txresp
 } -start
 
-varnish v1 -syntax 4.0 -arg "-i J.F.Nobody" -vcl+backend {
+# Test max_vcl param
+varnish v1 -arg "-i J.F.Nobody" -vcl+backend { } -start
+
+varnish v1 -cliok "param.set max_vcl 2"
+varnish v1 -cliok "param.set max_vcl_handling 2"
+
+varnish v1 -vcl+backend { }
+varnish v1 -errvcl {Too many (2) VCLs already loaded} {}
+
+varnish v1 -cliok "param.set max_vcl_handling 1"
+
+varnish v1 -cliexpect "Remember to vcl.discard the old/unused VCLs." \
+	{vcl.inline foobar "vcl 4.1; backend b1 {.host=\"${s1_addr}\";}"}
+
+varnish v1 -cliok "param.set max_vcl 100"
+
+varnish v1 -syntax 4.0 -vcl+backend {
 
 	import std;
 	import directors;
@@ -78,7 +94,7 @@ varnish v1 -syntax 4.0 -arg "-i J.F.Nobody" -vcl+backend {
 		set bereq.connect_timeout = 10s;
 	}
 
-} -start
+}
 
 client c1 {
 	txreq
diff --git a/include/tbl/params.h b/include/tbl/params.h
index 054b2f449..e583b3977 100644
--- a/include/tbl/params.h
+++ b/include/tbl/params.h
@@ -1519,6 +1519,38 @@ PARAM(
 	/* func */	NULL
 )
 
+PARAM(
+	/* name */	max_vcl_handling,
+	/* typ */	uint,
+	/* min */	"0",
+	/* max */	"2",
+	/* default */	"1",
+	/* units */	NULL,
+	/* flags */	0,
+	/* s-text */
+	"Behaviour when attempting to exceed max_vcl loaded VCL.\n"
+	"\n*  0 - Ignore max_vcl parameter.\n"
+	"\n*  1 - Issue warning.\n"
+	"\n*  2 - Refuse loading VCLs.",
+	/* l-text */	"",
+	/* func */	NULL
+)
+
+PARAM(
+	/* name */	max_vcl,
+	/* typ */	uint,
+	/* min */	"0",
+	/* max */	NULL,
+	/* default */	"100",
+	/* units */	NULL,
+	/* flags */	0,
+	/* s-text */
+	"Threshold of loaded VCL programs.  (VCL labels are not counted.)"
+	"  Parameter max_vcl_handling determines behaviour.",
+	/* l-text */	"",
+	/* func */	NULL
+)
+
 PARAM(
 	/* name */	vsm_free_cooldown,
 	/* typ */	timeout,


More information about the varnish-commit mailing list