[master] 4102ed93b vmod_directors: vmod object destruction vs. director destruction

Nils Goroll nils.goroll at uplex.de
Tue Jun 26 10:05:12 UTC 2018


commit 4102ed93bbb0192fefa7829db208a75c2344fa26
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Tue Jun 26 11:52:34 2018 +0200

    vmod_directors: vmod object destruction vs. director destruction
    
    Turn the director destruction inside-out: When the vmod object goes
    out of scope, we inform VRT that the director is to be
    destroyed. Actual destruction is moved to the respective director
    callback.
    
    This allows directors to outlast vmod objects.
    
    For vdir based directors, the director object is the vmod object; for
    shard, the two are different instances (we might want to reconsider
    changing this later).

diff --git a/lib/libvmod_directors/fall_back.c b/lib/libvmod_directors/fall_back.c
index e4a5a12ff..e6d3e7226 100644
--- a/lib/libvmod_directors/fall_back.c
+++ b/lib/libvmod_directors/fall_back.c
@@ -84,11 +84,22 @@ vmod_fallback_resolve(VRT_CTX, VCL_BACKEND dir)
 	return (be);
 }
 
+static void v_matchproto_(vdi_destroy_f)
+vmod_fallback_destroy(VCL_BACKEND dir)
+{
+	struct vmod_directors_fallback *fallback;
+
+	CAST_OBJ_NOTNULL(fallback, dir->priv, VMOD_DIRECTORS_FALLBACK_MAGIC);
+	vdir_delete(&fallback->vd);
+	FREE_OBJ(fallback);
+}
+
 static const struct vdi_methods vmod_fallback_methods[1] = {{
 	.magic =		VDI_METHODS_MAGIC,
 	.type =			"fallback",
 	.healthy =		vmod_fallback_healthy,
 	.resolve =		vmod_fallback_resolve,
+	.destroy =		vmod_fallback_destroy
 }};
 
 
@@ -113,11 +124,8 @@ vmod_fallback__fini(struct vmod_directors_fallback **fbp)
 {
 	struct vmod_directors_fallback *fb;
 
-	fb = *fbp;
-	*fbp = NULL;
-	CHECK_OBJ_NOTNULL(fb, VMOD_DIRECTORS_FALLBACK_MAGIC);
-	vdir_delete(&fb->vd);
-	FREE_OBJ(fb);
+	TAKE_OBJ_NOTNULL(fb, fbp, VMOD_DIRECTORS_FALLBACK_MAGIC);
+	VRT_DelDirector(&fb->vd->dir);
 }
 
 VCL_VOID v_matchproto_()
diff --git a/lib/libvmod_directors/hash.c b/lib/libvmod_directors/hash.c
index 9a362cb94..a82e19fe3 100644
--- a/lib/libvmod_directors/hash.c
+++ b/lib/libvmod_directors/hash.c
@@ -46,9 +46,20 @@ struct vmod_directors_hash {
 	struct vdir				*vd;
 };
 
+static void v_matchproto_(vdi_destroy_f)
+vmod_hash_destroy(VCL_BACKEND dir)
+{
+	struct vmod_directors_hash *rr;
+
+	CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_HASH_MAGIC);
+	vdir_delete(&rr->vd);
+	FREE_OBJ(rr);
+}
+
 static const struct vdi_methods vmod_hash_methods[1] = {{
 	.magic =		VDI_METHODS_MAGIC,
 	.type =			"hash",
+	.destroy =		vmod_hash_destroy
 }};
 
 
@@ -72,11 +83,8 @@ vmod_hash__fini(struct vmod_directors_hash **rrp)
 {
 	struct vmod_directors_hash *rr;
 
-	rr = *rrp;
-	*rrp = NULL;
-	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_HASH_MAGIC);
-	vdir_delete(&rr->vd);
-	FREE_OBJ(rr);
+	TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_HASH_MAGIC);
+	VRT_DelDirector(&rr->vd->dir);
 }
 
 VCL_VOID v_matchproto_()
diff --git a/lib/libvmod_directors/random.c b/lib/libvmod_directors/random.c
index 3968e5cf0..5ad0849e9 100644
--- a/lib/libvmod_directors/random.c
+++ b/lib/libvmod_directors/random.c
@@ -72,11 +72,22 @@ vmod_random_resolve(VRT_CTX, VCL_BACKEND dir)
 	return (be);
 }
 
+static void v_matchproto_(vdi_destroy_f)
+vmod_random_destroy(VCL_BACKEND dir)
+{
+	struct vmod_directors_random *random;
+
+	CAST_OBJ_NOTNULL(random, dir->priv, VMOD_DIRECTORS_RANDOM_MAGIC);
+	vdir_delete(&random->vd);
+	FREE_OBJ(random);
+}
+
 static const struct vdi_methods vmod_random_methods[1] = {{
 	.magic =		VDI_METHODS_MAGIC,
 	.type =			"random",
 	.healthy =		vmod_random_healthy,
 	.resolve =		vmod_random_resolve,
+	.destroy =		vmod_random_destroy
 }};
 
 
