From dfe1884c2529d728750d0824f73055627673cd72 Mon Sep 17 00:00:00 2001
From: Daniel Stenberg <daniel@haxx.se>
Date: Thu, 22 Jun 2006 21:36:53 +0000
Subject: Peter Silva introduced CURLOPT_MAX_SEND_SPEED_LARGE and
 CURLOPT_MAX_RECV_SPEED_LARGE that limit tha maximum rate libcurl is allowed
 to send or receive data. This kind of adds the the command line tool's option
 --limit-rate to the library.

The rate limiting logic in the curl app is now removed and is instead
provided by libcurl itself. Transfer rate limiting will now also work for -d
and -F, which it didn't before.
---
 lib/multi.c    | 34 ++++++++++++++++++++++++++++++++++
 lib/transfer.c | 33 +++++++++++++++++++++++++--------
 lib/url.c      | 16 ++++++++++++++++
 lib/urldata.h  |  2 ++
 4 files changed, 77 insertions(+), 8 deletions(-)

(limited to 'lib')

diff --git a/lib/multi.c b/lib/multi.c
index bafadd446..9201402a2 100644
--- a/lib/multi.c
+++ b/lib/multi.c
@@ -68,6 +68,7 @@ typedef enum {
   CURLM_STATE_DOING,       /* sending off the request (part 1) */
   CURLM_STATE_DO_MORE,     /* send off the request (part 2) */
   CURLM_STATE_PERFORM,     /* transfer data */
+  CURLM_STATE_TOOFAST,     /* wait because limit-rate exceeded */
   CURLM_STATE_DONE,        /* post data transfer operation */
   CURLM_STATE_COMPLETED,   /* operation complete */
 
@@ -156,6 +157,7 @@ static void multistate(struct Curl_one_easy *easy, CURLMstate state)
     "DOING",
     "DO_MORE",
     "PERFORM",
+    "TOOFAST",
     "DONE",
     "COMPLETED",
   };
@@ -440,6 +442,7 @@ static int multi_getsock(struct Curl_one_easy *easy,
                          int numsocks)
 {
   switch(easy->state) {
+  case CURLM_STATE_TOOFAST:  /* returns 0, so will not select. */
   default:
     return 0;
 
@@ -771,7 +774,37 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
       }
       break;
 
+    case CURLM_STATE_TOOFAST: /* limit-rate exceeded in either direction */
+      /* if both rates are within spec, resume transfer */
+      Curl_pgrsUpdate(easy->easy_conn);
+      if ( ( ( easy->easy_handle->set.max_send_speed == 0 ) ||
+             ( easy->easy_handle->progress.ulspeed <
+               easy->easy_handle->set.max_send_speed ) )  &&
+           ( ( easy->easy_handle->set.max_recv_speed == 0 ) ||
+             ( easy->easy_handle->progress.dlspeed <
+               easy->easy_handle->set.max_recv_speed ) )
+        )
+        multistate(easy, CURLM_STATE_PERFORM);
+
+      break;
+
     case CURLM_STATE_PERFORM:
+
+      /* check if over speed */
+      if ( (  ( easy->easy_handle->set.max_send_speed > 0 ) &&
+              ( easy->easy_handle->progress.ulspeed >
+                easy->easy_handle->set.max_send_speed ) )  ||
+           (  ( easy->easy_handle->set.max_recv_speed > 0 ) &&
+              ( easy->easy_handle->progress.dlspeed >
+                easy->easy_handle->set.max_recv_speed ) )
+        ) {
+        /* Transfer is over the speed limit. Change state.  TODO: Call
+         * Curl_expire() with the time left until we're targeted to be below
+         * the speed limit again. */
+        multistate(easy, CURLM_STATE_TOOFAST );
+        break;
+      }
+
       /* read/write data if it is ready to do so */
       easy->result = Curl_readwrite(easy->easy_conn, &done);
 
@@ -825,6 +858,7 @@ static CURLMcode multi_runsingle(struct Curl_multi *multi,
         }
       }
       break;
