[master] 87cb4ae Have VTCP_connect() also call socket(2), I have no idea why it didn't just do that from the start.
Poul-Henning Kamp
phk at FreeBSD.org
Mon Jan 12 22:03:51 CET 2015
commit 87cb4ae7e9f85deff0423a8c8fde1d28f6214591
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Jan 12 21:02:59 2015 +0000
Have VTCP_connect() also call socket(2), I have no idea why it didn't
just do that from the start.
diff --git a/bin/varnishd/cache/cache_backend.c b/bin/varnishd/cache/cache_backend.c
index 21b599c..8f12e91 100644
--- a/bin/varnishd/cache/cache_backend.c
+++ b/bin/varnishd/cache/cache_backend.c
@@ -85,49 +85,13 @@ VBE_ReleaseConn(struct vbc *vc)
dst = cache_param->tmx; \
} while (0)
-/*--------------------------------------------------------------------
- * 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.
- *
- */
-
-static int
-vbe_TryConnect(const struct busyobj *bo, int pf,
- const struct suckaddr *sa, const struct vbe_dir *vs)
-{
- int s, i, tmo;
- double tmod;
-
- CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
- CHECK_OBJ_NOTNULL(vs, VDI_SIMPLE_MAGIC);
-
- s = socket(pf, SOCK_STREAM, 0);
- if (s < 0)
- return (s);
-
- FIND_TMO(connect_timeout, tmod, bo, vs->vrt);
-
- tmo = (int)(tmod * 1000.0);
-
- i = VTCP_connect(s, sa, tmo);
-
- if (i != 0) {
- AZ(close(s));
- return (-1);
- }
-
- return (s);
-}
-
/*--------------------------------------------------------------------*/
static void
bes_conn_try(struct busyobj *bo, struct vbc *vc, const struct vbe_dir *vs)
{
- int s;
+ int s, msec;
+ double tmod;
struct backend *bp = vs->backend;
char abuf1[VTCP_ADDRBUFSIZE];
char pbuf1[VTCP_PORTBUFSIZE];
@@ -146,16 +110,19 @@ bes_conn_try(struct busyobj *bo, struct vbc *vc, const struct vbe_dir *vs)
/* release lock during stuff that can take a long time */
+ FIND_TMO(connect_timeout, tmod, bo, vs->vrt);
+ msec = (int)floor(tmod * 1000.0);
+
if (cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
- s = vbe_TryConnect(bo, PF_INET6, bp->ipv6, vs);
+ s = VTCP_connect(bp->ipv6, msec);
vc->addr = bp->ipv6;
}
if (s == -1 && bp->ipv4 != NULL) {
- s = vbe_TryConnect(bo, PF_INET, bp->ipv4, vs);
+ s = VTCP_connect(bp->ipv4, msec);
vc->addr = bp->ipv4;
}
if (s == -1 && !cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
- s = vbe_TryConnect(bo, PF_INET6, bp->ipv6, vs);
+ s = VTCP_connect(bp->ipv6, msec);
vc->addr = bp->ipv6;
}
diff --git a/bin/varnishd/cache/cache_backend_poll.c b/bin/varnishd/cache/cache_backend_poll.c
index 479872b..1519e0e 100644
--- a/bin/varnishd/cache/cache_backend_poll.c
+++ b/bin/varnishd/cache/cache_backend_poll.c
@@ -104,22 +104,6 @@ static struct lock vbp_mtx;
* want to measure the backends response without local distractions.
*/
-static int
-vbp_connect(int pf, const struct suckaddr *sa, int tmo)
-{
- int s, i;
-
- s = socket(pf, SOCK_STREAM, 0);
- if (s < 0)
- return (s);
-
- i = VTCP_connect(s, sa, tmo);
- if (i == 0)
- return (s);
- VTCP_close(&s);
- return (-1);
-}
-
static void
vbp_poke(struct vbp_target *vt)
{
@@ -139,21 +123,21 @@ vbp_poke(struct vbp_target *vt)
s = -1;
if (cache_param->prefer_ipv6 && bp->ipv6 != NULL) {
- s = vbp_connect(PF_INET6, bp->ipv6, tmo);
+ s = VTCP_connect(bp->ipv6, tmo);
t_now = VTIM_real();
tmo = (int)round((t_end - t_now) * 1e3);
if (s >= 0)
vt->good_ipv6 |= 1;
}
if (tmo > 0 && s < 0 && bp->ipv4 != NULL) {
- s = vbp_connect(PF_INET, bp->ipv4, tmo);
+ s = VTCP_connect(bp->ipv4, tmo);
t_now = VTIM_real();
tmo = (int)round((t_end - t_now) * 1e3);
if (s >= 0)
vt->good_ipv4 |= 1;
}
if (tmo > 0 && s < 0 && bp->ipv6 != NULL) {
- s = vbp_connect(PF_INET6, bp->ipv6, tmo);
+ s = VTCP_connect(bp->ipv6, tmo);
t_now = VTIM_real();
tmo = (int)round((t_end - t_now) * 1e3);
if (s >= 0)
diff --git a/include/vtcp.h b/include/vtcp.h
index d89d383..f4a9690 100644
--- a/include/vtcp.h
+++ b/include/vtcp.h
@@ -69,7 +69,7 @@ int VTCP_check_hup(int sock);
#ifdef SOL_SOCKET
void VTCP_name(const struct suckaddr *addr, char *abuf, unsigned alen,
char *pbuf, unsigned plen);
-int VTCP_connect(int s, const struct suckaddr *name, int msec);
+int VTCP_connect(const struct suckaddr *name, int msec);
void VTCP_close(int *s);
void VTCP_set_read_timeout(int s, double seconds);
#endif
diff --git a/lib/libvarnish/vtcp.c b/lib/libvarnish/vtcp.c
index 624dbb8..dcfb0cb 100644
--- a/lib/libvarnish/vtcp.c
+++ b/lib/libvarnish/vtcp.c
@@ -212,26 +212,37 @@ VTCP_nonblocking(int sock)
*/
int
-VTCP_connect(int s, const struct suckaddr *name, int msec)
+VTCP_connect(const struct suckaddr *name, int msec)
{
- int i, k;
+ int s, i, k;
socklen_t l;
struct pollfd fds[1];
const struct sockaddr *sa;
socklen_t sl;
- assert(s >= 0);
+ if (name == NULL)
+ return (-1);
+ /* Attempt the connect */
+ AN(VSA_Sane(name));
+ sa = VSA_Get_Sockaddr(name, &sl);
+ AN(sa);
+ AN(sl);
+
+ s = socket(sa->sa_family, SOCK_STREAM, 0);
+ if (s < 0)
+ return (s);
/* Set the socket non-blocking */
if (msec > 0)
(void)VTCP_nonblocking(s);
- /* Attempt the connect */
- AN(VSA_Sane(name));
- sa = VSA_Get_Sockaddr(name, &sl);
i = connect(s, sa, sl);
- if (i == 0 || errno != EINPROGRESS)
- return (i);
+ if (i == 0)
+ return (s);
+ if (errno != EINPROGRESS) {
+ AZ(close(s));
+ return (-1);
+ }
assert(msec > 0);
/* Exercise our patience, polling for write */
@@ -242,6 +253,7 @@ VTCP_connect(int s, const struct suckaddr *name, int msec)
if (i == 0) {
/* Timeout, close and give up */
+ AZ(close(s));
errno = ETIMEDOUT;
return (-1);
}
@@ -252,11 +264,13 @@ VTCP_connect(int s, const struct suckaddr *name, int msec)
/* An error means no connection established */
errno = k;
- if (k)
+ if (k) {
+ AZ(close(s));
return (-1);
+ }
(void)VTCP_blocking(s);
- return (0);
+ return (s);
}
/*--------------------------------------------------------------------
More information about the varnish-commit
mailing list