@@ -100,11 +111,8 @@ vmod_random__fini(struct vmod_directors_random **rrp)
 {
 	struct vmod_directors_random *rr;
 
-	rr = *rrp;
-	*rrp = NULL;
-	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_RANDOM_MAGIC);
-	vdir_delete(&rr->vd);
-	FREE_OBJ(rr);
+	TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_RANDOM_MAGIC);
+	VRT_DelDirector(&rr->vd->dir);
 }
 
 VCL_VOID v_matchproto_()
diff --git a/lib/libvmod_directors/round_robin.c b/lib/libvmod_directors/round_robin.c
index 636d88ae7..af2aec3b8 100644
--- a/lib/libvmod_directors/round_robin.c
+++ b/lib/libvmod_directors/round_robin.c
@@ -81,11 +81,22 @@ vmod_rr_resolve(VRT_CTX, VCL_BACKEND dir)
 	return (be);
 }
 
+static void v_matchproto_(vdi_destroy_f)
+vmod_rr_destroy(VCL_BACKEND dir)
+{
+	struct vmod_directors_round_robin *rr;
+
+	CAST_OBJ_NOTNULL(rr, dir->priv, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
+	vdir_delete(&rr->vd);
+	FREE_OBJ(rr);
+}
+
 static const struct vdi_methods vmod_rr_methods[1] = {{
 	.magic =		VDI_METHODS_MAGIC,
 	.type =			"round-robin",
 	.healthy =		vmod_rr_healthy,
 	.resolve =		vmod_rr_resolve,
+	.destroy =		vmod_rr_destroy
 }};
 
 VCL_VOID v_matchproto_()
@@ -108,11 +119,8 @@ vmod_round_robin__fini(struct vmod_directors_round_robin **rrp)
 {
 	struct vmod_directors_round_robin *rr;
 
-	rr = *rrp;
-	*rrp = NULL;
-	CHECK_OBJ_NOTNULL(rr, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
-	vdir_delete(&rr->vd);
-	FREE_OBJ(rr);
+	TAKE_OBJ_NOTNULL(rr, rrp, VMOD_DIRECTORS_ROUND_ROBIN_MAGIC);
+	VRT_DelDirector(&rr->vd->dir);
 }
 
 VCL_VOID v_matchproto_()
diff --git a/lib/libvmod_directors/vdir.c b/lib/libvmod_directors/vdir.c
index ee6b27896..61f78139e 100644
--- a/lib/libvmod_directors/vdir.c
+++ b/lib/libvmod_directors/vdir.c
@@ -75,10 +75,10 @@ vdir_delete(struct vdir **vdp)
 
 	TAKE_OBJ_NOTNULL(vd, vdp, VDIR_MAGIC);
 
+	AZ(vd->dir);
 	free(vd->backend);
 	free(vd->weight);
 	AZ(pthread_rwlock_destroy(&vd->mtx));
-	VRT_DelDirector(&vd->dir);
 	vbit_destroy(vd->vbm);
 	FREE_OBJ(vd);
 }
diff --git a/lib/libvmod_directors/vmod_shard.c b/lib/libvmod_directors/vmod_shard.c
index 6dacf20e6..558675335 100644
--- a/lib/libvmod_directors/vmod_shard.c
+++ b/lib/libvmod_directors/vmod_shard.c
@@ -227,11 +227,21 @@ shard__assert(void)
 	assert(t2a == t2b);
 }
 
+static void v_matchproto_(vdi_destroy_f)
+vmod_shard_destroy(VCL_BACKEND dir)
+{
+	struct sharddir *shardd;
+
+	CAST_OBJ_NOTNULL(shardd, dir->priv, SHARDDIR_MAGIC);
+	sharddir_delete(&shardd);
+}
+
 static const struct vdi_methods vmod_shard_methods[1] = {{
 	.magic =	VDI_METHODS_MAGIC,
 	.type =		"shard",
 	.resolve =	vmod_shard_resolve,
 	.healthy =	vmod_shard_healthy,
+	.destroy =	vmod_shard_destroy
 }};
 
 
@@ -261,7 +271,6 @@ vmod_shard__fini(struct vmod_directors_shard **vshardp)
 	struct vmod_directors_shard *vshard;
 
 	TAKE_OBJ_NOTNULL(vshard, vshardp, VMOD_SHARD_SHARD_MAGIC);
-	sharddir_delete(&vshard->shardd);
 	VRT_DelDirector(&vshard->dir);
 	FREE_OBJ(vshard);
 }


More information about the varnish-commit mailing list