Commit a21746d2 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Unify .grace and .ttl VCL variable handling

parent 3101d97c
......@@ -633,6 +633,11 @@ extern pthread_t cli_thread;
#define ASSERT_CLI() do {assert(pthread_self() == cli_thread);} while (0)
/* cache_expiry.c */
double EXP_Get_grace(const struct exp *e);
double EXP_Get_ttl(const struct exp *e);
void EXP_Set_grace(struct exp *e, double v);
void EXP_Set_ttl(struct exp *e, double v);
double EXP_Grace(double g);
void EXP_Insert(struct object *o);
void EXP_Inject(struct objcore *oc, struct lru *lru, double when);
......
......@@ -59,8 +59,47 @@ static struct lock exp_mtx;
/*--------------------------------------------------------------------
* struct exp manipulations
*
* The Get/Set functions encapsulate the mutual magic between the
* fields in one single place.
*/
void
EXP_Set_grace(struct exp *e, double v)
{
if (v > 0.)
e->grace = v;
else
e->grace = NAN;
}
double
EXP_Get_grace(const struct exp *e)
{
return (e->grace > 0. ? e->grace : -1.);
}
void
EXP_Set_ttl(struct exp *e, double v)
{
if (v > 0.)
e->ttl = v;
else {
e->ttl = -1.;
e->grace = NAN;
}
}
double
EXP_Get_ttl(const struct exp *e)
{
return (e->ttl > 0. ? e->ttl : -1.);
}
double
EXP_Grace(double g)
{
......
......@@ -363,105 +363,32 @@ VRT_r_req_restarts(const struct sess *sp)
return (sp->restarts);
}
/*--------------------------------------------------------------------
* ttl and grace have special magical relationships, encapsulated in
* cache_expire.c
*/
void
VRT_l_obj_ttl(const struct sess *sp, double a)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC); /* XXX */
if (sp->obj->objcore == NULL)
return;
WSP(sp, SLT_TTL, "%u VCL %.0f %.0f",
sp->obj->xid, a, sp->t_req);
/*
* If people set obj.ttl = 0s, they don't expect it to be cacheable
* any longer, but it will still be for up to 1s - epsilon because
* of the rounding to seconds.
* We special case and make sure that rounding does not surprise.
*/
if (a <= 0) {
sp->obj->exp.ttl = -1.;
sp->obj->exp.grace = 0.;
} else
sp->obj->exp.ttl = a;
EXP_Rearm(sp->obj);
}
double
VRT_r_obj_ttl(const struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
CHECK_OBJ_NOTNULL(sp->obj, OBJECT_MAGIC); /* XXX */
if (sp->obj->objcore == NULL)
return (0.0);
return (sp->obj->exp.ttl);
}
/*--------------------------------------------------------------------
* XXX: Working relative to t_req is maybe not the right thing, we could
* XXX: have spent a long time talking to the backend since then.
* XXX: It might make sense to cache a timestamp as "current time"
* XXX: before vcl_recv (== t_req) and vcl_fetch.
* XXX: On the other hand, that might lead to inconsistent behaviour
* XXX: where an object expires while we are running VCL code, and
* XXX: and that may not be a good idea either.
* XXX: See also related t_req use in cache_hash.c
*/
void
VRT_l_beresp_ttl(const struct sess *sp, double a)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
WSP(sp, SLT_TTL, "%u VCL %.0f %.0f", sp->xid, a, sp->t_req);
/*
* If people set obj.ttl = 0s, they don't expect it to be cacheable
* any longer, but it will still be for up to 1s - epsilon because
* of the rounding to seconds.
* We special case and make sure that rounding does not surprise.
*/
if (a <= 0) {
sp->wrk->exp.ttl = -1.;
sp->wrk->exp.grace = 0.;
} else
sp->wrk->exp.ttl = a;
}
double
VRT_r_beresp_ttl(const struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
return (sp->wrk->exp.ttl);
}
/*--------------------------------------------------------------------*/
#define VRT_DO_EXP(which, exp, fld, extra) \
\
void __match_proto__() \
VRT_l_##which##_grace(struct sess *sp, double a) \
VRT_l_##which##_##fld(struct sess *sp, double a) \
{ \
\
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); \
exp.fld = a >= 0.0 ? a : NAN; \
EXP_Set_##fld(&exp, a); \
extra; \
} \
\
double __match_proto__() \
VRT_r_##which##_grace(struct sess *sp) \
VRT_r_##which##_##fld(struct sess *sp) \
{ \
\
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC); \
return(EXP_Grace(exp.fld)); \
return(EXP_Get_##fld(&exp)); \
}
VRT_DO_EXP(req, sp->exp, grace, )
VRT_DO_EXP(obj, sp->obj->exp, grace, EXP_Rearm(sp->obj))
VRT_DO_EXP(obj, sp->obj->exp, ttl, EXP_Rearm(sp->obj))
VRT_DO_EXP(beresp, sp->wrk->exp, grace, )
VRT_DO_EXP(beresp, sp->wrk->exp, ttl, )
/*--------------------------------------------------------------------
* req.xid
......
......@@ -21,5 +21,5 @@ varnish v1 -vcl+backend {
client c1 {
txreq
rxresp
expect resp.msg == "10.000"
expect resp.msg == "-1.000"
} -run
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment