Skip to content

Commit 1fe5dd0

Browse files
Add transfer rate limit support to NetworkCurl
This can be used for testing for poor network connectivity Introduce max_transfer_bytes_per_second field in NetworkInitializationSettings. When non-zero, this value is applied per connection via CURLOPT_MAX_RECV_SPEED_LARGE and CURLOPT_MAX_SEND_SPEED_LARGE (requires libcurl >= 7.15.5). Relates-To: HERESUP-62561 Signed-off-by: Ming Li <ming.5.li@here.com>
1 parent 589915a commit 1fe5dd0

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2023-2025 HERE Europe B.V.
2+
* Copyright (C) 2023-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919

2020
#pragma once
2121

22+
#include <string>
23+
2224
#include <olp/core/CoreApi.h>
2325
#include <olp/core/http/CertificateSettings.h>
2426
#include <olp/core/porting/optional.h>
@@ -48,6 +50,14 @@ struct CORE_API NetworkInitializationSettings {
4850
* setting.
4951
*/
5052
porting::optional<std::string> diagnostic_output_path = porting::none;
53+
54+
/**
55+
* @brief Global transfer limit in bytes per second. A value of 0 means no
56+
* limit. This value is applied per connection to cURL via
57+
* CURLOPT_MAX_RECV_SPEED_LARGE / CURLOPT_MAX_SEND_SPEED_LARGE where
58+
* supported.
59+
*/
60+
size_t max_transfer_bytes_per_second = 0u;
5161
};
5262

5363
} // namespace http

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2025 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -406,10 +406,16 @@ NetworkCurl::NetworkCurl(NetworkInitializationSettings settings)
406406
: handles_(settings.max_requests_count),
407407
static_handle_count_(
408408
std::max(static_cast<size_t>(1u), settings.max_requests_count / 4u)),
409-
certificate_settings_(std::move(settings.certificate_settings)) {
409+
certificate_settings_(std::move(settings.certificate_settings)),
410+
max_transfer_bytes_per_second_(settings.max_transfer_bytes_per_second) {
410411
OLP_SDK_LOG_TRACE(kLogTag, "Created NetworkCurl with address="
411412
<< this << ", handles_count="
412413
<< settings.max_requests_count);
414+
if (max_transfer_bytes_per_second_ > 0) {
415+
OLP_SDK_LOG_INFO_F(kLogTag, "max_transfer_bytes_per_second=%zu",
416+
max_transfer_bytes_per_second_);
417+
}
418+
413419
auto error = curl_global_init(CURL_GLOBAL_ALL);
414420
curl_initialized_ = (error == CURLE_OK);
415421
if (!curl_initialized_) {
@@ -858,6 +864,17 @@ ErrorCode NetworkCurl::SendImplementation(
858864
config.GetMaxConnectionLifetime().count() ? 1L : 0L);
859865
#endif
860866

867+
#if CURL_AT_LEAST_VERSION(7, 15, 5)
868+
// Refer to https://curl.se/libcurl/c/CURLOPT_MAX_RECV_SPEED_LARGE.html for
869+
// the minimum supported version.
870+
if (max_transfer_bytes_per_second_ > 0) {
871+
curl_off_t speed_limit =
872+
static_cast<curl_off_t>(max_transfer_bytes_per_second_);
873+
curl_easy_setopt(curl_handle, CURLOPT_MAX_RECV_SPEED_LARGE, speed_limit);
874+
curl_easy_setopt(curl_handle, CURLOPT_MAX_SEND_SPEED_LARGE, speed_limit);
875+
}
876+
#endif
877+
861878
{
862879
std::lock_guard<std::mutex> lock(event_mutex_);
863880
AddEvent(EventInfo::Type::SEND_EVENT, handle);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019-2025 HERE Europe B.V.
2+
* Copyright (C) 2019-2026 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -373,6 +373,10 @@ class NetworkCurl : public Network,
373373
/// blobs so cURL does not need to copy them.
374374
CertificateSettings certificate_settings_;
375375

376+
/// Maximum transfer rate in bytes per second applied per connection (0 =
377+
/// unlimited).
378+
size_t max_transfer_bytes_per_second_{0u};
379+
376380
#ifdef OLP_SDK_CURL_HAS_SUPPORT_SSL_BLOBS
377381
/// SSL certificate blobs.
378382
porting::optional<SslCertificateBlobs> ssl_certificates_blobs_;

0 commit comments

Comments
 (0)