From bfbe3a1039dfe8aef9559439756d58697c15b068 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 14 Dec 2015 22:07:22 +1100 Subject: [PATCH] Don't block on dead servers if possible when calling server_alive from the gen_loop --- src/ckpool.h | 1 + src/generator.c | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/ckpool.h b/src/ckpool.h index 6d0e7348..61108868 100644 --- a/src/ckpool.h +++ b/src/ckpool.h @@ -111,6 +111,7 @@ struct server_instance { char *auth; char *pass; bool notify; + bool alive; connsock_t cs; void *data; // Private data diff --git a/src/generator.c b/src/generator.c index 5dc76b22..88bf20b2 100644 --- a/src/generator.c +++ b/src/generator.c @@ -147,6 +147,7 @@ static bool server_alive(ckpool_t *ckp, server_instance_t *si, bool pinging) /* Has this server already been reconnected? */ if (cs->fd > 0) return true; + si->alive = false; if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { LOGWARNING("Failed to extract address from %s", si->url); return ret; @@ -189,6 +190,7 @@ out: /* Close and invalidate the file handle */ Close(cs->fd); } else { + si->alive = true; LOGNOTICE("Server alive: %s:%s", cs->url, cs->port); keep_sockalive(cs->fd); } @@ -207,19 +209,30 @@ retry: if (!ping_main(ckp)) goto out; + /* First find a server that is already flagged alive if possible + * without blocking on server_alive() */ for (i = 0; i < ckp->btcds; i++) { server_instance_t *si = ckp->servers[i]; - if (server_alive(ckp, si, false)) { + if (si->alive) { alive = si; - break; + goto living; } } - if (!alive) { - LOGWARNING("CRITICAL: No bitcoinds active!"); - sleep(5); - goto retry; + + /* No servers flagged alive, try to connect to them blocking */ + for (i = 0; i < ckp->btcds; i++) { + server_instance_t *si = ckp->servers[i]; + + if (server_alive(ckp, si, false)) { + alive = si; + goto living; + } } + LOGWARNING("CRITICAL: No bitcoinds active!"); + sleep(5); + goto retry; +living: cs = &alive->cs; LOGINFO("Connected to live server %s:%s", cs->url, cs->port); out: