From ec052a9c5485e5027bd46620dd4438ee7bc5ad8d Mon Sep 17 00:00:00 2001 From: phk Date: Fri, 30 May 2008 22:18:26 +0000 Subject: [PATCH] Add a new backend attribute in VCL: "connect_timeout". This is how long time we wait for a TCP connection to the backend to become established. Typical usage: backend b1 { .host = "hex"; .port = "80"; .connect_timeout = 500 ms; } It can also be used in backends in director declarations. Also add a parameter called "connect_timeout" which sets the default to 400 msec (a number pulled out of my old black magicians hat). git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2642 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_backend.c | 13 +++++++++++-- varnish-cache/bin/varnishd/heritage.h | 3 +++ varnish-cache/bin/varnishd/mgt_param.c | 8 ++++++++ varnish-cache/include/vrt.h | 1 + varnish-cache/lib/libvcl/vcc_backend.c | 10 ++++++++-- varnish-cache/lib/libvcl/vcc_fixed_token.c | 1 + 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c index c2f35a0a..7a1e752c 100644 --- a/varnish-cache/bin/varnishd/cache_backend.c +++ b/varnish-cache/bin/varnishd/cache_backend.c @@ -150,7 +150,7 @@ VBE_TryConnect(const struct sess *sp, const struct addrinfo *ai) struct sockaddr_storage ss; int fam, sockt, proto; socklen_t alen; - int s; + int s, i, tmo; char abuf1[TCP_ADDRBUFSIZE], abuf2[TCP_ADDRBUFSIZE]; char pbuf1[TCP_PORTBUFSIZE], pbuf2[TCP_PORTBUFSIZE]; @@ -177,7 +177,16 @@ VBE_TryConnect(const struct sess *sp, const struct addrinfo *ai) return (s); } - if (connect(s, (void *)&ss, alen) != 0) { + tmo = params->connect_timeout; + if (sp->backend->vrt->connect_timeout > 10e-3) + tmo = sp->backend->vrt->connect_timeout * 1000; + + if (tmo > 0) + i = TCP_connect(s, (void *)&ss, alen, tmo); + else + i = connect(s, (void *)&ss, alen); + + if (i != 0) { AZ(close(s)); LOCK(&sp->backend->mtx); return (-1); diff --git a/varnish-cache/bin/varnishd/heritage.h b/varnish-cache/bin/varnishd/heritage.h index 7d011efd..e464fb75 100644 --- a/varnish-cache/bin/varnishd/heritage.h +++ b/varnish-cache/bin/varnishd/heritage.h @@ -140,6 +140,9 @@ struct params { /* Cache vbe_conns */ unsigned cache_vbe_conns; + /* Default connection_timeout */ + unsigned connect_timeout; + /* CLI buffer size */ unsigned cli_buffer; diff --git a/varnish-cache/bin/varnishd/mgt_param.c b/varnish-cache/bin/varnishd/mgt_param.c index eb7ad661..6694559e 100644 --- a/varnish-cache/bin/varnishd/mgt_param.c +++ b/varnish-cache/bin/varnishd/mgt_param.c @@ -639,6 +639,14 @@ static const struct parspec parspec[] = { "Cache vbe_conn's or rely on malloc, that's the question.", EXPERIMENTAL, "off", "bool" }, + { "connect_timeout", tweak_uint, + &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.", + 0, + "400", "ms" }, { "cli_buffer", tweak_uint, &master.cli_buffer, 4096, UINT_MAX, "Size of buffer for CLI input." "\nYou may need to increase this if you have big VCL files " diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index 24c086e1..de5f15b7 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -49,6 +49,7 @@ struct vrt_backend { const char *hostname; const char *vcl_name; const char *ident; + double connect_timeout; }; /* diff --git a/varnish-cache/lib/libvcl/vcc_backend.c b/varnish-cache/lib/libvcl/vcc_backend.c index b91f5709..c5171d2f 100644 --- a/varnish-cache/lib/libvcl/vcc_backend.c +++ b/varnish-cache/lib/libvcl/vcc_backend.c @@ -223,7 +223,7 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh, const struct token *qual, i struct host *h; struct fld_spec *fs; - fs = vcc_FldSpec(tl, "!host", "?port", NULL); + fs = vcc_FldSpec(tl, "!host", "?port", "?connect_timeout", NULL); t_first = tl->t; if (tl->t->tok == ID) { @@ -273,10 +273,16 @@ vcc_ParseBackendHost(struct tokenlist *tl, int *nbh, const struct token *qual, i t_host = tl->t; vcc_NextToken(tl); } else if (vcc_IdIs(t_field, "port")) { - ExpectErr(tl, CSTR); assert(tl->t->dec != NULL); t_port = tl->t; vcc_NextToken(tl); + } else if (vcc_IdIs(t_field, "connect_timeout")) { + Fh(tl, 0, "\t.connect_timeout = "); + tl->fb = tl->fh; + vcc_TimeVal(tl); + tl->fb = NULL; + ERRCHK(tl); + Fh(tl, 0, ",\n"); } else { ErrInternal(tl); return; diff --git a/varnish-cache/lib/libvcl/vcc_fixed_token.c b/varnish-cache/lib/libvcl/vcc_fixed_token.c index 9232c3b0..00c38f18 100644 --- a/varnish-cache/lib/libvcl/vcc_fixed_token.c +++ b/varnish-cache/lib/libvcl/vcc_fixed_token.c @@ -403,6 +403,7 @@ vcl_output_lang_h(struct vsb *sb) vsb_cat(sb, " const char *hostname;\n"); vsb_cat(sb, " const char *vcl_name;\n"); vsb_cat(sb, " const char *ident;\n"); + vsb_cat(sb, " double connect_timeout;\n"); vsb_cat(sb, "};\n"); vsb_cat(sb, "\n"); vsb_cat(sb, "/*\n"); -- 2.39.5