Skip to content

Commit

Permalink
Add low speed timeout by setting environment variables "TENSORSTORE_C…
Browse files Browse the repository at this point in the history
…URL_LOW_SPEED_TIME_SECONDS" and "TENSORSTORE_CURL_LOW_SPEED_LIMIT_BYTES"

By default, low speed is not enabled unless the corresponding environment variables are set.

PiperOrigin-RevId: 547270643
Change-Id: Ida7c0f3900331ceb00ae41e6ec1ab55fcfd1d775
  • Loading branch information
Tensorstore Team authored and copybara-github committed Jul 11, 2023
1 parent 1603aac commit 8e679be
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tensorstore/internal/http/curl_transport.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <stdlib.h>

#include <cstddef>
#include <cstdint>
#include <limits>
#include <memory>
#include <optional>
Expand Down Expand Up @@ -81,6 +82,12 @@ struct CurlConfig {
std::optional<std::string> ca_path = internal::GetEnv("TENSORSTORE_CA_PATH");
std::optional<std::string> ca_bundle =
internal::GetEnv("TENSORSTORE_CA_BUNDLE");
int64_t low_speed_time_seconds =
internal::GetEnvValue<int64_t>("TENSORSTORE_CURL_LOW_SPEED_TIME_SECONDS")
.value_or(0);
int64_t low_speed_limit_bytes =
internal::GetEnvValue<int64_t>("TENSORSTORE_CURL_LOW_SPEED_LIMIT_BYTES")
.value_or(0);
};

const CurlConfig& CurlEnvConfig() {
Expand Down Expand Up @@ -108,6 +115,21 @@ struct CurlRequestState {
TENSORSTORE_CHECK_OK(CurlEasySetopt(handle_.get(), CURLOPT_VERBOSE, 1L));
}

// Follow curl command manpage to set up default values for low speed
// timeout:
// https://curl.se/docs/manpage.html#-Y
if (config.low_speed_time_seconds > 0 || config.low_speed_limit_bytes > 0) {
auto seconds = config.low_speed_time_seconds > 0
? config.low_speed_time_seconds
: 30;
auto bytes =
config.low_speed_limit_bytes > 0 ? config.low_speed_limit_bytes : 1;
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_LOW_SPEED_TIME, seconds));
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_LOW_SPEED_LIMIT, bytes));
}

if (const auto& x = config.ca_path) {
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_CAPATH, x->c_str()));
Expand Down Expand Up @@ -161,6 +183,10 @@ struct CurlRequestState {
CurlEasySetopt(handle_.get(), CURLOPT_HEADERFUNCTION, nullptr));
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_ERRORBUFFER, nullptr));
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_LOW_SPEED_TIME, 0L));
TENSORSTORE_CHECK_OK(
CurlEasySetopt(handle_.get(), CURLOPT_LOW_SPEED_LIMIT, 0L));

factory_->CleanupHandle(std::move(handle_));
}
Expand Down

0 comments on commit 8e679be

Please sign in to comment.