Merge pull request #204 from EchterAgo/jsonrpc_speedup
JSONRPC: Increase network performance
This commit is contained in:
commit
872f02b1f0
1 changed files with 48 additions and 15 deletions
57
util.c
57
util.c
|
@ -38,15 +38,18 @@
|
|||
#include "miner.h"
|
||||
#include "elist.h"
|
||||
|
||||
struct data_buffer {
|
||||
void *buf;
|
||||
size_t len;
|
||||
};
|
||||
|
||||
struct header_info {
|
||||
char *lp_path;
|
||||
char *reason;
|
||||
char *stratum_url;
|
||||
size_t content_length;
|
||||
};
|
||||
|
||||
struct data_buffer {
|
||||
void *buf;
|
||||
size_t len;
|
||||
size_t allocated;
|
||||
struct header_info *headers;
|
||||
};
|
||||
|
||||
struct tq_ent {
|
||||
|
@ -182,21 +185,47 @@ static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
|
|||
{
|
||||
struct data_buffer *db = user_data;
|
||||
size_t len = size * nmemb;
|
||||
size_t oldlen, newlen;
|
||||
size_t newalloc, reqalloc;
|
||||
void *newmem;
|
||||
static const unsigned char zero = 0;
|
||||
static const size_t max_realloc_increase = 8 * 1024 * 1024;
|
||||
static const size_t initial_alloc = 16 * 1024;
|
||||
|
||||
oldlen = db->len;
|
||||
newlen = oldlen + len;
|
||||
/* minimum required allocation size */
|
||||
reqalloc = db->len + len + 1;
|
||||
|
||||
newmem = realloc(db->buf, newlen + 1);
|
||||
if (reqalloc > db->allocated) {
|
||||
if (db->len > 0) {
|
||||
newalloc = db->allocated * 2;
|
||||
} else {
|
||||
if (db->headers->content_length > 0)
|
||||
newalloc = db->headers->content_length + 1;
|
||||
else
|
||||
newalloc = initial_alloc;
|
||||
}
|
||||
|
||||
if (db->headers->content_length == 0) {
|
||||
/* limit the maximum buffer increase */
|
||||
if (newalloc - db->allocated > max_realloc_increase)
|
||||
newalloc = db->allocated + max_realloc_increase;
|
||||
}
|
||||
|
||||
/* ensure we have a big enough allocation */
|
||||
if (reqalloc > newalloc)
|
||||
newalloc = reqalloc;
|
||||
|
||||
newmem = realloc(db->buf, newalloc);
|
||||
if (!newmem)
|
||||
return 0;
|
||||
|
||||
db->buf = newmem;
|
||||
db->len = newlen;
|
||||
memcpy(db->buf + oldlen, ptr, len);
|
||||
memcpy(db->buf + newlen, &zero, 1); /* null terminate */
|
||||
db->allocated = newalloc;
|
||||
}
|
||||
|
||||
memcpy(db->buf + db->len, ptr, len); /* append new data */
|
||||
memcpy(db->buf + db->len + len, &zero, 1); /* null terminate */
|
||||
|
||||
db->len += len;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
@ -252,6 +281,9 @@ static size_t resp_hdr_cb(void *ptr, size_t size, size_t nmemb, void *user_data)
|
|||
val = NULL;
|
||||
}
|
||||
|
||||
if (!strcasecmp("Content-Length", key))
|
||||
hi->content_length = strtoul(val, NULL, 10);
|
||||
|
||||
out:
|
||||
free(key);
|
||||
free(val);
|
||||
|
@ -317,6 +349,7 @@ json_t *json_rpc_call(CURL *curl, const char *url,
|
|||
long timeout = (flags & JSON_RPC_LONGPOLL) ? opt_timeout : 30;
|
||||
struct header_info hi = {0};
|
||||
|
||||
all_data.headers = &hi;
|
||||
/* it is assumed that 'curl' is freshly [re]initialized at this pt */
|
||||
|
||||
if (opt_protocol)
|
||||
|
|
Loading…
Reference in a new issue