[master] ff37445 Implement new VRT functions for VCL switching

Poul-Henning Kamp phk at FreeBSD.org
Tue Aug 16 11:20:15 CEST 2016


commit ff374453d69f2551cd9d3008c15a7d4d96220ee9
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Aug 16 07:50:18 2016 +0000

    Implement new VRT functions for VCL switching

diff --git a/bin/varnishd/cache/cache_vcl.c b/bin/varnishd/cache/cache_vcl.c
index d857684..edba7b0 100644
--- a/bin/varnishd/cache/cache_vcl.c
+++ b/bin/varnishd/cache/cache_vcl.c
@@ -89,6 +89,23 @@ static struct vcl		*vcl_active; /* protected by vcl_mtx */
 
 /*--------------------------------------------------------------------*/
 
+static struct vcl *
+vcl_find(const char *name)
+{
+	struct vcl *vcl;
+
+	ASSERT_CLI();
+	VTAILQ_FOREACH(vcl, &vcl_head, list) {
+		if (vcl->discard)
+			continue;
+		if (!strcmp(vcl->loaded_name, name))
+			return (vcl);
+	}
+	return (NULL);
+}
+
+/*--------------------------------------------------------------------*/
+
 void
 VCL_Panic(struct vsb *vsb, const struct vcl *vcl)
 {
@@ -155,27 +172,34 @@ VCL_Method_Name(unsigned m)
 /*--------------------------------------------------------------------*/
 
 static void
-VCL_Get(struct vcl **vcc)
+vcl_get(struct vcl **vcc, struct vcl *vcl)
 {
-	while (vcl_active == NULL)
-		(void)usleep(100000);
 
-	CHECK_OBJ_NOTNULL(vcl_active, VCL_MAGIC);
-	assert(vcl_active->temp == VCL_TEMP_WARM);
+	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
+	assert(vcl->temp == VCL_TEMP_WARM);
 	Lck_Lock(&vcl_mtx);
-	AN(vcl_active);
-	if (vcl_active->label == NULL)
-		*vcc = vcl_active;
-	else if (strcmp(vcl_active->state, VCL_TEMP_LABEL))
-		*vcc = vcl_active;
+	AN(vcl);
+	if (vcl->label == NULL)
+		*vcc = vcl;
+	else if (strcmp(vcl->state, VCL_TEMP_LABEL))
+		*vcc = vcl;
 	else
-		*vcc = vcl_active->label;
+		*vcc = vcl->label;
 	AN(*vcc);
 	AZ((*vcc)->discard);
 	(*vcc)->busy++;
 	Lck_Unlock(&vcl_mtx);
 }
 
+static void
+vcl_get_active(struct vcl **vcc)
+{
+	while (vcl_active == NULL)
+		(void)usleep(100000);
+
+	vcl_get(vcc, vcl_active);
+}
+
 void
 VCL_Refresh(struct vcl **vcc)
 {
@@ -185,7 +209,7 @@ VCL_Refresh(struct vcl **vcc)
 		return;
 	if (*vcc != NULL)
 		VCL_Rel(vcc);	/* XXX: optimize locking */
-	VCL_Get(vcc);
+	vcl_get_active(vcc);
 }
 
 void
@@ -411,6 +435,26 @@ VRT_count(VRT_CTX, unsigned u)
 		    ctx->vcl->conf->ref[u].line, ctx->vcl->conf->ref[u].pos);
 }
 
+VCL_VCL
+VRT_vcl_lookup(const char *name)
+{
+	VCL_VCL vcl;
+
+	vcl = vcl_find(name);
+	AN(vcl);
+	return (vcl);
+}
+
+void
+VRT_vcl_select(VRT_CTX, VCL_VCL vcl)
+{
+	struct req *req = ctx->req;
+
+	CHECK_OBJ_NOTNULL(vcl, VCL_MAGIC);
+	VCL_Rel(&req->vcl);
+	vcl_get(&req->vcl, vcl);
+}
+
 struct vclref *
 VRT_ref_vcl(VRT_CTX, const char *desc)
 {
@@ -468,21 +512,6 @@ VRT_rel_vcl(VRT_CTX, struct vclref **refp)
 
 /*--------------------------------------------------------------------*/
 
-static struct vcl *
-vcl_find(const char *name)
-{
-	struct vcl *vcl;
-
-	ASSERT_CLI();
-	VTAILQ_FOREACH(vcl, &vcl_head, list) {
-		if (vcl->discard)
-			continue;
-		if (!strcmp(vcl->loaded_name, name))
-			return (vcl);
-	}
-	return (NULL);
-}
-
 static int
 vcl_setup_event(VRT_CTX, enum vcl_event_e ev)
 {
diff --git a/include/vrt.h b/include/vrt.h
index 9e8f13f..629eb46 100644
--- a/include/vrt.h
+++ b/include/vrt.h
@@ -98,6 +98,7 @@ typedef double					VCL_REAL;
 typedef const struct stevedore *		VCL_STEVEDORE;
 typedef const char *				VCL_STRING;
 typedef double					VCL_TIME;
+typedef struct vcl *				VCL_VCL;
 typedef void					VCL_VOID;
 
 /***********************************************************************
@@ -313,6 +314,10 @@ int VRT_Vmod_Init(struct vmod **hdl, void *ptr, int len, const char *nm,
     const char *path, const char *file_id, VRT_CTX);
 void VRT_Vmod_Fini(struct vmod **hdl);
 
+/* VCL program related */
+VCL_VCL VRT_vcl_lookup(const char *);
+void VRT_vcl_select(VRT_CTX, VCL_VCL);
+
 struct vmod_priv;
 typedef void vmod_priv_free_f(void *);
 struct vmod_priv {
diff --git a/lib/libvcc/vcc_types.c b/lib/libvcc/vcc_types.c
index 1de0725..f6ab02e 100644
--- a/lib/libvcc/vcc_types.c
+++ b/lib/libvcc/vcc_types.c
@@ -150,6 +150,11 @@ const struct type TIME[1] = {{
 	.tostring =		"VRT_TIME_string(ctx, \v1)",
 }};
 
+const struct type VCL[1] = {{
+	.magic =		TYPE_MAGIC,
+	.name =			"VCL",
+}};
+
 const struct type VOID[1] = {{
 	.magic =		TYPE_MAGIC,
 	.name =			"VOID",



More information about the varnish-commit mailing list