Browse Source

Use ping/pongs to determine if upstream pool is still alive

master
Con Kolivas 9 years ago
parent
commit
901fd3b1cf
  1. 2
      src/ckpool.h
  2. 23
      src/connector.c
  3. 10
      src/stratifier.c

2
src/ckpool.h

@ -277,6 +277,7 @@ enum stratum_msgtype {
SM_WORKINFO, SM_WORKINFO,
SM_SUGGESTDIFF, SM_SUGGESTDIFF,
SM_BLOCK, SM_BLOCK,
SM_PONG,
SM_NONE SM_NONE
}; };
@ -298,6 +299,7 @@ static const char __maybe_unused *stratum_msgs[] = {
"workinfo", "workinfo",
"suggestdiff", "suggestdiff",
"block", "block",
"pong",
"" ""
}; };

23
src/connector.c

@ -1094,11 +1094,21 @@ static void parse_remote_submitblock(ckpool_t *ckp, const json_t *val, const cha
send_proc(ckp->generator, gbt_block); send_proc(ckp->generator, gbt_block);
} }
static void ping_upstream(cdata_t *cdata)
{
char *buf;
ASPRINTF(&buf, "{\"method\":\"ping\"}\n");
ckmsgq_add(cdata->upstream_sends, buf);
}
static void *urecv_process(void *arg) static void *urecv_process(void *arg)
{ {
ckpool_t *ckp = (ckpool_t *)arg; ckpool_t *ckp = (ckpool_t *)arg;
cdata_t *cdata = ckp->data; cdata_t *cdata = ckp->data;
connsock_t *cs = &cdata->upstream_cs; connsock_t *cs = &cdata->upstream_cs;
bool alive = true;
ckp->proxy = true; ckp->proxy = true;
rename_proc("ureceiver"); rename_proc("ureceiver");
@ -1114,12 +1124,16 @@ static void *urecv_process(void *arg)
cksem_wait(&cs->sem); cksem_wait(&cs->sem);
ret = read_socket_line(cs, &timeout); ret = read_socket_line(cs, &timeout);
if (ret < 1) { if (ret < 1) {
if (likely(!ret)) ping_upstream(cdata);
if (likely(!ret)) {
LOGDEBUG("No message from upstream pool"); LOGDEBUG("No message from upstream pool");
else } else {
LOGNOTICE("Failed to read from upstream pool"); LOGNOTICE("Failed to read from upstream pool");
alive = false;
}
goto nomsg; goto nomsg;
} }
alive = true;
val = json_loads(cs->buf, 0, NULL); val = json_loads(cs->buf, 0, NULL);
if (unlikely(!val)) { if (unlikely(!val)) {
LOGWARNING("Received non-json msg from upstream pool %s", LOGWARNING("Received non-json msg from upstream pool %s",
@ -1135,10 +1149,15 @@ static void *urecv_process(void *arg)
} }
if (!safecmp(method, "submitblock")) if (!safecmp(method, "submitblock"))
parse_remote_submitblock(ckp, val, cs->buf); parse_remote_submitblock(ckp, val, cs->buf);
else if (!safecmp(method, "pong"))
LOGDEBUG("Received upstream pong");
else else
LOGWARNING("Unrecognised upstream method %s", method); LOGWARNING("Unrecognised upstream method %s", method);
nomsg: nomsg:
cksem_post(&cs->sem); cksem_post(&cs->sem);
if (!alive)
sleep(5);
} }
return NULL; return NULL;
} }

10
src/stratifier.c

@ -5999,6 +5999,14 @@ static void parse_remote_block(sdata_t *sdata, json_t *val, const char *buf)
reset_bestshares(sdata); reset_bestshares(sdata);
} }
static void send_remote_pong(sdata_t *sdata, stratum_instance_t *client)
{
json_t *json_msg;
JSON_CPACK(json_msg, "{ss}", "method", "pong");
stratum_add_send(sdata, json_msg, client->id, SM_PONG);
}
static void parse_trusted_msg(ckpool_t *ckp, sdata_t *sdata, json_t *val, const char *buf, static void parse_trusted_msg(ckpool_t *ckp, sdata_t *sdata, json_t *val, const char *buf,
stratum_instance_t *client) stratum_instance_t *client)
{ {
@ -6019,6 +6027,8 @@ static void parse_trusted_msg(ckpool_t *ckp, sdata_t *sdata, json_t *val, const
parse_remote_blocksubmit(ckp, val, buf, client); parse_remote_blocksubmit(ckp, val, buf, client);
else if (!safecmp(method, "block")) else if (!safecmp(method, "block"))
parse_remote_block(sdata, val, buf); parse_remote_block(sdata, val, buf);
else if (!safecmp(method, "ping"))
send_remote_pong(sdata, client);
else else
LOGWARNING("unrecognised trusted message %s", buf); LOGWARNING("unrecognised trusted message %s", buf);
} }

Loading…
Cancel
Save