From e7f36cbe4081ec9f419891aecb41c6f73e0aac2f Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 24 Jul 2017 20:53:01 +1000 Subject: [PATCH] Various proxy updates. --- src/generator.c | 100 +++++++++++++++++++++++++++++++++++++++-------- src/stratifier.c | 23 ++++------- 2 files changed, 91 insertions(+), 32 deletions(-) diff --git a/src/generator.c b/src/generator.c index 7bd73ed7..b2384302 100644 --- a/src/generator.c +++ b/src/generator.c @@ -2133,6 +2133,9 @@ static void *proxy_reconnect(void *arg) pthread_detach(pthread_self()); proxy_alive(ckp, proxy, cs, true); + sleep(3); + /* Delay resetting this flag to throttle how frequently we can create + * this thread and check proxy_alive */ proxy->reconnecting = false; return NULL; } @@ -2818,8 +2821,8 @@ static void parse_ableproxy(gdata_t *gdata, const int sockd, const char *buf, bo res = json_errormsg("Proxy id %d not found", id); goto out; } - JSON_CPACK(res, "{si,ss,ss,ss}", "id", proxy->id, "url", proxy->url, - "auth", proxy->auth, "pass", proxy->pass); + JSON_CPACK(res, "{si,ss, ss,ss,ss}", "id", proxy->id, "url", proxy->url, + "baseurl", proxy->baseurl,"auth", proxy->auth, "pass", proxy->pass); if (proxy->disabled != disable) { proxy->disabled = disable; LOGNOTICE("%sabling proxy %d:%s", disable ? "Dis" : "En", id, proxy->url); @@ -2899,12 +2902,11 @@ static void send_stats(gdata_t *gdata, const int sockd) send_api_response(val, sockd); } -static json_t *proxystats(proxy_instance_t *proxy) +/* Entered with parent proxy locked */ +static json_t *__proxystats(proxy_instance_t *proxy, proxy_instance_t *parent, bool discrete) { - proxy_instance_t *parent = proxy->parent; json_t *val = json_object(); - mutex_lock(&parent->proxy_lock); /* Opportunity to update hashrate just before we report it without * needing to check on idle proxies regularly */ __decay_proxy(proxy, parent, 0); @@ -2928,22 +2930,52 @@ static json_t *proxystats(proxy_instance_t *proxy) json_set_double(val, "tdsps60", proxy->tdsps60); json_set_double(val, "tdsps1440", proxy->tdsps1440); } - json_set_double(val, "dsps1", proxy->dsps1); - json_set_double(val, "dsps5", proxy->dsps5); - json_set_double(val, "dsps60", proxy->dsps60); - json_set_double(val, "dsps1440", proxy->dsps1440); - json_set_double(val, "accepted", proxy->diff_accepted); - json_set_double(val, "rejected", proxy->diff_rejected); + if (discrete) { + json_set_double(val, "dsps1", proxy->dsps1); + json_set_double(val, "dsps5", proxy->dsps5); + json_set_double(val, "dsps60", proxy->dsps60); + json_set_double(val, "dsps1440", proxy->dsps1440); + json_set_double(val, "accepted", proxy->diff_accepted); + json_set_double(val, "rejected", proxy->diff_rejected); + } json_set_int(val, "lastshare", proxy->last_share.tv_sec); json_set_bool(val, "global", proxy->global); json_set_bool(val, "disabled", proxy->disabled); json_set_bool(val, "alive", proxy->alive); json_set_int(val, "maxclients", proxy->clients_per_proxy); + + return val; +} + +static json_t *proxystats(proxy_instance_t *proxy, bool discrete) +{ + proxy_instance_t *parent = proxy->parent; + json_t *val; + + mutex_lock(&parent->proxy_lock); + val = __proxystats(proxy, parent, discrete); mutex_unlock(&parent->proxy_lock); return val; } +static json_t *all_proxystats(gdata_t *gdata) +{ + json_t *res, *arr_val = json_array(); + proxy_instance_t *proxy, *tmp; + + mutex_lock(&gdata->lock); + HASH_ITER(hh, gdata->proxies, proxy, tmp) { + mutex_unlock(&gdata->lock); + json_array_append_new(arr_val, proxystats(proxy, false)); + mutex_lock(&gdata->lock); + } + mutex_unlock(&gdata->lock); + + JSON_CPACK(res, "{so}", "proxy", arr_val); + return res; +} + static void parse_proxystats(gdata_t *gdata, const int sockd, const char *buf) { json_t *val = NULL, *res = NULL; @@ -2954,11 +2986,11 @@ static void parse_proxystats(gdata_t *gdata, const int sockd, const char *buf) val = json_loads(buf, 0, &err_val); if (unlikely(!val)) { - res = json_encode_errormsg(&err_val); - goto out; + res = all_proxystats(gdata); + goto out_noval; } if (!json_get_int(&id, val, "id")) { - res = json_errormsg("Failed to find id key"); + res = all_proxystats(gdata); goto out; } if (!json_get_int(&subid, val, "subid")) @@ -2974,10 +3006,42 @@ static void parse_proxystats(gdata_t *gdata, const int sockd, const char *buf) res = json_errormsg("Proxy id %d:%d not found", id, subid); goto out; } - res = proxystats(proxy); + res = proxystats(proxy, true); out: - if (val) - json_decref(val); + json_decref(val); +out_noval: + send_api_response(res, sockd); +} + +static void send_subproxystats(gdata_t *gdata, const int sockd) +{ + json_t *res, *arr_val = json_array(); + proxy_instance_t *parent, *tmp; + + mutex_lock(&gdata->lock); + HASH_ITER(hh, gdata->proxies, parent, tmp) { + json_t *val, *subarr_val = json_array(); + proxy_instance_t *subproxy, *subtmp; + + mutex_unlock(&gdata->lock); + + mutex_lock(&parent->proxy_lock); + HASH_ITER(sh, parent->subproxies, subproxy, subtmp) { + val = __proxystats(subproxy, parent, true); + json_set_int(val, "subid", subproxy->subid); + json_array_append_new(subarr_val, val); + } + mutex_unlock(&parent->proxy_lock); + + JSON_CPACK(val, "{si,so}", + "id", parent->id, + "subproxy", subarr_val); + json_array_append_new(arr_val, val); + mutex_lock(&gdata->lock); + } + mutex_unlock(&gdata->lock); + + JSON_CPACK(res, "{so}", "proxy", arr_val); send_api_response(res, sockd); } @@ -3087,6 +3151,8 @@ retry: parse_ableproxy(gdata, umsg->sockd, buf + 13, true); } else if (cmdmatch(buf, "proxystats")) { parse_proxystats(gdata, umsg->sockd, buf + 11); + } else if (cmdmatch(buf, "subproxystats")) { + send_subproxystats(gdata, umsg->sockd); } else if (cmdmatch(buf, "globaluser")) { parse_globaluser(ckp, gdata, buf + 11); } else if (cmdmatch(buf, "reconnect")) { diff --git a/src/stratifier.c b/src/stratifier.c index 203fc529..b56c1361 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1415,13 +1415,6 @@ static void block_update(ckpool_t *ckp, int *prio) txntable_t *txns; workbase_t *wb; - /* Skip update if we're getting stacked low priority updates too close - * together. */ - if (*prio < GEN_PRIORITY && time(NULL) < sdata->update_time + (ckp->update_interval / 2) && - sdata->current_workbase) { - ret = true; - goto out; - } retry: wb = generator_getbase(ckp); if (unlikely(!wb)) { @@ -2760,7 +2753,6 @@ static void dead_proxyid(sdata_t *sdata, const int id, const int subid, const bo if (proxy) { proxy->dead = true; proxy->deleted = deleted; - set_proxy_prio(sdata, proxy, 0xFFFF); if (!replaced && proxy->global) check_bestproxy(sdata); } @@ -4323,7 +4315,7 @@ static json_t *json_proxyinfo(const proxy_t *proxy) const proxy_t *parent = proxy->parent; json_t *val; - JSON_CPACK(val, "{si,si,si,sf,ss,ss,ss,ss,ss,si,si,si,si,sb,sb,sI,sI,sI,sI,si,si,sb,sb,si}", + JSON_CPACK(val, "{si,si,si,sf,ss,ss,ss,ss,ss,si,si,si,si,sb,sb,sI,sI,sI,sI,sI,si,sb,sb,si}", "id", proxy->id, "subid", proxy->subid, "priority", proxy_prio(parent), "diff", proxy->diff, "baseurl", proxy->baseurl, "url", proxy->url, "auth", proxy->auth, "pass", proxy->pass, @@ -4501,6 +4493,7 @@ retry: end_t = time(NULL); if (end_t - sdata->update_time >= ckp->update_interval) { + sdata->update_time = end_t; if (!ckp->proxy) { LOGDEBUG("%ds elapsed in strat_loop, updating gbt base", ckp->update_interval); @@ -5397,16 +5390,17 @@ static void set_worker_mindiff(ckpool_t *ckp, const char *workername, int mindif static void parse_worker_diffs(ckpool_t *ckp, json_t *worker_array) { + const char *workername; json_t *worker_entry; - char *workername; size_t index; int mindiff; json_array_foreach(worker_array, index, worker_entry) { - json_get_string(&workername, worker_entry, "workername"); + workername = json_string_value(json_object_get(worker_entry, "workername")); + if (!workername) + continue; json_get_int(&mindiff, worker_entry, "difficultydefault"); set_worker_mindiff(ckp, workername, mindiff); - dealloc(workername); } } @@ -7725,15 +7719,14 @@ static void parse_ckdb_cmd(ckpool_t *ckp, const char *cmd) } res_val = json_object_get(val, "diffchange"); json_array_foreach(res_val, index, arr_val) { - char *workername; + const char *workername; int mindiff; - json_get_string(&workername, arr_val, "workername"); + workername = json_string_value(json_object_get(arr_val, "workername")); if (!workername) continue; json_get_int(&mindiff, arr_val, "difficultydefault"); set_worker_mindiff(ckp, workername, mindiff); - dealloc(workername); } json_decref(val); }