Commit 31e909f0 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a VBE_TryConnect() which tries to connected to a given backend+addrinfo

combination.  This will be necessary in most if not all backend methods,
so it should be generic.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@1963 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 82db9328
......@@ -68,6 +68,7 @@ struct sess;
struct object;
struct objhead;
struct workreq;
struct addrinfo;
/*--------------------------------------------------------------------*/
......@@ -399,6 +400,7 @@ struct backend *VBE_NewBackend(struct backend_method *method);
struct vbe_conn *VBE_NewConn(void);
void VBE_ReleaseConn(struct vbe_conn *);
void VBE_UpdateHealth(struct sess *sp, struct vbe_conn *, int);
int VBE_TryConnect(struct sess *sp, struct addrinfo *ai);
/* cache_backend_simple.c */
extern struct backend_method backend_method_simple;
......
......@@ -33,8 +33,12 @@
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include "heritage.h"
#include "shmlog.h"
#include "cache.h"
......@@ -46,6 +50,64 @@ static MTX VBE_mtx;
struct backendlist backendlist = TAILQ_HEAD_INITIALIZER(backendlist);
/*--------------------------------------------------------------------
* Attempt to connect to a given addrinfo entry.
*
* Must be called with locked backend, but will release the backend
* lock during the slow/sleeping stuff, so that other worker threads
* can have a go, while we ponder.
*
*/
int
VBE_TryConnect(struct sess *sp, struct addrinfo *ai)
{
struct sockaddr_storage ss;
int fam, sockt, proto;
socklen_t alen;
int s;
char abuf1[TCP_ADDRBUFSIZE], abuf2[TCP_ADDRBUFSIZE];
char pbuf1[TCP_PORTBUFSIZE], pbuf2[TCP_PORTBUFSIZE];
/*
* ai is only valid with the lock held, so copy out the bits
* we need to make the connection
*/
fam = ai->ai_family;
sockt = ai->ai_socktype;
proto = ai->ai_protocol;
alen = ai->ai_addrlen;
assert(alen <= sizeof ss);
memcpy(&ss, ai->ai_addr, alen);
/* release lock during stuff that can take a long time */
UNLOCK(&sp->backend->mtx);
s = socket(fam, sockt, proto);
if (s < 0) {
LOCK(&sp->backend->mtx);
return (s);
}
if (connect(s, (void *)&ss, alen) != 0) {
close(s);
LOCK(&sp->backend->mtx);
return (-1);
}
TCP_myname(s, abuf1, sizeof abuf1, pbuf1, sizeof pbuf1);
TCP_name((void*)&ss, alen,
abuf2, sizeof abuf2, pbuf2, sizeof pbuf2);
WSL(sp->wrk, SLT_BackendOpen, s, "%s %s %s %s %s",
sp->backend->vcl_name, abuf1, pbuf1, abuf2, pbuf2);
LOCK(&sp->backend->mtx);
return (s);
}
/*--------------------------------------------------------------------
* Get a http structure for talking to the backend.
*/
......
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