Commit f2ae7fadb250cd32b5619e0fcec549e8671cca1f

ckolivas 2013-07-19T19:09:23

Adjust socket wait timeout in recv_line according to how long we've already waited to avoid a 60 second wait dropping to 1 second due to a blocked socket.

diff --git a/util.c b/util.c
index 86a1333..879e51c 100644
--- a/util.c
+++ b/util.c
@@ -42,6 +42,8 @@
 #include "compat.h"
 #include "util.h"
 
+#define DEFAULT_SOCKWAIT 60
+
 bool successful_connect = false;
 struct timeval nettime;
 
@@ -1025,7 +1027,7 @@ bool stratum_send(struct pool *pool, char *s, ssize_t len)
 	return (ret == SEND_OK);
 }
 
-static bool socket_full(struct pool *pool, bool wait)
+static bool socket_full(struct pool *pool, int wait)
 {
 	SOCKETTYPE sock = pool->sock;
 	struct timeval timeout;
@@ -1034,10 +1036,7 @@ static bool socket_full(struct pool *pool, bool wait)
 	FD_ZERO(&rd);
 	FD_SET(sock, &rd);
 	timeout.tv_usec = 0;
-	if (wait)
-		timeout.tv_sec = 60;
-	else
-		timeout.tv_sec = 1;
+	timeout.tv_sec = wait;
 	if (select(sock + 1, &rd, NULL, NULL, &timeout) > 0)
 		return true;
 	return false;
@@ -1049,7 +1048,7 @@ bool sock_full(struct pool *pool)
 	if (strlen(pool->sockbuf))
 		return true;
 
-	return (socket_full(pool, false));
+	return (socket_full(pool, 0));
 }
 
 static void clear_sockbuf(struct pool *pool)
@@ -1098,14 +1097,15 @@ static void recalloc_sock(struct pool *pool, size_t len)
  * from the socket and returns that as a malloced char */
 char *recv_line(struct pool *pool)
 {
-	ssize_t len, buflen;
 	char *tok, *sret = NULL;
+	ssize_t len, buflen;
+	int waited = 0;
 
 	if (!strstr(pool->sockbuf, "\n")) {
 		struct timeval rstart, now;
 
 		cgtime(&rstart);
-		if (!socket_full(pool, true)) {
+		if (!socket_full(pool, DEFAULT_SOCKWAIT)) {
 			applog(LOG_DEBUG, "Timed out waiting for data on socket_full");
 			goto out;
 		}
@@ -1122,8 +1122,10 @@ char *recv_line(struct pool *pool)
 				suspend_stratum(pool);
 				break;
 			}
+			cgtime(&now);
+			waited = tdiff(&now, &rstart);
 			if (n < 0) {
-				if (!sock_blocks() || !socket_full(pool, false)) {
+				if (!sock_blocks() || !socket_full(pool, DEFAULT_SOCKWAIT - waited)) {
 					applog(LOG_DEBUG, "Failed to recv sock in recv_line");
 					suspend_stratum(pool);
 					break;
@@ -1133,8 +1135,7 @@ char *recv_line(struct pool *pool)
 				recalloc_sock(pool, slen);
 				strcat(pool->sockbuf, s);
 			}
-			cgtime(&now);
-		} while (tdiff(&now, &rstart) < 60 && !strstr(pool->sockbuf, "\n"));
+		} while (waited < DEFAULT_SOCKWAIT && !strstr(pool->sockbuf, "\n"));
 	}
 
 	buflen = strlen(pool->sockbuf);
@@ -1646,7 +1647,7 @@ resend:
 		goto out;
 	}
 
-	if (!socket_full(pool, true)) {
+	if (!socket_full(pool, DEFAULT_SOCKWAIT)) {
 		applog(LOG_DEBUG, "Timed out waiting for response in initiate_stratum");
 		goto out;
 	}