Skip to content

Commit 5e95535

Browse files
authored
Supports option max connection lifetime (#1398)
This option configures maximum lifetime for connection in CURL based implementation. It's important for server based code which uptime is long to recreate connections more often to deal with load ballancer. Relates-To: OLPSUP-23436 Signed-off-by: Yauheni Khnykin <yauheni.khnykin@here.com>
1 parent c2e9f7c commit 5e95535

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

olp-cpp-sdk-core/include/olp/core/http/NetworkSettings.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,22 @@ class CORE_API NetworkSettings final {
119119
"WithTransferTimeout(std::chrono::milliseconds) instead")
120120
NetworkSettings& WithTransferTimeout(int timeout);
121121

122+
/**
123+
* @brief Gets max lifetime (since creation) allowed for reusing a connection.
124+
*
125+
* @return The lifetime.
126+
*/
127+
std::chrono::seconds GetMaxConnectionLifetime() const;
128+
129+
/**
130+
* @brief Sets max lifetime (since creation) allowed for reusing a connection.
131+
* Supported only for CURL implementation. If set to 0, this behavior is
132+
* disabled: all connections are eligible for reuse.
133+
*
134+
* @return A reference to *this.
135+
*/
136+
NetworkSettings& WithMaxConnectionLifetime(std::chrono::seconds lifetime);
137+
122138
/**
123139
* @brief Sets the transfer timeout.
124140
*
@@ -151,6 +167,8 @@ class CORE_API NetworkSettings final {
151167
std::chrono::milliseconds connection_timeout_ = std::chrono::seconds(60);
152168
/// The transfer timeout.
153169
std::chrono::milliseconds transfer_timeout_ = std::chrono::seconds(30);
170+
/// The max lifetime since creation allowed for reusing a connection.
171+
std::chrono::seconds connection_lifetime_{0};
154172
/// The network proxy settings.
155173
NetworkProxySettings proxy_settings_;
156174
};

olp-cpp-sdk-core/src/http/NetworkSettings.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ std::chrono::milliseconds NetworkSettings::GetTransferTimeoutDuration() const {
4545
return transfer_timeout_;
4646
}
4747

48+
std::chrono::seconds NetworkSettings::GetMaxConnectionLifetime() const {
49+
return connection_lifetime_;
50+
}
51+
4852
const NetworkProxySettings& NetworkSettings::GetProxySettings() const {
4953
return proxy_settings_;
5054
}
@@ -74,6 +78,12 @@ NetworkSettings& NetworkSettings::WithTransferTimeout(
7478
return *this;
7579
}
7680

81+
NetworkSettings& NetworkSettings::WithMaxConnectionLifetime(
82+
std::chrono::seconds lifetime) {
83+
connection_lifetime_ = lifetime;
84+
return *this;
85+
}
86+
7787
NetworkSettings& NetworkSettings::WithProxySettings(
7888
NetworkProxySettings settings) {
7989
proxy_settings_ = std::move(settings);

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int ConvertErrorCode(CURLcode curl_code) {
184184
} else if ((curl_code == CURLE_UNSUPPORTED_PROTOCOL) ||
185185
(curl_code == CURLE_URL_MALFORMAT)) {
186186
return static_cast<int>(ErrorCode::INVALID_URL_ERROR);
187-
#if (LIBCURL_VERSION_MAJOR >= 7) && (LIBCURL_VERSION_MINOR >= 24)
187+
#if CURL_AT_LEAST_VERSION(7, 24, 0)
188188
} else if (curl_code == CURLE_FTP_ACCEPT_FAILED) {
189189
return static_cast<int>(ErrorCode::AUTHORIZATION_ERROR);
190190
#endif
@@ -624,18 +624,26 @@ ErrorCode NetworkCurl::SendImplementation(
624624
}
625625
curl_easy_setopt(handle->handle, CURLOPT_ERRORBUFFER, handle->error_text);
626626

627-
#if (LIBCURL_VERSION_MAJOR >= 7) && (LIBCURL_VERSION_MINOR >= 21)
627+
#if CURL_AT_LEAST_VERSION(7, 21, 0)
628628
curl_easy_setopt(handle->handle, CURLOPT_ACCEPT_ENCODING, "");
629629
curl_easy_setopt(handle->handle, CURLOPT_TRANSFER_ENCODING, 1L);
630630
#endif
631631

632-
#if (LIBCURL_VERSION_MAJOR >= 7) && (LIBCURL_VERSION_MINOR >= 25)
632+
#if CURL_AT_LEAST_VERSION(7, 25, 0)
633633
// Enable keep-alive (since Curl 7.25.0)
634634
curl_easy_setopt(handle->handle, CURLOPT_TCP_KEEPALIVE, 1L);
635635
curl_easy_setopt(handle->handle, CURLOPT_TCP_KEEPIDLE, 120L);
636636
curl_easy_setopt(handle->handle, CURLOPT_TCP_KEEPINTVL, 60L);
637637
#endif
638638

639+
#if CURL_AT_LEAST_VERSION(7, 80, 0)
640+
curl_easy_setopt(handle->handle, CURLOPT_MAXLIFETIME_CONN,
641+
config.GetMaxConnectionLifetime().count());
642+
#else
643+
curl_easy_setopt(handle->handle, CURLOPT_FORBID_REUSE,
644+
config.GetMaxConnectionLifetime().count() ? 1L : 0L);
645+
#endif
646+
639647
{
640648
std::lock_guard<std::mutex> lock(event_mutex_);
641649
AddEvent(EventInfo::Type::SEND_EVENT, handle);

olp-cpp-sdk-core/tests/http/NetworkSettingsTest.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ TEST(NetworkSettingsTest, WithRetriesDeprecated) {
6666
EXPECT_EQ(settings.GetRetries(), 5);
6767
}
6868

69+
TEST(NetworkSettingsTest, WithMaxConnectionLifetimeDefault) {
70+
const auto settings = olp::http::NetworkSettings();
71+
EXPECT_EQ(settings.GetMaxConnectionLifetime(), std::chrono::seconds(0));
72+
}
73+
74+
TEST(NetworkSettingsTest, WithMaxConnectionLifetime) {
75+
const auto settings = olp::http::NetworkSettings().WithMaxConnectionLifetime(
76+
std::chrono::seconds(15));
77+
EXPECT_EQ(settings.GetMaxConnectionLifetime(), std::chrono::seconds(15));
78+
}
79+
6980
} // namespace
7081

7182
PORTING_POP_WARNINGS()

0 commit comments

Comments
 (0)