[master] 396460f75 vcl: Expose global parameters to VCL

Dridi Boukelmoune dridi.boukelmoune at gmail.com
Wed Apr 30 14:49:04 UTC 2025


commit 396460f759e20a90e7af47b09a6f9809a74e980a
Author: Dridi Boukelmoune <dridi.boukelmoune at gmail.com>
Date:   Wed Apr 30 14:52:53 2025 +0200

    vcl: Expose global parameters to VCL
    
    The goal is to allow constructs avoiding hard-coded values when they
    could be relative to existing parameters.
    
    Let's take the first one in the list for example, backend_idle_timeout.
    In a clustered deployment, with our without multiple tiers, this timeout
    can be used to increase timeout_idle for a fellow Varnish server in a
    way that allows it to pool its connections:
    
        sub vcl_recv {
            if (req.http.via ~ "varnish") {
                set sess.timeout_idle = param.backend_idle_timeout * 1.5;
            }
        }
    
    One would obviously have a more reliable way of identifying a member of
    the cluster, but assuming a similar configuration across the cluster,
    this enables adaptive timeouts without resorting to VCL reloads or VMODs
    to access parameters.

diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index 778c9a42f..ca4f765ee 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -1178,3 +1178,34 @@ SESS_VAR_DUR(timeout_idle, )
 SESS_VAR_DUR(timeout_linger, )
 SESS_VAR_DUR(send_timeout, )
 SESS_VAR_DUR(idle_send_timeout, set_idle_send_timeout(ctx->sp, d))
+
+/*--------------------------------------------------------------------*/
+
+#define PARAM_VAR(x, type)				\
+VCL_##type						\
+VRT_r_param_##x(VRT_CTX)				\
+{							\
+							\
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);		\
+	return (cache_param->x);			\
+}							\
+
+PARAM_VAR(backend_idle_timeout, DURATION)
+PARAM_VAR(backend_wait_limit, INT)
+PARAM_VAR(backend_wait_timeout, DURATION)
+PARAM_VAR(between_bytes_timeout, DURATION)
+PARAM_VAR(connect_timeout, DURATION)
+PARAM_VAR(default_grace, DURATION)
+PARAM_VAR(default_keep, DURATION)
+PARAM_VAR(default_ttl, DURATION)
+PARAM_VAR(first_byte_timeout, DURATION)
+PARAM_VAR(idle_send_timeout, DURATION)
+PARAM_VAR(max_esi_depth, INT)
+PARAM_VAR(max_restarts, INT)
+PARAM_VAR(max_retries, INT)
+PARAM_VAR(pipe_task_deadline, DURATION)
+PARAM_VAR(pipe_timeout, DURATION)
+PARAM_VAR(send_timeout, DURATION)
+PARAM_VAR(shortlived, DURATION)
+PARAM_VAR(timeout_idle, DURATION)
+PARAM_VAR(transit_buffer, BYTES)
diff --git a/bin/varnishtest/tests/c00136.vtc b/bin/varnishtest/tests/c00136.vtc
new file mode 100644
index 000000000..0e97be57f
--- /dev/null
+++ b/bin/varnishtest/tests/c00136.vtc
@@ -0,0 +1,34 @@
+varnishtest "Rearm timeout_idle upon HTTP/1 keep-alive"
+
+barrier b1 cond 2
+barrier b2 cond 2
+
+varnish v1 -cliok "param.set timeout_idle 1h"
+varnish v1 -vcl {
+	backend be none;
+	sub vcl_recv {
+		set sess.timeout_idle = param.timeout_idle;
+		return (synth(200));
+	}
+} -start
+
+client c1 {
+	txreq
+	rxresp
+	expect resp.status == 200
+
+	barrier b1 sync
+	barrier b2 sync
+
+	txreq
+	rxresp
+	expect resp.status == 200
+
+	expect_close
+} -start
+
+barrier b1 sync
+varnish v1 -cliok "param.set timeout_idle 1ms"
+barrier b2 sync
+
+client c1 -wait
diff --git a/doc/sphinx/reference/vcl_var.rst b/doc/sphinx/reference/vcl_var.rst
index f59abf263..c13402c7d 100644
--- a/doc/sphinx/reference/vcl_var.rst
+++ b/doc/sphinx/reference/vcl_var.rst
@@ -1948,3 +1948,164 @@ storage.<name>.used_space
 	Used space in the named stevedore. Only available for the malloc
 	stevedore.
 
+Runtime parameters
+------------------
+
+Some of the global runtime parameters related to VCL transactions are
+readable anywhere in VCL. The value of a parameter can change between
+accesses, it is not cached for the duration of a VCL task.
+
+.. TODO: sphinx references to param docs.
+
+param.backend_idle_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter backend_idle_timeout.
+
+param.backend_wait_limit
+
+	Type: INT
+
+	Readable from: all
+
+	Global parameter backend_wait_limit.
+
+param.backend_wait_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter backend_wait_timeout.
+
+param.between_bytes_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter between_bytes_timeout.
+
+param.connect_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter connect_timeout.
+
+param.default_grace
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter default_grace.
+
+param.default_keep
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter default_keep.
+
+param.default_ttl
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter default_ttl.
+
+param.first_byte_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter first_byte_timeout.
+
+param.idle_send_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter idle_send_timeout.
+
+param.max_esi_depth
+
+	Type: INT
+
+	Readable from: all
+
+	Global parameter max_esi_depth.
+
+param.max_restarts
+
+	Type: INT
+
+	Readable from: all
+
+	Global parameter max_restarts.
+
+param.max_retries
+
+	Type: INT
+
+	Readable from: all
+
+	Global parameter max_retries.
+
+param.pipe_task_deadline
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter pipe_task_deadline.
+
+param.pipe_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter pipe_timeout.
+
+param.send_timeout
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter send_timeout.
+
+param.shortlived
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter shortlived.
+
+param.timeout_idle
+
+	Type: DURATION
+
+	Readable from: all
+
+	Global parameter timeout_idle.
+
+param.transit_buffer
+
+	Type: BYTES
+
+	Readable from: all
+
+	Global parameter transit_buffer.
+


More information about the varnish-commit mailing list