+
     case CURLM_STATE_DONE:
       /* post-transfer command */
       easy->result = Curl_done(&easy->easy_conn, CURLE_OK);
diff --git a/lib/transfer.c b/lib/transfer.c
index 370734a13..0af351042 100644
--- a/lib/transfer.c
+++ b/lib/transfer.c
@@ -1628,15 +1628,31 @@ Transfer(struct connectdata *conn)
 
     interval_ms = 1 * 1000;
 
-    if(k->keepon & KEEP_READ)
-      fd_read = conn->sockfd;
-    else
-      fd_read = CURL_SOCKET_BAD;
-
-    if(k->keepon & KEEP_WRITE)
-      fd_write = conn->writesockfd;
-    else
+    /* limit-rate logic: if speed exceeds threshold, then do not include fd in
+       select set */
+    if ( (conn->data->set.max_send_speed > 0) &&
+         (conn->data->progress.ulspeed > conn->data->set.max_send_speed) )  {
       fd_write = CURL_SOCKET_BAD;
+      Curl_pgrsUpdate(conn);
+    }
+    else {
+      if(k->keepon & KEEP_WRITE)
+        fd_write = conn->writesockfd;
+      else
+        fd_write = CURL_SOCKET_BAD;
+    }
+
+    if ( (conn->data->set.max_recv_speed > 0) &&
+         (conn->data->progress.dlspeed > conn->data->set.max_recv_speed) ) {
+      fd_read = CURL_SOCKET_BAD;
+      Curl_pgrsUpdate(conn);
+    }
+    else {
+      if(k->keepon & KEEP_READ)
+        fd_read = conn->sockfd;
+      else
+        fd_read = CURL_SOCKET_BAD;
+    }
 
     switch (Curl_select(fd_read, fd_write, interval_ms)) {
     case -1: /* select() error, stop reading */
@@ -1651,6 +1667,7 @@ Transfer(struct connectdata *conn)
       continue;
     case 0:  /* timeout */
     default: /* readable descriptors */
+
       result = Curl_readwrite(conn, &done);
       break;
     }
diff --git a/lib/url.c b/lib/url.c
index 985ade1c5..85537b2ce 100644
--- a/lib/url.c
+++ b/lib/url.c
@@ -1039,6 +1039,22 @@ CURLcode Curl_setopt(struct SessionHandle *data, CURLoption option,
      */
     data->set.low_speed_limit=va_arg(param, long);
     break;
+  case CURLOPT_MAX_SEND_SPEED_LARGE:
+    /*
+     * The max speed limit that sends transfer more than
+     * CURLOPT_MAX_SEND_PER_SECOND bytes per second the transfer is
+     * throttled..
+     */
+    data->set.max_send_speed=va_arg(param, curl_off_t);
+    break;
+  case CURLOPT_MAX_RECV_SPEED_LARGE:
+    /*
+     * The max speed limit that sends transfer more than
+     * CURLOPT_MAX_RECV_PER_SECOND bytes per second the transfer is
+     * throttled..
+     */
+    data->set.max_recv_speed=va_arg(param, curl_off_t);
+    break;
   case CURLOPT_LOW_SPEED_TIME:
     /*
      * The low speed time that if transfers are below the set
diff --git a/lib/urldata.h b/lib/urldata.h
index b2cdd8dcb..834741fcc 100644
--- a/lib/urldata.h
+++ b/lib/urldata.h
@@ -1039,6 +1039,8 @@ struct UserDefined {
   curl_off_t infilesize;      /* size of file to upload, -1 means unknown */
   long low_speed_limit; /* bytes/second */
   long low_speed_time;  /* number of seconds */
+  curl_off_t max_send_speed; /* high speed limit in bytes/second for upload */
+  curl_off_t max_recv_speed; /* high speed limit in bytes/second for download */
   curl_off_t set_resume_from;  /* continue [ftp] transfer from here */
   char *cookie;         /* HTTP cookie string to send */
   struct curl_slist *headers; /* linked list of extra headers */
-- 
cgit v1.2.3