Skip to content

Commit

Permalink
Implement upload/download speed limit (fix Grive#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed May 15, 2016
1 parent 5327016 commit 44d3ddf
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Enjoy!
- no-remote-new and upload-only modes
- ignore regexp does not persist anymore (note that Grive will still track it to not
accidentally delete remote files when changing ignore regexp)
- added options to limit upload and download speed

### Grive2 v0.5

Expand Down
7 changes: 7 additions & 0 deletions grive/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ int Main( int argc, char **argv )
( "dry-run", "Only detect which files need to be uploaded/downloaded, "
"without actually performing them." )
( "ignore", po::value<std::string>(), "Perl RegExp to ignore files (matched against relative paths, remembered for next runs)." )
( "upload-speed,U", po::value<unsigned>(), "Limit upload speed in kbytes per second" )
( "download-speed,D", po::value<unsigned>(), "Limit download speed in kbytes per second" )
;

po::variables_map vm;
Expand Down Expand Up @@ -195,6 +197,11 @@ int Main( int argc, char **argv )
AuthAgent agent( token, http.get() ) ;
v2::Syncer2 syncer( &agent );

if ( vm.count( "upload-speed" ) > 0 )
agent.SetUploadSpeed( vm["upload-speed"].as<unsigned>() * 1000 );
if ( vm.count( "download-speed" ) > 0 )
agent.SetDownloadSpeed( vm["download-speed"].as<unsigned>() * 1000 );

Drive drive( &syncer, config.GetAll() ) ;
drive.DetectChanges() ;

Expand Down
10 changes: 10 additions & 0 deletions libgrive/src/http/Agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ long Agent::Post(
return Request( "POST", url, &s, dest, h );
}

void Agent::SetUploadSpeed( unsigned kbytes )
{
mMaxUpload = kbytes;
}

void Agent::SetDownloadSpeed( unsigned kbytes )
{
mMaxDownload = kbytes;
}

} } // end of namespace
6 changes: 6 additions & 0 deletions libgrive/src/http/Agent.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Header ;

class Agent
{
protected:
unsigned mMaxUpload, mMaxDownload ;

public :
virtual ~Agent() {}

Expand Down Expand Up @@ -70,6 +73,9 @@ public :
DataStream *dest,
const Header& hdr ) = 0 ;

virtual void SetUploadSpeed( unsigned kbytes ) ;
virtual void SetDownloadSpeed( unsigned kbytes ) ;

virtual std::string LastError() const = 0 ;
virtual std::string LastErrorHeaders() const = 0 ;

Expand Down
4 changes: 4 additions & 0 deletions libgrive/src/http/CurlAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ void CurlAgent::Init()
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERFUNCTION, &CurlAgent::HeaderCallback ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADERDATA, this ) ;
::curl_easy_setopt( m_pimpl->curl, CURLOPT_HEADER, 0L ) ;
if ( mMaxUpload > 0 )
::curl_easy_setopt( m_pimpl->curl, CURLOPT_MAX_SEND_SPEED_LARGE, mMaxUpload ) ;
if ( mMaxDownload > 0 )
::curl_easy_setopt( m_pimpl->curl, CURLOPT_MAX_RECV_SPEED_LARGE, mMaxDownload ) ;
m_pimpl->error = false;
m_pimpl->error_headers = "";
m_pimpl->error_data = "";
Expand Down
10 changes: 10 additions & 0 deletions libgrive/src/protocol/AuthAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ void AuthAgent::SetLog( http::ResponseLog *log )
return m_agent->SetLog( log );
}

void AuthAgent::SetUploadSpeed( unsigned kbytes )
{
m_agent->SetUploadSpeed( kbytes );
}

void AuthAgent::SetDownloadSpeed( unsigned kbytes )
{
m_agent->SetDownloadSpeed( kbytes );
}

http::Header AuthAgent::AppendHeader( const http::Header& hdr ) const
{
http::Header h(hdr) ;
Expand Down
3 changes: 3 additions & 0 deletions libgrive/src/protocol/AuthAgent.hh
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public :
std::string Escape( const std::string& str ) ;
std::string Unescape( const std::string& str ) ;

void SetUploadSpeed( unsigned kbytes ) ;
void SetDownloadSpeed( unsigned kbytes ) ;

private :
http::Header AppendHeader( const http::Header& hdr ) const ;
bool CheckRetry( long response ) ;
Expand Down

0 comments on commit 44d3ddf

Please sign in to comment.