Implement load balancing algorithm by rotating requests to each pool.
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
diff --git a/main.c b/main.c
index 6f098ba..6aae382 100644
--- a/main.c
+++ b/main.c
@@ -731,7 +731,10 @@ static void curses_print_status(int thr_id)
local_work, total_lo, total_ro, scan_intensity);
wclrtoeol(statuswin);
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);
wmove(statuswin, 5, 0);
wprintw(statuswin, " Block %s started: %s", current_block + 4, blockdate);
@@ -888,9 +891,24 @@ out_nofree:
static const char *rpc_req =
"{\"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)
{
- struct pool *pool = current_pool();
+ struct pool *pool = select_pool();
json_t *val;
bool rc = false;
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);
work->pool = pool;
+ total_getworks++;
+ pool->getwork_requested++;
json_decref(val);
out:
@@ -1498,15 +1518,12 @@ static bool queue_request(void)
{
int maxq = opt_queue + mining_threads;
struct workio_cmd *wc;
- struct pool *pool;
/* If we've been generating lots of local work we may already have
* enough in the queue */
if (requests_queued() >= maxq || real_staged() >= maxq)
return true;
- pool = current_pool();
-
/* fill out work request message */
wc = calloc(1, sizeof(*wc));
if (unlikely(!wc)) {
@@ -1524,8 +1541,6 @@ static bool queue_request(void)
workio_cmd_free(wc);
return false;
}
- total_getworks++;
- pool->getwork_requested++;
inc_queued();
return true;
}