Add smart handling of long polling failures

Switch to short scan time if long polling fails, and try
to reactivate it only if the server continues to advertise
the feature in HTTP headers.
This commit is contained in:
pooler 2012-02-12 03:23:06 +01:00
parent 46fe063865
commit 131d5a2374
2 changed files with 22 additions and 17 deletions

View file

@ -582,10 +582,16 @@ static void *longpoll_thread(void *userdata)
{
struct thr_info *mythr = userdata;
CURL *curl = NULL;
char *copy_start, *hdr_path, *lp_url = NULL;
char *copy_start, *hdr_path = NULL, *lp_url = NULL;
bool need_slash = false;
int failures = 0;
curl = curl_easy_init();
if (unlikely(!curl)) {
applog(LOG_ERR, "CURL initialization failed");
goto out;
}
start:
hdr_path = tq_pop(mythr->q, NULL);
if (!hdr_path)
goto out;
@ -611,12 +617,6 @@ static void *longpoll_thread(void *userdata)
applog(LOG_INFO, "Long-polling activated for %s", lp_url);
curl = curl_easy_init();
if (unlikely(!curl)) {
applog(LOG_ERR, "CURL initialization failed");
goto out;
}
while (1) {
json_t *val;
int err;
@ -624,7 +624,6 @@ static void *longpoll_thread(void *userdata)
val = json_rpc_call(curl, lp_url, rpc_userpass, rpc_req,
false, true, &err);
if (likely(val)) {
failures = 0;
applog(LOG_INFO, "LONGPOLL detected new block");
pthread_mutex_lock(&g_work_lock);
if (work_decode(json_object_get(val, "result"), &g_work)) {
@ -639,9 +638,16 @@ static void *longpoll_thread(void *userdata)
pthread_mutex_lock(&g_work_lock);
g_work_time -= LP_SCANTIME;
pthread_mutex_unlock(&g_work_lock);
restart_threads();
if (err != CURLE_OPERATION_TIMEDOUT) {
if (err == CURLE_OPERATION_TIMEDOUT) {
restart_threads();
} else {
have_longpoll = false;
restart_threads();
free(hdr_path);
free(lp_url);
lp_url = NULL;
sleep(opt_fail_pause);
goto start;
}
}
}
@ -903,6 +909,10 @@ int main(int argc, char *argv[])
if (!thr_info)
return 1;
thr_hashrates = (double *) calloc(opt_n_threads, sizeof(double));
if (!thr_hashrates)
return 1;
/* init workio thread info */
work_thr_id = opt_n_threads;
thr = &thr_info[work_thr_id];
@ -934,8 +944,6 @@ int main(int argc, char *argv[])
} else
longpoll_thr_id = -1;
thr_hashrates = (double *) calloc(opt_n_threads, sizeof(double));
/* start mining threads */
for (i = 0; i < opt_n_threads; i++) {
thr = &thr_info[i];

5
util.c
View file

@ -217,13 +217,10 @@ json_t *json_rpc_call(CURL *curl, const char *url,
char curl_err_str[CURL_ERROR_SIZE];
long timeout = opt_timeout;
struct header_info hi = { };
bool lp_scanning = false;
bool lp_scanning = longpoll_scan && !have_longpoll;
/* it is assumed that 'curl' is freshly [re]initialized at this pt */
if (longpoll_scan)
lp_scanning = want_longpoll && !have_longpoll;
if (opt_protocol)
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
curl_easy_setopt(curl, CURLOPT_URL, url);