Bug 1368455 - Set the connection timeout in the pingsender to 30 seconds. r?gsvelto
The default connection timeout was 5 minutes on Linux and 30 seconds on Windows.
MozReview-Commit-ID: 3Y2L7X4n6W3
--- a/toolkit/components/telemetry/pingsender/pingsender.cpp
+++ b/toolkit/components/telemetry/pingsender/pingsender.cpp
@@ -16,16 +16,19 @@ using std::ifstream;
using std::ios;
using std::string;
namespace PingSender {
const char* kUserAgent = "pingsender/1.0";
const char* kCustomVersionHeader = "X-PingSender-Version: 1.0";
const char* kContentEncodingHeader = "Content-Encoding: gzip";
+// The maximum time, in milliseconds, we allow for the connection phase
+// to the server.
+const uint32_t kConnectionTimeoutMs = 30 * 1000;
/**
* This shared function returns a Date header string for use in HTTP requests.
* See "RFC 7231, section 7.1.1.2: Date" for its specifications.
*/
std::string
GenerateDateHeader()
{
--- a/toolkit/components/telemetry/pingsender/pingsender_unix_common.cpp
+++ b/toolkit/components/telemetry/pingsender/pingsender_unix_common.cpp
@@ -177,16 +177,19 @@ CurlWrapper::Post(const string& url, con
easy_setopt(mCurl, CURLOPT_POSTFIELDSIZE, payload.length());
// Set the contents of the POST data
easy_setopt(mCurl, CURLOPT_POSTFIELDS, payload.c_str());
// Fail if the server returns a 4xx code
easy_setopt(mCurl, CURLOPT_FAILONERROR, 1);
+ // Override the default connection timeout, which is 5 minutes.
+ easy_setopt(mCurl, CURLOPT_CONNECTTIMEOUT_MS, kConnectionTimeoutMs);
+
// Block until the operation is performend. Ignore the response, if the POST
// fails we can't do anything about it.
err = easy_perform(mCurl);
// Whatever happens, we want to clean up the header memory.
slist_free_all(headerChunk);
if (err != CURLE_OK) {
PINGSENDER_LOG("ERROR: Failed to send HTTP request, %s\n",
--- a/toolkit/components/telemetry/pingsender/pingsender_win.cpp
+++ b/toolkit/components/telemetry/pingsender/pingsender_win.cpp
@@ -71,16 +71,26 @@ Post(const string& url, const string& pa
/* lpszProxyBypass */ NULL,
/* dwFlags */ 0));
if (internet.empty()) {
PINGSENDER_LOG("ERROR: Could not open wininet internet handle\n");
return false;
}
+ DWORD timeout = static_cast<DWORD>(kConnectionTimeoutMs);
+ bool rv = InternetSetOption(internet.get(),
+ INTERNET_OPTION_CONNECT_TIMEOUT,
+ &timeout,
+ sizeof(timeout));
+ if (!rv) {
+ PINGSENDER_LOG("ERROR: Could not set the connection timeout\n");
+ return false;
+ }
+
ScopedHInternet connection(InternetConnect(internet.get(),
host, components.nPort,
/* lpszUsername */ NULL,
/* lpszPassword */ NULL,
INTERNET_SERVICE_HTTP,
/* dwFlags */ 0,
/* dwContext */ NULL));
@@ -104,21 +114,21 @@ Post(const string& url, const string& pa
}
// Build a string containing all the headers.
std::string headers = GenerateDateHeader() + "\r\n";
headers += kCustomVersionHeader;
headers += "\r\n";
headers += kContentEncodingHeader;
- bool rv = HttpSendRequest(request.get(),
- headers.c_str(),
- -1L,
- (LPVOID)payload.c_str(),
- payload.size());
+ rv = HttpSendRequest(request.get(),
+ headers.c_str(),
+ -1L,
+ (LPVOID)payload.c_str(),
+ payload.size());
if (!rv) {
PINGSENDER_LOG("ERROR: Could not execute HTTP POST request\n");
return false;
}
// HttpSendRequest doesn't fail if we hit an HTTP error, so manually check
// for errors. Please note that this is not needed on the Linux/MacOS version
// of the pingsender, as libcurl already automatically fails on HTTP errors.