From 836f2f3ff2560faebc16d6a2e5bb2e7a157b4ab1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Sat, 7 Feb 2015 17:41:47 +1100 Subject: [PATCH] Avoid extra call to get subscription from generator by pushing it with the subscribe message to the stratifier --- src/generator.c | 31 ++++++++++++------------------- src/stratifier.c | 16 ++++++---------- 2 files changed, 18 insertions(+), 29 deletions(-) diff --git a/src/generator.c b/src/generator.c index b0d4c378..df8d9999 100644 --- a/src/generator.c +++ b/src/generator.c @@ -1131,30 +1131,22 @@ static proxy_instance_t *proxy_by_id(gdata_t *gdata, const int id) return proxi; } -static void send_subscribe(gdata_t *gdata, proxy_instance_t *cproxy, int *sockd, const char *buf) +static proxy_instance_t *current_proxy(gdata_t *gdata); + +static char *get_subscribe(ckpool_t *ckp, proxy_instance_t *proxi) { - proxy_instance_t *proxi; + gdata_t *gdata = ckp->data; json_t *json_msg; - int id = 0; char *msg; - sscanf(buf, "getsubscribe=%d", &id); - proxi = proxy_by_id(gdata, id); - if (unlikely(!proxi || !proxi->nonce2len)) { - ASPRINTF(&msg, "notready"); - goto out_send; - } JSON_CPACK(json_msg, "{sisssisb}", "proxy", proxi->id, "enonce1", proxi->enonce1, "nonce2len", proxi->nonce2len, - "reconnect", proxi == cproxy ? true : false); + "reconnect", proxi == current_proxy(gdata) ? true : false); msg = json_dumps(json_msg, JSON_NO_UTF8); json_decref(json_msg); -out_send: - send_unix_msg(*sockd, msg); - free(msg); - _Close(sockd); + return msg; } static void send_notify(gdata_t *gdata, int *sockd, const char *buf) @@ -1421,11 +1413,14 @@ out: /* Close and invalidate the file handle */ Close(cs->fd); } else { - char msg[128]; + char *msg, *buf; keep_sockalive(cs->fd); - snprintf(msg, 127, "subscribe=%d", proxi->id); - send_proc(ckp->stratifier, msg); + msg = get_subscribe(ckp, proxi); + ASPRINTF(&buf, "subscribe=%s", msg); + free(msg); + send_proc(ckp->stratifier, buf); + free(buf); proxi->notified = false; } return ret; @@ -1734,8 +1729,6 @@ retry: if (cmdmatch(buf, "shutdown")) { ret = 0; goto out; - } else if (cmdmatch(buf, "getsubscribe")) { - send_subscribe(gdata, proxi, &sockd, buf); } else if (cmdmatch(buf, "getnotify")) { send_notify(gdata, &sockd, buf); } else if (cmdmatch(buf, "getdiff")) { diff --git a/src/stratifier.c b/src/stratifier.c index ec0238a4..06f92b77 100644 --- a/src/stratifier.c +++ b/src/stratifier.c @@ -1044,31 +1044,27 @@ static void update_subscribe(ckpool_t *ckp, const char *cmd) { sdata_t *sdata = ckp->data; bool reconnect = true; - char *buf, *msg; + const char *buf; proxy_t *proxy; json_t *val; int id = 0; - sscanf(cmd, "subscribe=%d", &id); - ASPRINTF(&msg, "getsubscribe=%d", id); - buf = send_recv_proc(ckp->generator, msg); - dealloc(msg); - if (unlikely(!buf)) { - LOGWARNING("Failed to get subscribe from generator in update_subscribe"); - drop_allclients(ckp); + if (unlikely(strlen(cmd) < 11)) { + LOGWARNING("Received zero length string for subscribe in update_subscribe"); return; } + buf = cmd + 10; LOGDEBUG("Update subscribe: %s", buf); if (unlikely(!safecmp(buf, "notready"))) { LOGNOTICE("Generator not ready to send subscribe for proxy %d", id); return; } val = json_loads(buf, 0, NULL); - free(buf); if (unlikely(!val)) { - LOGWARNING("Failed to json decode getsubscribe response in update_subscribe"); + LOGWARNING("Failed to json decode subscribe response in update_subscribe"); return; } + json_get_int(&id, val, "proxy"); LOGNOTICE("Got updated subscribe for proxy %d", id);