[master] 4bbf6df68 Add timeout_idle customization per session

Nils Goroll nils.goroll at uplex.de
Tue Apr 23 12:58:08 UTC 2019


commit 4bbf6df68722dbe3ea77508d160c1dd258ab1d5e
Author: Nils Goroll <nils.goroll at uplex.de>
Date:   Sat Apr 20 12:03:25 2019 +0200

    Add timeout_idle customization per session
    
    includes vtc
    
    Purposes:
    
    * Allow to optimize keepalive for trusted client connections (e.g. CDN,
      authenticated clients)
    
    * Prepare for an optimization to time out connections on inactive VCLs
      earlier (see #2764)
    
    Other client timeouts will be added when accepted.

diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index 67aa4a9a5..a6ade1d39 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -572,9 +572,12 @@ struct sess {
 
 	vtim_real		t_open;		/* fd accepted */
 	vtim_real		t_idle;		/* fd accepted or resp sent */
-
+	vtim_dur		timeout_idle;
 };
 
+#define SESS_TMO(sp, tmo)					\
+	(isnan((sp)->tmo) ? cache_param->tmo : (sp)->tmo)
+
 /* Prototypes etc ----------------------------------------------------*/
 
 
diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index 95696d4eb..875129f68 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -378,6 +378,7 @@ SES_New(struct pool *pp)
 
 	sp->t_open = NAN;
 	sp->t_idle = NAN;
+	sp->timeout_idle = NAN;
 	Lck_New(&sp->mtx, lck_sess);
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	return (sp);
@@ -465,7 +466,7 @@ SES_Wait(struct sess *sp, const struct transport *xp)
 	wp->priv2 = (uintptr_t)xp;
 	wp->idle = sp->t_idle;
 	wp->func = ses_handle;
-	wp->tmo = cache_param->timeout_idle;
+	wp->tmo = SESS_TMO(sp, timeout_idle);
 	if (Wait_Enter(pp->waiter, wp))
 		SES_Delete(sp, SC_PIPE_OVERFLOW, NAN);
 }
diff --git a/bin/varnishd/cache/cache_vrt_var.c b/bin/varnishd/cache/cache_vrt_var.c
index c734e0a5a..8e564fa19 100644
--- a/bin/varnishd/cache/cache_vrt_var.c
+++ b/bin/varnishd/cache/cache_vrt_var.c
@@ -939,3 +939,27 @@ HTTP_VAR(req)
 HTTP_VAR(resp)
 HTTP_VAR(bereq)
 HTTP_VAR(beresp)
+
+/*--------------------------------------------------------------------*/
+
+VCL_VOID
+VRT_l_sess_timeout_idle(VRT_CTX, VCL_DURATION d)
+{
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(ctx->sp, SESS_MAGIC);
+	ctx->sp->timeout_idle = d > 0.0 ? d : 0.0;
+}
+
+VCL_DURATION
+VRT_r_sess_timeout_idle(VRT_CTX)
+{
+	VCL_DURATION d;
+
+	CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
+	CHECK_OBJ_NOTNULL(ctx->sp, SESS_MAGIC);
+
+	d = ctx->sp->timeout_idle;
+	if (isnan(d))
+		return (cache_param->timeout_idle);
+	return (d);
+}
diff --git a/bin/varnishd/http1/cache_http1_fsm.c b/bin/varnishd/http1/cache_http1_fsm.c
index 4f6e8efd1..c289bbbf1 100644
--- a/bin/varnishd/http1/cache_http1_fsm.c
+++ b/bin/varnishd/http1/cache_http1_fsm.c
@@ -331,7 +331,7 @@ HTTP1_Session(struct worker *wrk, struct req *req)
 			hs = HTC_RxStuff(req->htc, HTTP1_Complete,
 			    &req->t_first, &req->t_req,
 			    sp->t_idle + cache_param->timeout_linger,
-			    sp->t_idle + cache_param->timeout_idle,
+			    sp->t_idle + SESS_TMO(sp, timeout_idle),
 			    NAN,
 			    cache_param->http_req_size);
 			AZ(req->htc->ws->r);
diff --git a/bin/varnishd/http2/cache_http2_proto.c b/bin/varnishd/http2/cache_http2_proto.c
index 48ad050c7..7681bfff5 100644
--- a/bin/varnishd/http2/cache_http2_proto.c
+++ b/bin/varnishd/http2/cache_http2_proto.c
@@ -1097,8 +1097,8 @@ h2_rxframe(struct worker *wrk, struct h2_sess *h2)
 	h2->sess->t_idle = VTIM_real();
 	hs = HTC_RxStuff(h2->htc, h2_frame_complete,
 	    NULL, NULL, NAN,
-	    h2->sess->t_idle + cache_param->timeout_idle, NAN,
-	    h2->local_settings.max_frame_size + 9);
+	    h2->sess->t_idle + SESS_TMO(h2->sess, timeout_idle),
+	    NAN, h2->local_settings.max_frame_size + 9);
 	switch (hs) {
 	case HTC_S_COMPLETE:
 		break;
diff --git a/bin/varnishtest/tests/b00067.vtc b/bin/varnishtest/tests/b00067.vtc
index 3f4d6a175..5bb1fee4e 100644
--- a/bin/varnishtest/tests/b00067.vtc
+++ b/bin/varnishtest/tests/b00067.vtc
@@ -3,6 +3,11 @@ varnishtest "Check timeout_idle"
 varnish v1 -arg "-p timeout_idle=1" -vcl {
 	backend dummy { .host = "${bad_ip}"; }
 
+	sub vcl_deliver {
+		if (req.url == "/sess") {
+			set sess.timeout_idle = 2s;
+		}
+	}
 	sub vcl_backend_error {
 		set beresp.status = 200;
 		set beresp.ttl = 1h;
@@ -12,9 +17,19 @@ varnish v1 -arg "-p timeout_idle=1" -vcl {
 client c1 {
 	txreq
 	rxresp
-	delay .2
+	delay 0.2
+	txreq
+	rxresp
+	delay 1.2
+	expect_close
+} -run
+
+client c1 {
+	txreq -url "/sess"
+	rxresp
+	delay 1.2
 	txreq
 	rxresp
-	delay 2
+	delay 2.2
 	expect_close
 } -run
diff --git a/doc/sphinx/reference/vcl_var.rst b/doc/sphinx/reference/vcl_var.rst
index b1e492e06..7474e02fc 100644
--- a/doc/sphinx/reference/vcl_var.rst
+++ b/doc/sphinx/reference/vcl_var.rst
@@ -1250,6 +1250,17 @@ sess.xid	``VCL >= 4.1``
 
 	Unique ID of this session.
 
+sess.timeout_idle
+
+	Type: DURATION
+
+	Readable from: client
+
+	Writable from: client
+
+	Idle timeout for this session, defaults to the
+	``timeout_idle`` parameter, see :ref:`varnishd(1)`
+
 storage
 ~~~~~~~
 


More information about the varnish-commit mailing list