Re-use CURL object, thereby caching DNS and HTTP connections where possible.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
diff --git a/cpu-miner.c b/cpu-miner.c
index f288471..e78c48f 100644
--- a/cpu-miner.c
+++ b/cpu-miner.c
@@ -23,6 +23,7 @@
#include <pthread.h>
#include <getopt.h>
#include <jansson.h>
+#include <curl/curl.h>
#include "compat.h"
#include "miner.h"
@@ -199,7 +200,7 @@ err_out:
return false;
}
-static void submit_work(struct work *work)
+static void submit_work(CURL *curl, struct work *work)
{
char *hexstr = NULL;
json_t *val, *res;
@@ -221,7 +222,7 @@ static void submit_work(struct work *work)
fprintf(stderr, "DBG: sending RPC call:\n%s", s);
/* issue JSON-RPC request */
- val = json_rpc_call(rpc_url, userpass, s);
+ val = json_rpc_call(curl, rpc_url, userpass, s);
if (!val) {
fprintf(stderr, "submit_work json_rpc_call failed\n");
goto out;
@@ -259,6 +260,13 @@ static void *miner_thread(void *thr_id_int)
static const char *rpc_req =
"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
uint32_t max_nonce = 0xffffff;
+ CURL *curl;
+
+ curl = curl_easy_init();
+ if (!curl) {
+ fprintf(stderr, "CURL initialization failed\n");
+ return NULL;
+ }
while (1) {
struct work work __attribute__((aligned(128)));
@@ -268,7 +276,7 @@ static void *miner_thread(void *thr_id_int)
bool rc;
/* obtain new work from bitcoin */
- val = json_rpc_call(rpc_url, userpass, rpc_req);
+ val = json_rpc_call(curl, rpc_url, userpass, rpc_req);
if (!val) {
fprintf(stderr, "json_rpc_call failed, ");
@@ -369,11 +377,13 @@ static void *miner_thread(void *thr_id_int)
/* if nonce found, submit work */
if (rc)
- submit_work(&work);
+ submit_work(curl, &work);
failures = 0;
}
+ curl_easy_cleanup(curl);
+
return NULL;
}
diff --git a/miner.h b/miner.h
index 539b5d6..314796b 100644
--- a/miner.h
+++ b/miner.h
@@ -5,6 +5,7 @@
#include <stdint.h>
#include <sys/time.h>
#include <jansson.h>
+#include <curl/curl.h>
#ifdef __SSE2__
#define WANT_SSE2_4WAY 1
@@ -45,7 +46,7 @@ static inline void swap256(void *dest_p, const void *src_p)
extern bool opt_debug;
extern bool opt_protocol;
extern const uint32_t sha256_init_state[];
-extern json_t *json_rpc_call(const char *url, const char *userpass,
+extern json_t *json_rpc_call(CURL *curl, const char *url, const char *userpass,
const char *rpc_req);
extern char *bin2hex(unsigned char *p, size_t len);
extern bool hex2bin(unsigned char *p, const char *hexstr, size_t len);
diff --git a/util.c b/util.c
index 25d6d08..b60b0ac 100644
--- a/util.c
+++ b/util.c
@@ -80,9 +80,9 @@ static size_t upload_data_cb(void *ptr, size_t size, size_t nmemb,
return len;
}
-json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req)
+json_t *json_rpc_call(CURL *curl, const char *url,
+ const char *userpass, const char *rpc_req)
{
- CURL *curl;
json_t *val, *err_val, *res_val;
int rc;
struct data_buffer all_data = { };
@@ -92,11 +92,7 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req
char len_hdr[64];
char curl_err_str[CURL_ERROR_SIZE];
- curl = curl_easy_init();
- if (!curl) {
- fprintf(stderr, "CURL initialization failed, aborting JSON-RPC call\n");
- return NULL;
- }
+ /* it is assumed that 'curl' is freshly [re]initialized at this pt */
if (opt_protocol)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
@@ -172,13 +168,13 @@ json_t *json_rpc_call(const char *url, const char *userpass, const char *rpc_req
databuf_free(&all_data);
curl_slist_free_all(headers);
- curl_easy_cleanup(curl);
+ curl_easy_reset(curl);
return val;
err_out:
databuf_free(&all_data);
curl_slist_free_all(headers);
- curl_easy_cleanup(curl);
+ curl_easy_reset(curl);
return NULL;
}