Browse Source

Abstract out the testing of whether a server is alive to be used for future fallback code

master
Con Kolivas 10 years ago
parent
commit
d021692a21
  1. 57
      src/generator.c

57
src/generator.c

@ -122,27 +122,17 @@ struct proxy_instance {
typedef struct proxy_instance proxy_instance_t; typedef struct proxy_instance proxy_instance_t;
static server_instance_t *live_server(ckpool_t *ckp) static bool server_alive(ckpool_t *ckp, server_instance_t *si, bool pinging)
{ {
server_instance_t *alive = NULL;
connsock_t *cs;
int i;
LOGDEBUG("Attempting to connect to bitcoind");
retry:
if (!ping_main(ckp))
goto out;
for (i = 0; i < ckp->btcds; i++) {
server_instance_t *si;
char *userpass = NULL; char *userpass = NULL;
bool ret = false;
connsock_t *cs;
gbtbase_t *gbt; gbtbase_t *gbt;
si = ckp->servers[i];
cs = &si->cs; cs = &si->cs;
if (!extract_sockaddr(si->url, &cs->url, &cs->port)) { if (!extract_sockaddr(si->url, &cs->url, &cs->port)) {
LOGWARNING("Failed to extract address from %s", si->url); LOGWARNING("Failed to extract address from %s", si->url);
continue; return ret;
} }
userpass = strdup(si->auth); userpass = strdup(si->auth);
realloc_strcat(&userpass, ":"); realloc_strcat(&userpass, ":");
@ -151,33 +141,60 @@ retry:
dealloc(userpass); dealloc(userpass);
if (!cs->auth) { if (!cs->auth) {
LOGWARNING("Failed to create base64 auth from %s", userpass); LOGWARNING("Failed to create base64 auth from %s", userpass);
continue; return ret;
} }
cs->fd = connect_socket(cs->url, cs->port); cs->fd = connect_socket(cs->url, cs->port);
if (cs->fd < 0) { if (cs->fd < 0) {
if (!pinging)
LOGWARNING("Failed to connect socket to %s:%s !", cs->url, cs->port); LOGWARNING("Failed to connect socket to %s:%s !", cs->url, cs->port);
continue; return ret;
} }
keep_sockalive(cs->fd);
/* Test we can connect, authorise and get a block template */ /* Test we can connect, authorise and get a block template */
gbt = ckzalloc(sizeof(gbtbase_t)); gbt = ckzalloc(sizeof(gbtbase_t));
si->data = gbt; si->data = gbt;
if (!gen_gbtbase(cs, gbt)) { if (!gen_gbtbase(cs, gbt)) {
if (!pinging) {
LOGINFO("Failed to get test block template from %s:%s!", LOGINFO("Failed to get test block template from %s:%s!",
cs->url, cs->port); cs->url, cs->port);
continue; }
goto out_close;
} }
clear_gbtbase(gbt); clear_gbtbase(gbt);
if (!validate_address(cs, ckp->btcaddress)) { if (!validate_address(cs, ckp->btcaddress)) {
LOGWARNING("Invalid btcaddress: %s !", ckp->btcaddress); LOGWARNING("Invalid btcaddress: %s !", ckp->btcaddress);
continue; goto out_close;
} }
ret = true;
out_close:
if (!ret)
close(cs->fd);
else
keep_sockalive(cs->fd);
return ret;
}
/* Find the highest priority server alive and return it */
static server_instance_t *live_server(ckpool_t *ckp)
{
server_instance_t *alive = NULL;
connsock_t *cs;
int i;
LOGDEBUG("Attempting to connect to bitcoind");
retry:
if (!ping_main(ckp))
goto out;
for (i = 0; i < ckp->btcds; i++) {
server_instance_t *si = ckp->servers[i];
if (server_alive(ckp, si, false)) {
alive = si; alive = si;
break; break;
} }
}
if (!alive) { if (!alive) {
LOGWARNING("CRITICAL: No bitcoinds active!"); LOGWARNING("CRITICAL: No bitcoinds active!");
sleep(5); sleep(5);

Loading…
Cancel
Save