]> err.no Git - varnish/commitdiff
Add a new backend attribute in VCL: "connect_timeout".
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Fri, 30 May 2008 22:18:26 +0000 (22:18 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Fri, 30 May 2008 22:18:26 +0000 (22:18 +0000)
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
varnish-cache/bin/varnishd/heritage.h
varnish-cache/bin/varnishd/mgt_param.c
varnish-cache/include/vrt.h
varnish-cache/lib/libvcl/vcc_backend.c
varnish-cache/lib/libvcl/vcc_fixed_token.c

index c2f35a0a67fafabea9b8e2a30181ce5c5c23e1f2..7a1e752c6b53ab9cc0168abb4f76a202f072e65e 100644 (file)
@@ -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);
index 7d011efdab6711ba26eefcc428a96f6940f99377..e464fb75a702e523f3aebc67b90172580b83cf3c 100644 (file)
@@ -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;
 
index eb7ad66135f7389927c4a179604138f2f41850fe..6694559ec9758b4874d810d0e591c19ed0ac9022 100644 (file)
@@ -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 "
index 24c086e17cb4ee7a41eeab05a519d201626635dd..de5f15b7e6fa7e3658d9bf50be0e1e30c9d5fe92 100644 (file)
@@ -49,6 +49,7 @@ struct vrt_backend {
        const char      *hostname;
        const char      *vcl_name;
        const char      *ident;
+       double          connect_timeout;
 };
 
 /*
index b91f5709cf8a64e6fb4f1d6f8e804e5f8f9e2955..c5171d2fe5a84c258a9172e34a92f2aaaeec7c5b 100644 (file)
@@ -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;
index 9232c3b0528a2fa30c4d9b83c4d7239b3d2e32c3..00c38f18a69812ca81534bc5dde23f8e64f0ad7f 100644 (file)
@@ -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");