Commit e8597bf4 authored by Petter Knudsen's avatar Petter Knudsen

Added support for setting read timeouts for backend requests...

Added support for setting read timeouts for backend requests (first_byte_timeout and between_bytes_timeout), in addition to make the connect_timeout available for the bereq object in vcl_miss and vcl_fetch.

first_byte_timeout is a read timeout from the connection to the backend is created to when the first byte arrives. It can be set as a parameter to varnish, as a field in the backend declaration or as bereq.first_byte_timeout in vcl_miss and vcl_pass.

between_bytes_timeout is a read timeout between each read from the backend. It can be set as a parameter to varnish, as a field in the backend declaration or as bereq.between_bytes_timeout in vcl_miss and vcl_pass.

The time unit for these timeout values are seconds. NOTE: The connect_timeout previously used milliseconds as time unit, so beware.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@3406 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 884f8ebd
......@@ -337,6 +337,11 @@ struct sess {
double t_resp;
double t_end;
/* Timeouts */
double connect_timeout;
double first_byte_timeout;
double between_bytes_timeout;
/* Acceptable grace period */
double grace;
......@@ -538,6 +543,8 @@ struct sess *SES_New(const struct sockaddr *addr, unsigned len);
void SES_Delete(struct sess *sp);
void SES_RefSrcAddr(struct sess *sp);
void SES_Charge(struct sess *sp);
void SES_ResetBackendTimeouts(struct sess *sp);
void SES_InheritBackendTimeouts(struct sess *sp);
/* cache_shmlog.c */
void VSL_Init(void);
......
......@@ -95,7 +95,7 @@ VBE_TryConnect(const struct sess *sp, int pf, const struct sockaddr *sa,
if (s < 0)
return (s);
tmo = params->connect_timeout;
tmo = (int)(sp->connect_timeout * 1000);
if (bp->connect_timeout > 10e-3)
tmo = (int)(bp->connect_timeout * 1000);
......
......@@ -104,6 +104,8 @@ struct backend {
char *ident;
char *vcl_name;
double connect_timeout;
double first_byte_timeout;
double between_bytes_timeout;
uint32_t hash;
......
......@@ -222,6 +222,8 @@ VBE_AddBackend(struct cli *cli, const struct vrt_backend *vb)
REPLACE(b->hosthdr, vb->hosthdr);
b->connect_timeout = vb->connect_timeout;
b->first_byte_timeout = vb->first_byte_timeout;
b->between_bytes_timeout = vb->between_bytes_timeout;
b->max_conn = vb->max_connections;
/*
......
......@@ -845,6 +845,8 @@ cnt_recv(struct sess *sp)
CHECK_OBJ_NOTNULL(sp->vcl, VCL_CONF_MAGIC);
AZ(sp->obj);
SES_ResetBackendTimeouts(sp);
/* By default we use the first backend */
AZ(sp->director);
sp->director = sp->vcl->director[0];
......
......@@ -336,6 +336,8 @@ Fetch(struct sess *sp)
if (sp->vbe == NULL)
return (__LINE__);
vc = sp->vbe;
/* Inherit the backend timeouts from the selected backend */
SES_InheritBackendTimeouts(sp);
/*
* Now that we know our backend, we can set a default Host:
......@@ -369,8 +371,11 @@ Fetch(struct sess *sp)
VSL_stats->backend_req++;
HTC_Init(htc, bereq->ws, vc->fd);
do
TCP_set_read_timeout(vc->fd, sp->first_byte_timeout);
do {
i = HTC_Rx(htc);
TCP_set_read_timeout(vc->fd, sp->between_bytes_timeout);
}
while (i == 0);
if (i < 0) {
......
......@@ -58,6 +58,7 @@
#include "shmlog.h"
#include "cache.h"
#include "cache_backend.h"
/*--------------------------------------------------------------------*/
......@@ -316,6 +317,8 @@ SES_New(const struct sockaddr *addr, unsigned len)
sp->http = &sm->http[0];
sp->http0 = &sm->http[1];
SES_ResetBackendTimeouts(sp);
return (sp);
}
......@@ -367,3 +370,38 @@ SES_Init()
Lck_New(&stat_mtx);
Lck_New(&ses_mem_mtx);
}
void
SES_ResetBackendTimeouts(struct sess *sp)
{
sp->connect_timeout = params->connect_timeout;
sp->first_byte_timeout = params->first_byte_timeout;
sp->between_bytes_timeout = params->between_bytes_timeout;
}
void
SES_InheritBackendTimeouts(struct sess *sp)
{
struct backend *be = NULL;
AN(sp);
AN(sp->vbe);
AN(sp->vbe->backend);
be = sp->vbe->backend;
/*
* We only inherit the backend's timeout if the session timeout
* has not already been set in the VCL, as the order of precedence
* is parameter < backend definition < VCL.
*/
if (be->connect_timeout > 1e-3 &&
sp->connect_timeout == params->connect_timeout)
sp->connect_timeout = be->connect_timeout;
if (be->first_byte_timeout > 1e-3 &&
sp->first_byte_timeout == params->first_byte_timeout)
sp->first_byte_timeout = be->first_byte_timeout;
if (be->between_bytes_timeout > 1e-3
&& sp->between_bytes_timeout == params->between_bytes_timeout)
sp->between_bytes_timeout = be->between_bytes_timeout;
}
......@@ -299,6 +299,48 @@ VRT_r_resp_status(const struct sess *sp)
return (atoi(sp->http->hd[HTTP_HDR_STATUS].b));
}
void
VRT_l_bereq_connect_timeout(struct sess *sp, double num)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
sp->connect_timeout = (num > 0 ? num : 0);
}
double
VRT_r_bereq_connect_timeout(struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
return sp->connect_timeout;
}
void
VRT_l_bereq_first_byte_timeout(struct sess *sp, double num)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
sp->first_byte_timeout = (num > 0 ? num : 0);
}
double
VRT_r_bereq_first_byte_timeout(struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
return sp->first_byte_timeout;
}
void
VRT_l_bereq_between_bytes_timeout(struct sess *sp, double num)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
sp->between_bytes_timeout = (num > 0 ? num : 0);
}
double
VRT_r_bereq_between_bytes_timeout(struct sess *sp)
{
CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
return sp->between_bytes_timeout;
}
/*--------------------------------------------------------------------*/
void
......
......@@ -154,7 +154,11 @@ struct params {
unsigned cache_vbe_conns;
/* Default connection_timeout */
unsigned connect_timeout;
double connect_timeout;
/* Read timeouts for backend */
double first_byte_timeout;
double between_bytes_timeout;
/* How long to linger on sessions */
unsigned session_linger;
......
......@@ -97,6 +97,24 @@ tweak_generic_timeout(struct cli *cli, volatile unsigned *dst, const char *arg)
cli_out(cli, "%u", *dst);
}
static void
tweak_generic_timeout_double(struct cli *cli, volatile double *dst, const char *arg)
{
double u;
if (arg != NULL) {
u = strtod(arg, NULL);
if (u < 0) {
cli_out(cli, "Timeout must be greater or equal to zero\n");
cli_result(cli, CLIS_PARAM);
return;
}
*dst = u;
} else
cli_out(cli, "%f", *dst);
}
/*--------------------------------------------------------------------*/
static void
......@@ -108,7 +126,14 @@ tweak_timeout(struct cli *cli, const struct parspec *par, const char *arg)
tweak_generic_timeout(cli, dest, arg);
}
static void
tweak_timeout_double(struct cli *cli, const struct parspec *par, const char *arg)
{
volatile double *dest;
dest = par->priv;
tweak_generic_timeout_double(cli, dest, arg);
}
/*--------------------------------------------------------------------*/
static void
......@@ -746,14 +771,31 @@ static const struct parspec parspec[] = {
"Cache vbe_conn's or rely on malloc, that's the question.",
EXPERIMENTAL,
"off", "bool" },
{ "connect_timeout", tweak_uint,
{ "connect_timeout", tweak_timeout_double,
&master.connect_timeout,0, UINT_MAX,
"Default connection timeout for backend connections. "
"We only try to connect to the backend for this many "
"milliseconds before giving up. "
"VCL can override this default value for each backend.",
"seconds before giving up. "
"VCL can override this default value for each backend. "
"This does not apply to pipe. ",
0,
"0.4", "s" },
{ "first_byte_timeout", tweak_timeout_double,
&master.first_byte_timeout,0, UINT_MAX,
"Default timeout for receiving first byte from backend. "
"We only wait for this many seconds for the first "
"byte before giving up. A value of 0 means it will never time out. "
"VCL can override this default value for each backend request.",
0,
"60", "s" },
{ "between_bytes_timeout", tweak_timeout_double,
&master.between_bytes_timeout,0, UINT_MAX,
"Default timeout between bytes when receiving data from backend. "
"We only wait for this many seconds between bytes "
"before giving up. A value of 0 means it will never time out. "
"VCL can override this default value for each backend request.",
0,
"400", "ms" },
"60", "s" },
{ "accept_fd_holdoff", tweak_timeout,
&master.accept_fd_holdoff, 0, 3600*1000,
"If we run out of file descriptors, the accept thread will "
......
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check the between_bytes_timeout behaves from parameters"
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 1.5
send "Baba\n"
} -start
varnish v1 -vcl+backend {} -start
varnish v1 -cliok "param.set between_bytes_timeout 1"
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 0.5
send "Baba\n"
delay 0.5
send "Baba\n"
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check the between_bytes_timeout behaves from vcl"
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 1.5
send "Baba\n"
} -start
varnish v1 -vcl+backend {
sub vcl_miss {
set bereq.between_bytes_timeout = 1s;
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 0.5
send "Baba\n"
delay 0.5
send "Baba\n"
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check the between_bytes_timeout behaves from backend definition"
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 1.5
send "Baba\n"
} -start
varnish v1 -vcl {
backend b1 {
.host = "127.0.0.1";
.port = "9080";
.between_bytes_timeout = 1s;
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
send "HTTP/1.1 200 Ok\r\nConnection: close\r\n\r\n"
delay 0.5
send "Baba\n"
delay 0.5
send "Baba\n"
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check that the first_byte_timeout works from parameters"
server s1 {
rxreq
delay 1.5
txresp
} -start
varnish v1 -vcl+backend {} -start
varnish v1 -cliok "param.set first_byte_timeout 1"
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
delay 0.5
txresp
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check that the first_byte_timeout works from vcl"
server s1 {
rxreq
delay 1.5
txresp
} -start
varnish v1 -vcl+backend {
sub vcl_miss {
set bereq.first_byte_timeout = 1s;
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
delay 0.5
txresp
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check that the first_byte_timeout works from backend definition"
server s1 {
rxreq
delay 1.5
txresp
} -start
varnish v1 -vcl {
backend b1 {
.host = "127.0.0.1";
.port = "9080";
.first_byte_timeout = 1s;
}
} -start
client c1 {
txreq
rxresp
expect resp.status == 503
} -run
server s1 {
rxreq
delay 0.5
txresp
} -start
client c1 {
txreq
rxresp
expect resp.status == 200
} -run
# $Id: b00019.vtc 3300 2008-10-15 09:52:15Z tfheen $
test "Check the precedence for timeouts"
server s1 -listen 127.0.0.1:9080 {
rxreq
expect req.url == "from_backend"
delay 1;
txresp
} -start
server s2 -listen 127.0.0.1:9180 {
rxreq
expect req.url == "from_vcl"
delay 1.5;
txresp
} -start
varnish v1 -vcl {
backend b1 {
.host = "127.0.0.1";
.port = "9080";
.first_byte_timeout = 2s;
}
backend b2 {
.host = "127.0.0.1";
.port = "9180";
.first_byte_timeout = 1s;
}
sub vcl_recv {
if (req.url == "from_backend") {
set req.backend = b1;
pass;
}
set req.backend = b2;
}
sub vcl_miss {
set bereq.first_byte_timeout = 2s;
}
} -start
varnish v1 -cliok "param.set first_byte_timeout 0.5"
client c1 {
txreq -url "from_backend"
rxresp
expect resp.status == 200
txreq -url "from_vcl"
rxresp
expect resp.status == 200
} -run
......@@ -65,6 +65,7 @@ void TCP_name(const struct sockaddr *addr, unsigned l, char *abuf,
int TCP_connect(int s, const struct sockaddr *name, socklen_t namelen,
int msec);
void TCP_close(int *s);
void TCP_set_read_timeout(int socket, double seconds);
#endif
/* from libvarnish/time.c */
......
......@@ -69,6 +69,8 @@ struct vrt_backend {
const unsigned char *ipv6_sockaddr;
double connect_timeout;
double first_byte_timeout;
double between_bytes_timeout;
unsigned max_connections;
struct vrt_backend_probe probe;
};
......
......@@ -28,6 +28,12 @@ const char * VRT_r_bereq_url(const struct sess *);
void VRT_l_bereq_url(const struct sess *, const char *, ...);
const char * VRT_r_bereq_proto(const struct sess *);
void VRT_l_bereq_proto(const struct sess *, const char *, ...);
double VRT_r_bereq_connect_timeout(struct sess *);
void VRT_l_bereq_connect_timeout(struct sess *, double);
double VRT_r_bereq_first_byte_timeout(struct sess *);
void VRT_l_bereq_first_byte_timeout(struct sess *, double);
double VRT_r_bereq_between_bytes_timeout(struct sess *);
void VRT_l_bereq_between_bytes_timeout(struct sess *, double);
const char * VRT_r_obj_proto(const struct sess *);
void VRT_l_obj_proto(const struct sess *, const char *, ...);
int VRT_r_obj_status(const struct sess *);
......
......@@ -47,6 +47,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "config.h"
#ifndef HAVE_STRLCPY
......@@ -210,3 +211,14 @@ TCP_close(int *s)
errno == ENOTCONN);
*s = -1;
}
void
TCP_set_read_timeout(int s, double seconds)
{
struct timeval timeout;
timeout.tv_sec = floor(seconds);
timeout.tv_usec = 1e6 * (seconds - timeout.tv_sec);
#ifdef SO_RCVTIMEO_WORKS
AZ(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof timeout));
#endif
}
......@@ -473,6 +473,8 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name,
"?port",
"?host_header",
"?connect_timeout",
"?first_byte_timeout",
"?between_bytes_timeout",
"?probe",
"?max_connections",
NULL);
......@@ -537,6 +539,20 @@ vcc_ParseHostDef(struct tokenlist *tl, int *nbh, const struct token *name,
Fb(tl, 0, ",\n");
ExpectErr(tl, ';');
vcc_NextToken(tl);
} else if (vcc_IdIs(t_field, "first_byte_timeout")) {
Fb(tl, 0, "\t.first_byte_timeout = ");
vcc_TimeVal(tl);
ERRCHK(tl);
Fb(tl, 0, ",\n");
ExpectErr(tl, ';');
vcc_NextToken(tl);
} else if (vcc_IdIs(t_field, "between_bytes_timeout")) {
Fb(tl, 0, "\t.between_bytes_timeout = ");
vcc_TimeVal(tl);
ERRCHK(tl);
Fb(tl, 0, ",\n");
ExpectErr(tl, ';');
vcc_NextToken(tl);
} else if (vcc_IdIs(t_field, "max_connections")) {
u = vcc_UintVal(tl);
vcc_NextToken(tl);
......
......@@ -243,6 +243,8 @@ vcl_output_lang_h(struct vsb *sb)
vsb_cat(sb, "\tconst unsigned char\t\t*ipv4_sockaddr;\n");
vsb_cat(sb, "\tconst unsigned char\t\t*ipv6_sockaddr;\n");
vsb_cat(sb, "\n\tdouble\t\t\t\tconnect_timeout;\n");
vsb_cat(sb, "\tdouble\t\t\t\tfirst_byte_timeout;\n");
vsb_cat(sb, "\tdouble\t\t\t\tbetween_bytes_timeout;\n");
vsb_cat(sb, "\tunsigned\t\t\tmax_connections;\n");
vsb_cat(sb, "\tstruct vrt_backend_probe\tprobe;\n");
vsb_cat(sb, "};\n\n/*\n * A director with a predictable reply\n");
......@@ -335,9 +337,16 @@ vcl_output_lang_h(struct vsb *sb)
vsb_cat(sb, " const char *, ...);\nconst char * VRT_r_bereq_proto(c");
vsb_cat(sb, "onst struct sess *);\nvoid VRT_l_bereq_proto(const str");
vsb_cat(sb, "uct sess *, const char *, ...);\n");
vsb_cat(sb, "const char * VRT_r_obj_proto(const struct sess *);\n");
vsb_cat(sb, "void VRT_l_obj_proto(const struct sess *, const char *");
vsb_cat(sb, ", ...);\nint VRT_r_obj_status(const struct sess *);\n");
vsb_cat(sb, "double VRT_r_bereq_connect_timeout(struct sess *);\n");
vsb_cat(sb, "void VRT_l_bereq_connect_timeout(struct sess *, double");
vsb_cat(sb, ");\ndouble VRT_r_bereq_first_byte_timeout(struct sess ");
vsb_cat(sb, "*);\nvoid VRT_l_bereq_first_byte_timeout(struct sess *");
vsb_cat(sb, ", double);\ndouble VRT_r_bereq_between_bytes_timeout(s");
vsb_cat(sb, "truct sess *);\nvoid VRT_l_bereq_between_bytes_timeout");
vsb_cat(sb, "(struct sess *, double);\nconst char * VRT_r_obj_proto");
vsb_cat(sb, "(const struct sess *);\nvoid VRT_l_obj_proto(const str");
vsb_cat(sb, "uct sess *, const char *, ...);\n");
vsb_cat(sb, "int VRT_r_obj_status(const struct sess *);\n");
vsb_cat(sb, "void VRT_l_obj_status(const struct sess *, int);\n");
vsb_cat(sb, "const char * VRT_r_obj_response(const struct sess *);\n");
vsb_cat(sb, "void VRT_l_obj_response(const struct sess *, const cha");
......
......@@ -127,6 +127,21 @@ set spobj {
{ pipe pass miss fetch }
"const struct sess *"
}
{ bereq.connect_timeout
RW TIME
{ pass miss }
"struct sess *"
}
{ bereq.first_byte_timeout
RW TIME
{ pass miss }
"struct sess *"
}
{ bereq.between_bytes_timeout
RW TIME
{ pass miss }
"struct sess *"
}
# The (possibly) cached object
{ obj.proto
......
......@@ -108,6 +108,21 @@ struct var vcc_vars[] = {
V_RW, "HDR_BEREQ",
VCL_MET_PIPE | VCL_MET_PASS | VCL_MET_MISS | VCL_MET_FETCH
},
{ "bereq.connect_timeout", TIME, 21,
"VRT_r_bereq_connect_timeout(sp)", "VRT_l_bereq_connect_timeout(sp, ",
V_RW, 0,
VCL_MET_PASS | VCL_MET_MISS
},
{ "bereq.first_byte_timeout", TIME, 24,
"VRT_r_bereq_first_byte_timeout(sp)", "VRT_l_bereq_first_byte_timeout(sp, ",
V_RW, 0,
VCL_MET_PASS | VCL_MET_MISS
},
{ "bereq.between_bytes_timeout", TIME, 27,
"VRT_r_bereq_between_bytes_timeout(sp)", "VRT_l_bereq_between_bytes_timeout(sp, ",
V_RW, 0,
VCL_MET_PASS | VCL_MET_MISS
},
{ "obj.proto", STRING, 9,
"VRT_r_obj_proto(sp)", "VRT_l_obj_proto(sp, ",
V_RW, 0,
......
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