From e69db64eb0a84bee922ba190915a426191299eaf Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 7 Oct 2014 23:38:54 +1100 Subject: [PATCH 1/3] Add commands to reject and accept further connections --- src/ckpool.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/ckpool.c b/src/ckpool.c index 31746936..1a2ce6bd 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -286,6 +286,14 @@ retry: Close(connfd); } else LOGWARNING("Failed to send_procmsg to connector"); + } else if (cmdmatch(buf, "accept")) { + LOGWARNING("Listener received accept message, accepting clients"); + send_unix_msg(sockd, "accepting"); + send_procmsg(ckp->connector, "accept"); + } else if (cmdmatch(buf, "reject")) { + LOGWARNING("Listener received reject message, rejecting clients"); + send_unix_msg(sockd, "rejecting"); + send_procmsg(ckp->connector, "reject"); } else if (cmdmatch(buf, "restart")) { LOGWARNING("Listener received restart message, attempting handover"); send_unix_msg(sockd, "restarting"); From 5532de0f1b7db988568b012e3fbee4ffefaa6633 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Tue, 7 Oct 2014 23:47:36 +1100 Subject: [PATCH 2/3] Implement a stratum reconnect function --- src/ckpool.c | 8 ++++++-- src/stratifier.c | 13 +++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index 1a2ce6bd..a013e5d2 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -288,12 +288,16 @@ retry: LOGWARNING("Failed to send_procmsg to connector"); } else if (cmdmatch(buf, "accept")) { LOGWARNING("Listener received accept message, accepting clients"); - send_unix_msg(sockd, "accepting"); send_procmsg(ckp->connector, "accept"); + send_unix_msg(sockd, "accepting"); } else if (cmdmatch(buf, "reject")) { LOGWARNING("Listener received reject message, rejecting clients"); - send_unix_msg(sockd, "rejecting"); send_procmsg(ckp->connector, "reject"); + send_unix_msg(sockd, "rejecting"); + } else if (cmdmatch(buf, "reconnect")) { + LOGWARNING("Listener received request to send reconnect to clients"); + send_procmsg(ckp->stratifier, "reconnect"); + send_unix_msg(sockd, "reconnecting"); } else if (cmdmatch(buf, "restart")) { LOGWARNING("Listener received restart message, attempting handover"); send_unix_msg(sockd, "restarting"); diff --git a/src/stratifier.c b/src/stratifier.c index 47ef38ca..6bc8e952 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1138,6 +1138,17 @@ static void stratum_broadcast_message(const char *msg) stratum_broadcast(json_msg); } +/* Send a generic reconnect to all clients without parameters to make them + * reconnect to the same server. */ +static void reconnect_clients(void) +{ + json_t *json_msg; + + JSON_CPACK(json_msg, "{sosss[]}", "id", json_null(), "method", "client.reconnect", + "params"); + stratum_broadcast(json_msg); +} + static void block_solve(ckpool_t *ckp) { char cdfield[64]; @@ -1281,6 +1292,8 @@ retry: drop_allclients(ckp); } else if (cmdmatch(buf, "block")) { block_solve(ckp); + } else if (cmdmatch(buf, "reconnect")) { + reconnect_clients(); } else if (cmdmatch(buf, "loglevel")) { sscanf(buf, "loglevel=%d", &ckp->loglevel); } else { From 86fa46069c916699c33c68f9d7d4497abb84cd81 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 8 Oct 2014 00:14:00 +1100 Subject: [PATCH 3/3] Rework the handover mechanism to make the old client reject further clients, then send a reconnect, then shutdown and wait before killing it --- src/ckpool.c | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/src/ckpool.c b/src/ckpool.c index a013e5d2..2cade1eb 100644 --- a/src/ckpool.c +++ b/src/ckpool.c @@ -784,7 +784,7 @@ int process_exit(ckpool_t *ckp, proc_instance_t *pi, int ret) LOGWARNING("%s %s exiting with return code %d, shutting down!", ckp->name, pi->processname, ret); send_proc(&ckp->main, "shutdown"); - sleep(1); + cksleep_ms(100); ret = 1; } else /* Should be part of a normal shutdown */ LOGNOTICE("%s %s exited normally", ckp->name, pi->processname); @@ -856,8 +856,8 @@ static void sighandler(int sig) cancel_join_pthread(&ckp->pth_watchdog); __shutdown_children(ckp, SIGUSR1); - /* Wait a second, then send SIGKILL */ - sleep(1); + /* Wait, then send SIGKILL */ + cksleep_ms(100); __shutdown_children(ckp, SIGKILL); cancel_pthread(&ckp->pth_listener); exit(0); @@ -1106,6 +1106,18 @@ static struct option long_options[] = { }; #endif +static void send_recv_path(const char *path, const char *msg) +{ + int sockd = open_unix_client(path); + char *response; + + send_unix_msg(sockd, msg); + response = recv_unix_msg(sockd); + LOGWARNING("Received: %s in response to %s request", response, msg); + dealloc(response); + Close(sockd); +} + int main(int argc, char **argv) { struct sigaction handler; @@ -1324,13 +1336,15 @@ int main(int argc, char **argv) if (sockd > 0 && send_unix_msg(sockd, "getfd")) { ckp.oldconnfd = get_fd(sockd); - Close(sockd); - sockd = open_unix_client(ckp.main.us.path); - send_unix_msg(sockd, "shutdown"); + + send_recv_path(ckp.main.us.path, "reject"); + send_recv_path(ckp.main.us.path, "reconnect"); + send_recv_path(ckp.main.us.path, "shutdown"); + cksleep_ms(500); + if (ckp.oldconnfd > 0) LOGWARNING("Inherited old socket with new file descriptor %d!", ckp.oldconnfd); - Close(sockd); } }