From ab40f87579195b334fcfe4d2efb30052d5c2d3f3 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 30 Mar 2006 11:16:27 +0000 Subject: [PATCH] get us closer to a connection to the backend git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@88 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_backend.c | 50 ++++++++++++++++++++-- varnish-cache/bin/varnishd/varnishd.c | 4 +- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_backend.c b/varnish-cache/bin/varnishd/cache_backend.c index 84f8fe08..f1ffc390 100644 --- a/varnish-cache/bin/varnishd/cache_backend.c +++ b/varnish-cache/bin/varnishd/cache_backend.c @@ -3,8 +3,15 @@ */ #include +#include +#include +#include #include #include +#include +#include +#include + #include "libvarnish.h" #include "vcl_lang.h" @@ -32,10 +39,47 @@ static TAILQ_HEAD(,vbe) vbe_head = TAILQ_HEAD_INITIALIZER(vbe_head); static pthread_mutex_t vbemtx; -/*--------------------------------------------------------------------*/ +/*-------------------------------------------------------------------- + * XXX: we should not call getaddrinfo() every time, we should cache + * and apply round-robin with blacklisting of entries that do not respond + * etc. Periodic re-lookups to capture changed DNS records would also + * be a good thing in that case. + */ + void connect_to_backend(struct vbe_conn *vc, struct backend *bp) { + struct addrinfo *res, *res0, hint; + int error, s; + + assert(bp != NULL); + assert(bp->hostname != NULL); + memset(&hint, 0, sizeof hint); + hint.ai_family = PF_UNSPEC; + hint.ai_socktype = SOCK_STREAM; + error = getaddrinfo(bp->hostname, + bp->portname == NULL ? "http" : bp->portname, + &hint, &res); + if (error) { + fprintf(stderr, "getaddrinfo: %s\n", + gai_strerror(error)); + return; + } + res0 = res; + do { + s = socket(res0->ai_family, res0->ai_socktype, + res0->ai_protocol); + if (s < 0) + continue; + error = connect(s, res0->ai_addr, res0->ai_addrlen); + if (!error) + break; + close(s); + s = -1; + } while ((res0 = res0->ai_next) != NULL); + freeaddrinfo(res); + vc->fd = s; + return; } /*--------------------------------------------------------------------*/ @@ -73,6 +117,7 @@ VBE_GetFd(struct backend *bp) vc = calloc(sizeof *vc, 1); assert(vc != NULL); vc->vbe = vp; + vc->fd = -1; TAILQ_INSERT_TAIL(&vp->bconn, vc, list); AZ(pthread_mutex_unlock(&vbemtx)); connect_to_backend(vc, bp); @@ -81,9 +126,6 @@ VBE_GetFd(struct backend *bp) return (-1); } - - - void VBE_Init(void) { diff --git a/varnish-cache/bin/varnishd/varnishd.c b/varnish-cache/bin/varnishd/varnishd.c index 3ee9f9b8..f2c9e482 100644 --- a/varnish-cache/bin/varnishd/varnishd.c +++ b/varnish-cache/bin/varnishd/varnishd.c @@ -68,7 +68,9 @@ vcl_default(const char *bflag) buf = NULL; asprintf(&buf, - "backend default { set backend.host = \"%s\"; }\n" + "backend default {\n" + " set backend.host = \"%s\";\n" + "}\n" "sub main {\n" " pass;\n" #if 0 -- 2.39.5