This is an automated email from the ASF dual-hosted git repository.

chhsiao pushed a commit to branch 1.5.x
in repository https://gitbox.apache.org/repos/asf/mesos.git

commit f43d2cd58fb17cce26fdc050421d1d00b680c253
Author: Chun-Hung Hsiao <chhs...@apache.org>
AuthorDate: Wed Mar 28 22:47:46 2018 -0700

    Added the `stall_timeout` parameter to `net::download()`.
    
    When the `stall_timeout` is given, the download would be aborted if the
    download speed keeps below 1 byte per second during the timeout.
    
    Review: https://reviews.apache.org/r/65855/
---
 3rdparty/stout/include/stout/net.hpp | 19 +++++++++++++++++--
 1 file changed, 17 insertions(+), 2 deletions(-)

diff --git a/3rdparty/stout/include/stout/net.hpp 
b/3rdparty/stout/include/stout/net.hpp
index abb0144..d2992c0 100644
--- a/3rdparty/stout/include/stout/net.hpp
+++ b/3rdparty/stout/include/stout/net.hpp
@@ -48,6 +48,7 @@
 #include <string>
 
 #include <stout/bytes.hpp>
+#include <stout/duration.hpp>
 #include <stout/error.hpp>
 #include <stout/ip.hpp>
 #include <stout/option.hpp>
@@ -135,8 +136,12 @@ inline Try<Bytes> contentLength(const std::string& url)
 
 // Returns the HTTP response code resulting from attempting to
 // download the specified HTTP or FTP URL into a file at the specified
-// path.
-inline Try<int> download(const std::string& url, const std::string& path)
+// path. The `stall_timeout` parameter controls how long the download
+// waits before aborting when the download speed keeps below 1 byte/sec.
+inline Try<int> download(
+    const std::string& url,
+    const std::string& path,
+    const Option<Duration>& stall_timeout = None())
 {
   initialize();
 
@@ -176,6 +181,16 @@ inline Try<int> download(const std::string& url, const 
std::string& path)
   }
   curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
 
+  if (stall_timeout.isSome()) {
+    // Set the options to abort the download if the speed keeps below
+    // 1 byte/sec during the timeout. See:
+    // https://curl.haxx.se/libcurl/c/CURLOPT_LOW_SPEED_LIMIT.html
+    // https://curl.haxx.se/libcurl/c/CURLOPT_LOW_SPEED_TIME.html
+    curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
+    curl_easy_setopt(
+        curl, CURLOPT_LOW_SPEED_TIME, 
static_cast<long>(stall_timeout->secs()));
+  }
+
   CURLcode curlErrorCode = curl_easy_perform(curl);
   if (curlErrorCode != 0) {
     curl_easy_cleanup(curl);

Reply via email to