Skip to content

Commit 34888a9

Browse files
authoredApr 1, 2021
Sort out cURL timeouts and increase default
1 parent 3560691 commit 34888a9

8 files changed

+18
-73
lines changed
 

‎builtin/settingtypes.txt

+3-4
Original file line numberDiff line numberDiff line change
@@ -1413,9 +1413,8 @@ enable_ipv6 (IPv6) bool true
14131413

14141414
[*Advanced]
14151415

1416-
# Default timeout for cURL, stated in milliseconds.
1417-
# Only has an effect if compiled with cURL.
1418-
curl_timeout (cURL timeout) int 5000
1416+
# Maximum time an interactive request (e.g. server list fetch) may take, stated in milliseconds.
1417+
curl_timeout (cURL interactive timeout) int 20000
14191418

14201419
# Limits number of parallel HTTP requests. Affects:
14211420
# - Media fetch if server uses remote_media setting.
@@ -1424,7 +1423,7 @@ curl_timeout (cURL timeout) int 5000
14241423
# Only has an effect if compiled with cURL.
14251424
curl_parallel_limit (cURL parallel limit) int 8
14261425

1427-
# Maximum time in ms a file download (e.g. a mod download) may take.
1426+
# Maximum time a file download (e.g. a mod download) may take, stated in milliseconds.
14281427
curl_file_download_timeout (cURL file download timeout) int 300000
14291428

14301429
# Makes DirectX work with LuaJIT. Disable if it causes troubles.

‎doc/lua_api.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8394,7 +8394,7 @@ Used by `HTTPApiTable.fetch` and `HTTPApiTable.fetch_async`.
83948394
url = "http://example.org",
83958395

83968396
timeout = 10,
8397-
-- Timeout for connection in seconds. Default is 3 seconds.
8397+
-- Timeout for request to be completed in seconds. Default depends on engine settings.
83988398

83998399
method = "GET", "POST", "PUT" or "DELETE"
84008400
-- The http method to use. Defaults to "GET".

‎src/client/clientmedia.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,6 @@ void ClientMediaDownloader::initialStep(Client *client)
216216

217217
// This is the first time we use httpfetch, so alloc a caller ID
218218
m_httpfetch_caller = httpfetch_caller_alloc();
219-
m_httpfetch_timeout = g_settings->getS32("curl_timeout");
220219

