From 521025aa75cc30eca50c48b1e9ab5f6dd94c58a9 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Wed, 20 Jul 2011 13:35:55 +1000 Subject: [PATCH] Implement load balancing algorithm by rotating requests to each pool. --- main.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/main.c b/main.c index 6f098bab..6aae3825 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; }