Skip to content

Commit

Permalink
added retry on HTTP 500 & 503 (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
nestal committed Jul 19, 2012
1 parent 28e8012 commit c6a1c47
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 6 deletions.
2 changes: 1 addition & 1 deletion libgrive/src/http/CurlAgent.cc
Expand Up @@ -156,7 +156,7 @@ long CurlAgent::ExecCurl(
Trace( "HTTP response %1%", http_code ) ;
::curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, 0 ) ;

if ( curl_code != CURLE_OK || http_code >= 400 )
if ( curl_code != CURLE_OK || (http_code >= 400 && http_code < 500) )
{
BOOST_THROW_EXCEPTION(
Error()
Expand Down
47 changes: 42 additions & 5 deletions libgrive/src/protocol/AuthAgent.cc
Expand Up @@ -18,7 +18,10 @@
*/

#include "AuthAgent.hh"

#include "http/Header.hh"
#include "util/log/Log.hh"
#include "util/OS.hh"

#include <cassert>

Expand Down Expand Up @@ -47,7 +50,11 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
return m_agent->Put( url, data, dest, AppendHeader(hdr) ) ;
long response ;
while ( CheckRetry(
response = m_agent->Put(url, data, dest, AppendHeader(hdr)) ) ) ;

return response ;
}

long AuthAgent::Put(
Expand All @@ -56,15 +63,23 @@ long AuthAgent::Put(
Receivable *dest,
const Header& hdr )
{
return m_agent->Put( url, file, dest, AppendHeader(hdr) ) ;
long response ;
while ( CheckRetry(
response = m_agent->Put( url, file, dest, AppendHeader(hdr) ) ) ) ;

return response ;
}

long AuthAgent::Get(
const std::string& url,
Receivable *dest,
const Header& hdr )
{
return m_agent->Get( url, dest, AppendHeader(hdr) ) ;
long response ;
while ( CheckRetry(
response = m_agent->Get( url, dest, AppendHeader(hdr) ) ) ) ;

return response ;
}

long AuthAgent::Post(
Expand All @@ -73,7 +88,11 @@ long AuthAgent::Post(
Receivable *dest,
const Header& hdr )
{
return m_agent->Post( url, data, dest, AppendHeader(hdr) ) ;
long response ;
while ( CheckRetry(
response = m_agent->Post( url, data, dest, AppendHeader(hdr) ) ) ) ;

return response ;
}

long AuthAgent::Custom(
Expand All @@ -82,7 +101,11 @@ long AuthAgent::Custom(
Receivable *dest,
const Header& hdr )
{
return m_agent->Custom( method, url, dest, AppendHeader(hdr) ) ;
long response ;
while ( CheckRetry(
response = m_agent->Custom( method, url, dest, AppendHeader(hdr) ) ) ) ;

return response ;
}

std::string AuthAgent::RedirLocation() const
Expand All @@ -100,4 +123,18 @@ std::string AuthAgent::Unescape( const std::string& str )
return m_agent->Unescape( str ) ;
}

bool AuthAgent::CheckRetry( long response )
{
if ( response == 500 || response == 503 )
{
Log( "resquest failed due to temperory error: %1%. retrying in 5 seconds",
response, log::warning ) ;

os::Sleep( 5 ) ;
return true ;
}
else
return false ;
}

} // end of namespace
1 change: 1 addition & 0 deletions libgrive/src/protocol/AuthAgent.hh
Expand Up @@ -72,6 +72,7 @@ public :

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

private :
OAuth2 m_auth ;
Expand Down
13 changes: 13 additions & 0 deletions libgrive/src/util/OS.cc
Expand Up @@ -81,4 +81,17 @@ void SetFileTime( const std::string& filename, const DateTime& t )
) ;
}

void Sleep( unsigned int sec )
{
struct timespec ts = { sec, 0 } ;

int result = 0 ;
do
{
struct timespec rem ;
nanosleep( &ts, &rem ) ;
ts = rem ;
} while ( result == -1 && errno == EINTR ) ;
}

} } // end of namespaces
2 changes: 2 additions & 0 deletions libgrive/src/util/OS.hh
Expand Up @@ -38,6 +38,8 @@ namespace os

void SetFileTime( const std::string& filename, const DateTime& t ) ;
void SetFileTime( const fs::path& filename, const DateTime& t ) ;

void Sleep( unsigned int sec ) ;
}

} // end of namespaces

0 comments on commit c6a1c47

Please sign in to comment.