221220
// Set the active fetch limit to curl_parallel_limit or 84,
222221
// whichever is greater. This gives us some leeway so that
@@ -258,8 +257,6 @@ void ClientMediaDownloader::initialStep(Client *client)
258257
remote->baseurl + MTHASHSET_FILE_NAME;
259258
fetch_request.caller = m_httpfetch_caller;
260259
fetch_request.request_id = m_httpfetch_next_id; // == i
261-
fetch_request.timeout = m_httpfetch_timeout;
262-
fetch_request.connect_timeout = m_httpfetch_timeout;
263260
fetch_request.method = HTTP_POST;
264261
fetch_request.raw_data = required_hash_set;
265262
fetch_request.extra_headers.emplace_back(
@@ -432,9 +429,8 @@ void ClientMediaDownloader::startRemoteMediaTransfers()
432429
fetch_request.url = url;
433430
fetch_request.caller = m_httpfetch_caller;
434431
fetch_request.request_id = m_httpfetch_next_id;
435-
fetch_request.timeout = 0; // no data timeout!
436-
fetch_request.connect_timeout =
437-
m_httpfetch_timeout;
432+
fetch_request.timeout =
433+
g_settings->getS32("curl_file_download_timeout");
438434
httpfetch_async(fetch_request);
439435

440436
m_remote_file_transfers.insert(std::make_pair(

‎src/client/clientmedia.h

-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ class ClientMediaDownloader
137137
// Status of remote transfers
138138
unsigned long m_httpfetch_caller;
139139
unsigned long m_httpfetch_next_id = 0;
140-
long m_httpfetch_timeout = 0;
141140
s32 m_httpfetch_active = 0;
142141
s32 m_httpfetch_active_limit = 0;
143142
s32 m_outstanding_hash_sets = 0;

‎src/convert_json.cpp

+1-46
Original file line numberDiff line numberDiff line change
@@ -17,56 +17,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
1717
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1818
*/
1919

20-
#include <vector>
2120
#include <iostream>
2221
#include <sstream>
22+
#include <memory>
2323

2424
#include "convert_json.h"
25-
#include "content/mods.h"
26-
#include "config.h"
27-
#include "log.h"
28-
#include "settings.h"
29-
#include "httpfetch.h"
30-
#include "porting.h"
31-
32-
Json::Value fetchJsonValue(const std::string &url,
33-
std::vector<std::string> *extra_headers)
34-
{
35-
HTTPFetchRequest fetch_request;
36-
HTTPFetchResult fetch_result;
37-
fetch_request.url = url;
38-
fetch_request.caller = HTTPFETCH_SYNC;
39-
40-
if (extra_headers != NULL)
41-
fetch_request.extra_headers = *extra_headers;
42-
43-
httpfetch_sync(fetch_request, fetch_result);
44-
45-
if (!fetch_result.succeeded) {
46-
return Json::Value();
47-
}
48-
Json::Value root;
49-
std::istringstream stream(fetch_result.data);
50-
51-
Json::CharReaderBuilder builder;
52-
builder.settings_["collectComments"] = false;
53-
std::string errs;
54-
55-
if (!Json::parseFromStream(builder, stream, &root, &errs)) {
56-
errorstream << "URL: " << url << std::endl;
57-
errorstream << "Failed to parse json data " << errs << std::endl;
58-
if (fetch_result.data.size() > 100) {
59-
errorstream << "Data (" << fetch_result.data.size()
60-
<< " bytes) printed to warningstream." << std::endl;
61-
warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
62-
} else {
63-
errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;
64-
}
65-
return Json::Value();
66-
}
67-
68-
return root;
69-
}
7025

7126
void fastWriteJson(const Json::Value &value, std::ostream &to)
7227
{

‎src/convert_json.h

-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include <json/json.h>
2323
#include <ostream>
2424

25-
Json::Value fetchJsonValue(const std::string &url,
26-
std::vector<std::string> *extra_headers);
27-
2825
void fastWriteJson(const Json::Value &value, std::ostream &to);
2926

3027
std::string fastWriteJson(const Json::Value &value);

‎src/defaultsettings.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void set_default_settings()
5656
settings->setDefault("client_unload_unused_data_timeout", "600");
5757
settings->setDefault("client_mapblock_limit", "7500");
5858
settings->setDefault("enable_build_where_you_stand", "false");
59-
settings->setDefault("curl_timeout", "5000");
59+
settings->setDefault("curl_timeout", "20000");
6060
settings->setDefault("curl_parallel_limit", "8");
6161
settings->setDefault("curl_file_download_timeout", "300000");
6262
settings->setDefault("curl_verify_cert", "true");

‎src/httpfetch.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2222
#include <iostream>
2323
#include <sstream>
2424
#include <list>
25-
#include <map>
25+
#include <unordered_map>
2626
#include <cerrno>
2727
#include <mutex>
2828
#include "network/socket.h" // for select()
@@ -37,13 +37,14 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3737
#include "settings.h"
3838
#include "noise.h"
3939

40-
std::mutex g_httpfetch_mutex;
41-
std::map<unsigned long, std::queue<HTTPFetchResult> > g_httpfetch_results;
42-
PcgRandom g_callerid_randomness;
40+
static std::mutex g_httpfetch_mutex;
41+
static std::unordered_map<unsigned long, std::queue<HTTPFetchResult>>
42+
g_httpfetch_results;
43+
static PcgRandom g_callerid_randomness;
4344

4445
HTTPFetchRequest::HTTPFetchRequest() :
4546
timeout(g_settings->getS32("curl_timeout")),
46-
connect_timeout(timeout),
47+
connect_timeout(10 * 1000),
4748
useragent(std::string(PROJECT_NAME_C "/") + g_version_hash + " (" + porting::get_sysinfo() + ")")
4849
{
4950
}
@@ -54,7 +55,7 @@ static void httpfetch_deliver_result(const HTTPFetchResult &fetch_result)
5455
unsigned long caller = fetch_result.caller;
5556
if (caller != HTTPFETCH_DISCARD) {
5657
MutexAutoLock lock(g_httpfetch_mutex);
57-
g_httpfetch_results[caller].push(fetch_result);
58+
g_httpfetch_results[caller].emplace(fetch_result);
5859
}
5960
}
6061

@@ -67,8 +68,7 @@ unsigned long httpfetch_caller_alloc()
6768
// Check each caller ID except HTTPFETCH_DISCARD
6869
const unsigned long discard = HTTPFETCH_DISCARD;
6970
for (unsigned long caller = discard + 1; caller != discard; ++caller) {
70-
std::map<unsigned long, std::queue<HTTPFetchResult> >::iterator
71-
it = g_httpfetch_results.find(caller);
71+
auto it = g_httpfetch_results.find(caller);
7272
if (it == g_httpfetch_results.end()) {
7373
verbosestream << "httpfetch_caller_alloc: allocating "
7474
<< caller << std::endl;
@@ -127,8 +127,7 @@ bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result)
127127
MutexAutoLock lock(g_httpfetch_mutex);
128128

129129
// Check that caller exists
130-
std::map<unsigned long, std::queue<HTTPFetchResult> >::iterator
131-
it = g_httpfetch_results.find(caller);
130+
auto it = g_httpfetch_results.find(caller);
132131
if (it == g_httpfetch_results.end())
133132
return false;
134133

@@ -138,7 +137,7 @@ bool httpfetch_async_get(unsigned long caller, HTTPFetchResult &fetch_result)
138137
return false;
139138

140139
// Pop first result
141-
fetch_result = caller_results.front();
140+
fetch_result = std::move(caller_results.front());
142141
caller_results.pop();
143142
return true;
144143
}

0 commit comments

Comments
 (0)
Please sign in to comment.