Browse Source

Fall over to another bitcoind if any of our calls fail

master
Con Kolivas 11 years ago
parent
commit
67fefd2675
  1. 36
      src/generator.c

36
src/generator.c

@ -105,6 +105,7 @@ typedef struct proxy_instance proxy_instance_t;
static server_instance_t *live_server(ckpool_t *ckp) static server_instance_t *live_server(ckpool_t *ckp)
{ {
server_instance_t *alive = NULL; server_instance_t *alive = NULL;
connsock_t *cs;
int i; int i;
LOGDEBUG("Attempting to connect to bitcoind"); LOGDEBUG("Attempting to connect to bitcoind");
@ -112,7 +113,6 @@ retry:
for (i = 0; i < ckp->btcds; i++) { for (i = 0; i < ckp->btcds; i++) {
server_instance_t *si; server_instance_t *si;
char *userpass = NULL; char *userpass = NULL;
connsock_t *cs;
gbtbase_t *gbt; gbtbase_t *gbt;
si = ckp->servers[i]; si = ckp->servers[i];
@ -159,20 +159,31 @@ retry:
sleep(5); sleep(5);
goto retry; goto retry;
} }
cs = &alive->cs;
LOGINFO("Connected to live server %s:%s", cs->url, cs->port);
return alive; return alive;
} }
static void kill_server(server_instance_t *si)
{
close(si->cs.fd);
si->cs.fd = -1;
}
static int gen_loop(proc_instance_t *pi) static int gen_loop(proc_instance_t *pi)
{ {
server_instance_t *si = NULL;
unixsock_t *us = &pi->us; unixsock_t *us = &pi->us;
ckpool_t *ckp = pi->ckp; ckpool_t *ckp = pi->ckp;
server_instance_t *si;
int sockd, ret = 0; int sockd, ret = 0;
char *buf = NULL; char *buf = NULL;
connsock_t *cs; connsock_t *cs;
gbtbase_t *gbt; gbtbase_t *gbt;
char hash[68]; char hash[68];
reconnect:
if (si)
kill_server(si);
si = live_server(ckp); si = live_server(ckp);
gbt = si->data; gbt = si->data;
cs = &si->cs; cs = &si->cs;
@ -203,6 +214,7 @@ retry:
LOGWARNING("Failed to get block template from %s:%s", LOGWARNING("Failed to get block template from %s:%s",
cs->url, cs->port); cs->url, cs->port);
send_unix_msg(sockd, "Failed"); send_unix_msg(sockd, "Failed");
goto reconnect;
} else { } else {
char *s = json_dumps(gbt->json, 0); char *s = json_dumps(gbt->json, 0);
@ -215,19 +227,20 @@ retry:
LOGINFO("No best block hash support from %s:%s", LOGINFO("No best block hash support from %s:%s",
cs->url, cs->port); cs->url, cs->port);
send_unix_msg(sockd, "Failed"); send_unix_msg(sockd, "Failed");
} else { } else
send_unix_msg(sockd, hash); send_unix_msg(sockd, hash);
}
} else if (!strncasecmp(buf, "getlast", 7)) { } else if (!strncasecmp(buf, "getlast", 7)) {
int height = get_blockcount(cs); int height = get_blockcount(cs);
if (height == -1) if (height == -1) {
send_unix_msg(sockd, "Failed"); send_unix_msg(sockd, "Failed");
else { goto reconnect;
} else {
LOGDEBUG("Height: %d", height); LOGDEBUG("Height: %d", height);
if (!get_blockhash(cs, height, hash)) if (!get_blockhash(cs, height, hash)) {
send_unix_msg(sockd, "Failed"); send_unix_msg(sockd, "Failed");
else { goto reconnect;
} else {
send_unix_msg(sockd, hash); send_unix_msg(sockd, hash);
LOGDEBUG("Hash: %s", hash); LOGDEBUG("Hash: %s", hash);
} }
@ -244,6 +257,7 @@ retry:
goto retry; goto retry;
out: out:
kill_server(si);
dealloc(buf); dealloc(buf);
return ret; return ret;
} }
@ -1009,6 +1023,7 @@ static void *proxy_send(void *arg)
static proxy_instance_t *live_proxy(ckpool_t *ckp) static proxy_instance_t *live_proxy(ckpool_t *ckp)
{ {
proxy_instance_t *alive = NULL; proxy_instance_t *alive = NULL;
connsock_t *cs;
int i; int i;
LOGDEBUG("Attempting to connect to proxy"); LOGDEBUG("Attempting to connect to proxy");
@ -1016,7 +1031,6 @@ retry:
for (i = 0; i < ckp->proxies; i++) { for (i = 0; i < ckp->proxies; i++) {
proxy_instance_t *proxi; proxy_instance_t *proxi;
server_instance_t *si; server_instance_t *si;
connsock_t *cs;
si = ckp->servers[i]; si = ckp->servers[i];
proxi = si->data; proxi = si->data;
@ -1049,7 +1063,8 @@ retry:
sleep(5); sleep(5);
goto retry; goto retry;
} }
LOGNOTICE("Connected to upstream server %s:%s as proxy", alive->cs->url, alive->cs->port); cs = alive->cs;
LOGNOTICE("Connected to upstream server %s:%s as proxy", cs->url, cs->port);
mutex_init(&alive->notify_lock); mutex_init(&alive->notify_lock);
create_pthread(&alive->pth_precv, proxy_recv, alive); create_pthread(&alive->pth_precv, proxy_recv, alive);
mutex_init(&alive->psend_lock); mutex_init(&alive->psend_lock);
@ -1144,7 +1159,6 @@ out:
return ret; return ret;
} }
static int server_mode(ckpool_t *ckp, proc_instance_t *pi) static int server_mode(ckpool_t *ckp, proc_instance_t *pi)
{ {
server_instance_t *si; server_instance_t *si;

Loading…
Cancel
Save