r1963 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Thu Sep 20 10:14:40 CEST 2007


Author: phk
Date: 2007-09-20 10:14:40 +0200 (Thu, 20 Sep 2007)
New Revision: 1963

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_backend.c
Log:
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.



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2007-09-20 07:22:56 UTC (rev 1962)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2007-09-20 08:14:40 UTC (rev 1963)
@@ -68,6 +68,7 @@
 struct object;
 struct objhead;
 struct workreq;
+struct addrinfo;
 
 /*--------------------------------------------------------------------*/
 
@@ -399,6 +400,7 @@
 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;

Modified: trunk/varnish-cache/bin/varnishd/cache_backend.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_backend.c	2007-09-20 07:22:56 UTC (rev 1962)
+++ trunk/varnish-cache/bin/varnishd/cache_backend.c	2007-09-20 08:14:40 UTC (rev 1963)
@@ -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,7 +50,65 @@
 
 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.
  */
 




More information about the varnish-commit mailing list