Commit c0935a94899bc7261bc98b17a52d7c11b005fde4

Jeff Garzik 2011-02-10T00:41:44

Re-use CURL object, thereby caching DNS and HTTP connections where possible.

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;
 }