[4.1] cdd5fda Hold a VCL reference in the debug vmod

Lasse Karstensen lkarsten at varnish-software.com
Thu Jan 14 15:15:08 CET 2016


commit cdd5fdabe88c34162f959cc85dc0adcaa26ef596
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Sat Dec 5 16:09:14 2015 +0100

    Hold a VCL reference in the debug vmod
    
    By default the reference is released as soon as the VCL goes cold, which
    doesn't harm the existing tests yet allows to cover the new VRT_ref_vcl
    and VRT_rel_vcl functions.
    
    It is possible to ask the vmod to release the reference after a delay.

diff --git a/lib/libvmod_debug/vmod.vcc b/lib/libvmod_debug/vmod.vcc
index dad36ec..42159fb 100644
--- a/lib/libvmod_debug/vmod.vcc
+++ b/lib/libvmod_debug/vmod.vcc
@@ -138,3 +138,7 @@ Mark a workspace as overflowed.
 $Function INT workspace_free(ENUM { client, backend, session, thread })
 
 Find how much unallocated space there is left in a workspace.
+
+$Function VOID vcl_release_delay(DURATION)
+
+Hold a reference to the VCL when it goes cold for the given delay.
diff --git a/lib/libvmod_debug/vmod_debug.c b/lib/libvmod_debug/vmod_debug.c
index 9b3449c..91b49c7 100644
--- a/lib/libvmod_debug/vmod_debug.c
+++ b/lib/libvmod_debug/vmod_debug.c
@@ -28,6 +28,7 @@
 
 #include "config.h"
 
+#include <pthread.h>
 #include <stdlib.h>
 #include <stdio.h>
 
@@ -46,6 +47,8 @@ struct priv_vcl {
 	uintptr_t		exp_cb;
 };
 
+VCL_DURATION vcl_release_delay = 0.0;
+
 VCL_VOID __match_proto__(td_debug_panic)
 vmod_panic(VRT_CTX, const char *str, ...)
 {
@@ -280,14 +283,38 @@ event_warm(VRT_CTX)
 		return (-1);
 	}
 
+	VRT_ref_vcl(ctx);
 	return (0);
 }
 
+static void*
+cooldown_thread(void *priv)
+{
+	struct vrt_ctx ctx;
+
+	AN(priv);
+	INIT_OBJ(&ctx, VRT_CTX_MAGIC);
+	ctx.vcl = (struct vcl*)priv;
+
+	VTIM_sleep(vcl_release_delay);
+	VRT_rel_vcl(&ctx);
+	return (NULL);
+}
+
 static int
 event_cold(VRT_CTX)
 {
+	pthread_t thread;
 
 	VSL(SLT_Debug, 0, "%s: VCL_EVENT_COLD", VCL_Name(ctx->vcl));
+
+	if (vcl_release_delay == 0.0) {
+		VRT_rel_vcl(ctx);
+		return (0);
+	}
+
+	AZ(pthread_create(&thread, NULL, cooldown_thread, ctx->vcl));
+	AZ(pthread_detach(thread));
 	return (0);
 }
 
@@ -379,3 +406,12 @@ vmod_workspace_overflow(VRT_CTX, VCL_ENUM which)
 
 	WS_MarkOverflow(ws);
 }
+
+void
+vmod_vcl_release_delay(VRT_CTX, VCL_DURATION delay)
+{
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	assert(delay > 0.0);
+	vcl_release_delay = delay;
+}



More information about the varnish-commit mailing list