Skip to content

Commit 68ff507

Browse files
Use CURLOPT_TIMEOUT_MS for timeout (#1446)
Curl uses CURLOPT_SERVER_RESPONSE_TIMEOUT foк ping pong protocols (ftp, smtp, imap, pop3), while NetworkCurl uses http exclusively, hence timeout value specified in NetworkConfig was ignored. Relates-To: OLPSUP-23107 Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com>
1 parent 7098332 commit 68ff507

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

olp-cpp-sdk-core/src/http/curl/NetworkCurl.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,15 @@ std::string ConcatenateDnsAddresses(
270270

271271
return result;
272272
}
273+
274+
// Returns an integer value of the duration in specified representation.
275+
// Casts to long, as long is a type expected by `curl_easy_setopt`
276+
template <typename ToDuration, typename Duration>
277+
long CountIn(Duration&& duration) {
278+
const auto count = std::chrono::duration_cast<ToDuration>(duration).count();
279+
return static_cast<long>(count);
280+
}
281+
273282
} // anonymous namespace
274283

275284
NetworkCurl::NetworkCurl(NetworkInitializationSettings settings)
@@ -671,12 +680,19 @@ ErrorCode NetworkCurl::SendImplementation(
671680
#endif
672681

673682
curl_easy_setopt(handle->handle, CURLOPT_FOLLOWLOCATION, 1L);
683+
684+
// `::count` is defined on all duration types, to be on the safe side
685+
// regarding durations on NetworkConfig. Refactoring of NetworkConfig to
686+
// return different types will be handled gracefully here. Force specific type
687+
// of the representation & type expected by curl.
688+
const long connect_timeout_ms =
689+
CountIn<std::chrono::milliseconds>(config.GetConnectionTimeoutDuration());
690+
const long timeout_ms =
691+
CountIn<std::chrono::milliseconds>(config.GetTransferTimeoutDuration());
692+
674693
curl_easy_setopt(handle->handle, CURLOPT_CONNECTTIMEOUT_MS,
675-
config.GetConnectionTimeoutDuration().count());
676-
curl_easy_setopt(handle->handle, CURLOPT_SERVER_RESPONSE_TIMEOUT,
677-
std::chrono::duration_cast<std::chrono::seconds>(
678-
config.GetTransferTimeoutDuration())
679-
.count());
694+
connect_timeout_ms);
695+
curl_easy_setopt(handle->handle, CURLOPT_TIMEOUT_MS, timeout_ms);
680696
curl_easy_setopt(handle->handle, CURLOPT_WRITEFUNCTION,
681697
&NetworkCurl::RxFunction);
682698
curl_easy_setopt(handle->handle, CURLOPT_WRITEDATA, handle);

0 commit comments

Comments
 (0)