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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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;
}