Skip to content

Commit a4f6168

Browse files
committed
chore: change the connection timeout period to 5 seconds
Users can set the timeout period using environment variables `LINGLONG_CONNECT_TIMEOUT`. Signed-off-by: ice909 <[email protected]>
1 parent 337e420 commit a4f6168

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

external/http/src/apiClient.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -437,8 +437,19 @@ void apiClient_invoke(apiClient_t *apiClient,
437437
apiClient);
438438
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
439439
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
440-
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 1);
441-
440+
const char *timeout_str = getenv("LINGLONG_CONNECT_TIMEOUT");
441+
long timeout = 5;
442+
if (timeout_str != NULL) {
443+
char *endptr;
444+
timeout = strtol(timeout_str, &endptr, 10);
445+
446+
if (endptr == timeout_str || *endptr != '\0') {
447+
timeout = 5;
448+
} else if (timeout <= 0 || timeout > INT_MAX) {
449+
timeout = 5;
450+
}
451+
}
452+
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, timeout);
442453

443454
if(bodyParameters != NULL) {
444455
postData(handle, bodyParameters);

libs/linglong/src/linglong/repo/ostree_repo.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,9 @@ OSTreeRepo::listRemote(const package::FuzzyReference &fuzzyRef) const noexcept
14801480
}
14811481

14821482
if (response == nullptr) {
1483-
return LINGLONG_ERR("failed to send request to remote server");
1483+
return LINGLONG_ERR("failed to send request to remote server\nIf the network is slow, "
1484+
"set a longer timeout via the LINGLONG_CONNECT_TIMEOUT environment "
1485+
"variable (current default: 5 seconds).");
14841486
}
14851487
auto freeResponse = utils::finally::finally([&response] {
14861488
fuzzy_search_app_200_response_free(response);
@@ -1489,7 +1491,10 @@ OSTreeRepo::listRemote(const package::FuzzyReference &fuzzyRef) const noexcept
14891491
if (response->code != 200) {
14901492
QString msg = (response->msg != nullptr)
14911493
? response->msg
1492-
: QString{ "cannot send request to remote server: %1" }.arg(response->code);
1494+
: QString{ "cannot send request to remote server: %1\nIf the network is slow, "
1495+
"set a longer timeout via the LINGLONG_CONNECT_TIMEOUT environment "
1496+
"variable (current default: 5 seconds)." }
1497+
.arg(response->code);
14931498
return LINGLONG_ERR(msg);
14941499
}
14951500

tools/openapi-c-libcurl-client/apiClient.c.mustache

+13-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,19 @@ void apiClient_invoke(apiClient_t *apiClient,
494494
apiClient);
495495
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
496496
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
497-
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, 1);
497+
const char *timeout_str = getenv("LINGLONG_CONNECT_TIMEOUT");
498+
long timeout = 5;
499+
if (timeout_str != NULL) {
500+
char *endptr;
501+
timeout = strtol(timeout_str, &endptr, 10);
502+
503+
if (endptr == timeout_str || *endptr != '\0') {
504+
timeout = 5;
505+
} else if (timeout <= 0 || timeout > INT_MAX) {
506+
timeout = 5;
507+
}
508+
}
509+
curl_easy_setopt(handle, CURLOPT_CONNECTTIMEOUT, timeout);
498510

499511
{{#hasAuthMethods}}
500512
{{#authMethods}}

0 commit comments

Comments
 (0)