[4.1] a40ceb0 Introduce VTCP_read() which is a timeout-read on tcp sockets.

Poul-Henning Kamp phk at FreeBSD.org
Fri Sep 4 15:54:53 CEST 2015


commit a40ceb077e58c1d35f5e27568e745959be8e5007
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Aug 10 09:23:47 2015 +0000

    Introduce VTCP_read() which is a timeout-read on tcp sockets.

diff --git a/bin/varnishd/cache/cache_session.c b/bin/varnishd/cache/cache_session.c
index 9b8d2a5..56340e0 100644
--- a/bin/varnishd/cache/cache_session.c
+++ b/bin/varnishd/cache/cache_session.c
@@ -41,7 +41,6 @@
 #include "config.h"
 
 #include <errno.h>
-#include <poll.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -197,8 +196,7 @@ SES_RxReInit(struct http_conn *htc)
 enum htc_status_e
 SES_Rx(struct http_conn *htc, double tmo)
 {
-	int i, j;
-	struct pollfd pfd[1];
+	int i;
 
 	CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC);
 	AN(htc->ws->r);
@@ -207,18 +205,9 @@ SES_Rx(struct http_conn *htc, double tmo)
 	i = (htc->ws->r - htc->rxbuf_e) - 1;	/* space for NUL */
 	if (i <= 0)
 		return (HTC_S_OVERFLOW);
-	if (tmo > 0.0) {
-		pfd[0].fd = htc->fd;
-		pfd[0].events = POLLIN;
-		pfd[0].revents = 0;
-		j = (int)floor(tmo * 1e3);
-		if (j == 0)
-			j++;
-		j = poll(pfd, 1, j);
-		if (j == 0)
-			return (HTC_S_TIMEOUT);
-	}
-	i = read(htc->fd, htc->rxbuf_e, i);
+	i = VTCP_read(htc->fd, htc->rxbuf_e, i, tmo);
+	if (i == -2)
+		return (HTC_S_TIMEOUT);
 	if (i <= 0)
 		return (HTC_S_EOF);
 	htc->rxbuf_e += i;
diff --git a/include/vtcp.h b/include/vtcp.h
index f00b44a..a42e768 100644
--- a/include/vtcp.h
+++ b/include/vtcp.h
@@ -62,4 +62,5 @@ int VTCP_listen(const struct suckaddr *addr, int depth, const char **errp);
 int VTCP_listen_on(const char *addr, const char *def_port, int depth,
     const char **errp);
 void VTCP_set_read_timeout(int s, double seconds);
+int VTCP_read(int fd, void *ptr, size_t len, double tmo);
 // #endif
diff --git a/lib/libvarnish/vtcp.c b/lib/libvarnish/vtcp.c
index a168c5f..3992555 100644
--- a/lib/libvarnish/vtcp.c
+++ b/lib/libvarnish/vtcp.c
@@ -554,3 +554,28 @@ VTCP_Check(int a)
 #endif
 	return (0);
 }
+
+/*--------------------------------------------------------------------
+ *
+ */
+
+int
+VTCP_read(int fd, void *ptr, size_t len, double tmo)
+{
+	struct pollfd pfd[1];
+	int i, j;
+
+	if (tmo > 0.0) {
+		pfd[0].fd = fd;
+		pfd[0].events = POLLIN;
+		pfd[0].revents = 0;
+		j = (int)floor(tmo * 1e3);
+		if (j == 0)
+			j++;
+		j = poll(pfd, 1, j);
+		if (j == 0)
+			return (-2);
+	}
+	i = read(fd, ptr, len);
+	return (i < 0 ? -1 : i);
+}



More information about the varnish-commit mailing list