From c6a1c47d4021a6c9a0c8820b0052f148474d6bec Mon Sep 17 00:00:00 2001 From: Nestal Wan Date: Fri, 20 Jul 2012 00:50:40 +0800 Subject: [PATCH] added retry on HTTP 500 & 503 (#82) --- libgrive/src/http/CurlAgent.cc | 2 +- libgrive/src/protocol/AuthAgent.cc | 47 ++++++++++++++++++++++++++---- libgrive/src/protocol/AuthAgent.hh | 1 + libgrive/src/util/OS.cc | 13 +++++++++ libgrive/src/util/OS.hh | 2 ++ 5 files changed, 59 insertions(+), 6 deletions(-) diff --git a/libgrive/src/http/CurlAgent.cc b/libgrive/src/http/CurlAgent.cc index ef70d693..85dfe38e 100644 --- a/libgrive/src/http/CurlAgent.cc +++ b/libgrive/src/http/CurlAgent.cc @@ -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() diff --git a/libgrive/src/protocol/AuthAgent.cc b/libgrive/src/protocol/AuthAgent.cc index 06cacd21..b54f2324 100644 --- a/libgrive/src/protocol/AuthAgent.cc +++ b/libgrive/src/protocol/AuthAgent.cc @@ -18,7 +18,10 @@ */ #include "AuthAgent.hh" + #include "http/Header.hh" +#include "util/log/Log.hh" +#include "util/OS.hh" #include @@ -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( @@ -56,7 +63,11 @@ 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( @@ -64,7 +75,11 @@ long AuthAgent::Get( 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( @@ -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( @@ -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 @@ -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 diff --git a/libgrive/src/protocol/AuthAgent.hh b/libgrive/src/protocol/AuthAgent.hh index d3fb0534..3b198c60 100644 --- a/libgrive/src/protocol/AuthAgent.hh +++ b/libgrive/src/protocol/AuthAgent.hh @@ -72,6 +72,7 @@ public : private : http::Header AppendHeader( const http::Header& hdr ) const ; + bool CheckRetry( long response ) ; private : OAuth2 m_auth ; diff --git a/libgrive/src/util/OS.cc b/libgrive/src/util/OS.cc index 0d51136b..5bf2f663 100644 --- a/libgrive/src/util/OS.cc +++ b/libgrive/src/util/OS.cc @@ -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 diff --git a/libgrive/src/util/OS.hh b/libgrive/src/util/OS.hh index 4a0350dc..31ab51de 100644 --- a/libgrive/src/util/OS.hh +++ b/libgrive/src/util/OS.hh @@ -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