Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stability fixes #217

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 62 additions & 16 deletions libgrive/src/drive/Resource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "xml/Node.hh"
#include "xml/NodeSet.hh"
#include "xml/String.hh"
#include "xml/TreeBuilder.hh"

#include <boost/bind.hpp>
#include <boost/exception/all.hpp>
Expand Down Expand Up @@ -599,23 +600,68 @@ bool Resource::Upload(
% xml::Escape(m_name)
).str() ;

http::StringResponse str ;
if ( post )
http->Post( link, meta, &str, hdr ) ;
else
http->Put( link, meta, &str, hdr ) ;

http::Header uphdr ;
uphdr.Add( "Expect:" ) ;
uphdr.Add( "Accept:" ) ;
bool retrying=false;
while ( true ) {
if ( retrying ) {
file.Seek( 0, SEEK_SET );
os::Sleep( 5 );
}

// the content upload URL is in the "Location" HTTP header
std::string uplink = http->RedirLocation() ;
http::XmlResponse xml ;

http->Put( uplink, &file, &xml, uphdr ) ;
AssignIDs( Entry( xml.Response() ) ) ;
m_mtime = Entry(xml.Response()).MTime();
try {
http::StringResponse str ;
if ( post )
http->Post( link, meta, &str, hdr ) ;
else
http->Put( link, meta, &str, hdr ) ;
} catch ( Error &e ) {
std::string const *info = boost::get_error_info<xml::TreeBuilder::ExpatApiError>(e);
if ( info && (*info == "XML_Parse") ) {
Log( "Error parsing pre-upload response XML, retrying whole upload in 5s",
log::warning );
retrying = true;
continue;
} else {
throw e;
}
}

http::Header uphdr ;
uphdr.Add( "Expect:" ) ;
uphdr.Add( "Accept:" ) ;

// the content upload URL is in the "Location" HTTP header
std::string uplink = http->RedirLocation() ;
http::XmlResponse xml ;

long http_code = 0;
try {
http_code = http->Put( uplink, &file, &xml, uphdr ) ;
} catch ( Error &e ) {
std::string const *info = boost::get_error_info<xml::TreeBuilder::ExpatApiError>(e);
if ( info && (*info == "XML_Parse") ) {
Log( "Error parsing response XML, retrying whole upload in 5s",
log::warning );
retrying = true;
continue;
} else {
throw e;
}
}

if ( http_code == 410 || http_code == 412 ) {
Log( "request failed with %1%, retrying whole upload in 5s", http_code,
log::warning ) ;
retrying = true;
continue;
}

if ( retrying )
Log( "upload succeeded on retry", log::warning );
Entry responseEntry = Entry( xml.Response() );
AssignIDs( responseEntry ) ;
m_mtime = responseEntry.MTime();
break;
}

return true ;
}
Expand Down
5 changes: 5 additions & 0 deletions libgrive/src/http/XmlResponse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ XmlResponse::XmlResponse() : m_tb( new xml::TreeBuilder )
{
}

void XmlResponse::Clear()
{
m_tb.reset(new xml::TreeBuilder);
}

std::size_t XmlResponse::Write( const char *data, std::size_t count )
{
m_tb->ParseData( data, count ) ;
Expand Down
21 changes: 19 additions & 2 deletions libgrive/src/protocol/AuthAgent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

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

#include <cassert>

Expand Down Expand Up @@ -69,8 +71,22 @@ long AuthAgent::Put(
Header auth = AppendHeader(hdr) ;

long response ;
while ( CheckRetry(
response = m_agent->Put( url, file, dest, AppendHeader(hdr) ) ) ) ;
bool keepTrying = true;
while ( keepTrying ) {
response = m_agent->Put( url, file, dest, auth );
keepTrying = CheckRetry( response );
if ( keepTrying ) {
file->Seek( 0, SEEK_SET );
XmlResponse *xmlResponse = dynamic_cast<XmlResponse*>(dest);
if( xmlResponse )
xmlResponse->Clear();
}
}

// On 410 Gone or 412 Precondition failed, recovery may be possible so don't
// throw an exception
if ( response == 410 || response == 412 )
return response;

return CheckHttpResponse(response, url, auth) ;
}
Expand Down Expand Up @@ -152,6 +168,7 @@ bool AuthAgent::CheckRetry( long response )
Log( "resquest failed due to auth token expired: %1%. refreshing token",
response, log::warning ) ;

os::Sleep( 5 ) ;
m_auth.Refresh() ;
return true ;
}
Expand Down