Browse Source

Implement load balancing algorithm by rotating requests to each pool.

Con Kolivas 14 years ago
parent
commit
521025aa75
1 changed files with 22 additions and 7 deletions
  1. 22 7
      main.c

+ 22 - 7
main.c

@@ -731,7 +731,10 @@ static void curses_print_status(int thr_id)
 		local_work, total_lo, total_ro, scan_intensity);
 		local_work, total_lo, total_ro, scan_intensity);
 	wclrtoeol(statuswin);
 	wclrtoeol(statuswin);
 	wmove(statuswin, 4, 0);
 	wmove(statuswin, 4, 0);
-	wprintw(statuswin, " Connected to %s as user %s", pool->rpc_url, pool->rpc_user);
+	if (pool_strategy == POOL_LOADBALANCE && total_pools > 1)
+		wprintw(statuswin, " Connected to multiple pools");
+	else
+		wprintw(statuswin, " Connected to %s as user %s", pool->rpc_url, pool->rpc_user);
 	wclrtoeol(statuswin);
 	wclrtoeol(statuswin);
 	wmove(statuswin, 5, 0);
 	wmove(statuswin, 5, 0);
 	wprintw(statuswin, " Block %s  started: %s", current_block + 4, blockdate);
 	wprintw(statuswin, " Block %s  started: %s", current_block + 4, blockdate);
@@ -888,9 +891,24 @@ out_nofree:
 static const char *rpc_req =
 static const char *rpc_req =
 	"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
 	"{\"method\": \"getwork\", \"params\": [], \"id\":0}\r\n";
 
 
+static int rotating_pool;
+
+/* Select any active pool in a rotating fashion when loadbalance is chosen */
+static inline struct pool *select_pool(void)
+{
+	if (pool_strategy == POOL_LOADBALANCE) {
+		rotating_pool++;
+		if (rotating_pool >= total_pools)
+			rotating_pool = 0;
+		if (!pools[rotating_pool].idle)
+			return &pools[rotating_pool];
+	}
+	return current_pool();
+}
+
 static bool get_upstream_work(struct work *work)
 static bool get_upstream_work(struct work *work)
 {
 {
-	struct pool *pool = current_pool();
+	struct pool *pool = select_pool();
 	json_t *val;
 	json_t *val;
 	bool rc = false;
 	bool rc = false;
 	CURL *curl = curl_easy_init();
 	CURL *curl = curl_easy_init();
@@ -909,6 +927,8 @@ static bool get_upstream_work(struct work *work)
 
 
 	rc = work_decode(json_object_get(val, "result"), work);
 	rc = work_decode(json_object_get(val, "result"), work);
 	work->pool = pool;
 	work->pool = pool;
+	total_getworks++;
+	pool->getwork_requested++;
 
 
 	json_decref(val);
 	json_decref(val);
 out:
 out:
@@ -1498,15 +1518,12 @@ static bool queue_request(void)
 {
 {
 	int maxq = opt_queue + mining_threads;
 	int maxq = opt_queue + mining_threads;
 	struct workio_cmd *wc;
 	struct workio_cmd *wc;
-	struct pool *pool;
 
 
 	/* If we've been generating lots of local work we may already have
 	/* If we've been generating lots of local work we may already have
 	 * enough in the queue */
 	 * enough in the queue */
 	if (requests_queued() >= maxq || real_staged() >= maxq)
 	if (requests_queued() >= maxq || real_staged() >= maxq)
 		return true;
 		return true;
 
 
-	pool = current_pool();
-
 	/* fill out work request message */
 	/* fill out work request message */
 	wc = calloc(1, sizeof(*wc));
 	wc = calloc(1, sizeof(*wc));
 	if (unlikely(!wc)) {
 	if (unlikely(!wc)) {
@@ -1524,8 +1541,6 @@ static bool queue_request(void)
 		workio_cmd_free(wc);
 		workio_cmd_free(wc);
 		return false;
 		return false;
 	}
 	}
-	total_getworks++;
-	pool->getwork_requested++;
 	inc_queued();
 	inc_queued();
 	return true;
 	return true;
 }
 }