Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init logic to retry http request if there are connection problem #26

Conversation

vincenzopalazzo
Copy link
Member

Hi,

In this PR I propose the idea described inside this issue #16. In most of cases the connection can have problems, e.g: The VPN connection crashed during a esplora fetch operation, and this cause the crash of lightningd. Or the esplora don't have a feerate available for the last block and with this code, the plugin can wait before to retry the operation.

In this PR I wrote the not maintainable code but I promise that if this logic work I will refactoring it with a call back function :-)

I hope this can help!

Signed-off-by: Vincenzo Palazzo vincenzopalazzodev@gmail.com

Signed-off-by: Vincenzo Palazzo <vincenzopalazzodev@gmail.com>
chunk.memory = tal_arr(ctx, u8, 64);
chunk.size = 0;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trailing space

@@ -76,47 +79,78 @@ static size_t write_memory_callback(void *contents, size_t size, size_t nmemb,

static u8 *request(const tal_t *ctx, const char *url, const bool post,
const char *data) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spurious CR insertion ?

CURL *curl;
CURLcode res;
curl = curl_easy_init();
if (!curl) {
plugin_log(cmd->plugin, LOG_UNUSUAL, "Error with lincurl init call");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
plugin_log(cmd->plugin, LOG_UNUSUAL, "Error with lincurl init call");
plugin_log(cmd->plugin, LOG_BROKEN, "Could not get a curl context");

struct curl_memory_data chunk;
const struct command *cmd = (struct command*) ctx;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ouch. No, a tal_t struct cannot be cast to a command one!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ops! Is i want the log here I could add another parameter to the request? only solution?

Comment on lines +118 to +128
retrycurl:
res = curl_easy_perform(curl);

if (res != CURLE_OK) {
if (time < retry_time){
time++;
plugin_log(cmd->plugin, LOG_DBG, "Curl return a result different to CURLE_OK, it is %d", CURLE_OK);
sleep(wait_time);
plugin_log(cmd->plugin, LOG_DBG, "%ld: Retry curl request to URL: %s", time, url);
goto retrycurl;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I propose:

for (;;) {
    res = curl_easy_perform(curl);
    if (res == CURLE_OK)
        break;
    if (++time > retry_time)
        return tal_free(chunk.memory);
    sleep(wait_time);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very cool :-) I was too excited to use the goto :-P

Comment on lines +133 to +145
time = 0;
//FIXME(vincenzopalazzo): Is the idea pass the test, Is good refactoring this code to write it only one times
//and execut if in different part of code.
retryinfo:
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response_code);
if (response_code != 200) {
if(time < retry_time){
time++;
plugin_log(cmd->plugin, LOG_DBG, "bad code in the info answare! wait %d before retry", wait_time);
sleep(wait_time);
plugin_log(cmd->plugin, LOG_DBG, "%ld: Retry info request to URL=%s", time, url);
goto retryinfo;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just make my above proposition a function ?


static char *endpoint = NULL;
static char *cainfo_path = NULL;
static char *capath = NULL;
static u64 verbose = 0;
static int wait_time = 60; //Time express in second
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is way too long. I think it can just be a hardcoded value in a make_curl_request function ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean the time is too long?

return tal_free(chunk.memory);
}
curl_easy_cleanup(curl);
tal_resize(&chunk.memory, chunk.size);
return chunk.memory;
}

static char *request_get(const tal_t *ctx, const char *url) {
static char *request_get(const tal_t *ctx, const char *url) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spurious space ?

@@ -148,7 +182,7 @@ static struct command_result *getchaininfo(struct command *cmd,
const char *buf UNUSED,
const jsmntok_t *toks UNUSED) {
char *err;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

spurious space ?

@darosior
Copy link
Contributor

Do you mind if i propose an alternate PR ?

@vincenzopalazzo
Copy link
Member Author

Do you mind if i propose an alternate PR ?

@darosior Absolutly no! I know that my C is very bad at the moment! I can not wait to see you PR :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants