diff options
author | Daniel Stenberg <daniel@haxx.se> | 2006-06-22 21:36:53 +0000 |
---|---|---|
committer | Daniel Stenberg <daniel@haxx.se> | 2006-06-22 21:36:53 +0000 |
commit | dfe1884c2529d728750d0824f73055627673cd72 (patch) | |
tree | 4f6d30648e54d10f10401408f0f819ff344ce318 /lib | |
parent | 3e5dcc8bcd4aaa7b6d7f8432327cbdd5487113e4 (diff) |
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.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/multi.c | 34 | ||||
-rw-r--r-- | lib/transfer.c | 33 | ||||
-rw-r--r-- | lib/url.c | 16 | ||||
-rw-r--r-- | lib/urldata.h | 2 |
4 files changed, 77 insertions, 8 deletions
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; } @@ -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 */ |