问题描述:

I have a custom ExponentialBackOff policy taken from Eric Koleda's post here: https://github.com/google/google-api-dotnet-client/issues/608

var exponentialBackoff = new ExponentialBackOff(TimeSpan.FromMilliseconds(1000), 5);

return new BackOffHandler(new BackOffHandler.Initializer(exponentialBackoff)

{

HandleUnsuccessfulResponseFunc = delegate (HttpResponseMessage response)

{

RequestError e = service.DeserializeError(response).Result;

return response.StatusCode == HttpStatusCode.Forbidden

&& (e.Errors[0].Reason == "rateLimitExceeded"

|| e.Errors[0].Reason == "userRateLimitExceeded");

}

});

This works fine in general for Gmail, Directory and Drive (all v1.9.2), except for when uploading files above 10mb. This is due to the way the upload is chunked into 10mb pieces and sent with a ResumableUpload. If I remove the custom policy then I have no issues. I tried to add in extra code to the delegate to handle a 308 status code which is returned by the ResumableUpload functionality like so:

HandleUnsuccessfulResponseFunc = delegate (HttpResponseMessage response)

{

if (response.StatusCode == (HttpStatusCode)308)

{

return true;

}

RequestError e = service.DeserializeError(response).Result;

return response.StatusCode == HttpStatusCode.Forbidden

&& (e.Errors[0].Reason == "rateLimitExceeded"

|| e.Errors[0].Reason == "userRateLimitExceeded");

}

However, when I do this I receive the following error:

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.

Parameter name: currentRetry

at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)

at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)

at Microsoft.Runtime.CompilerServices.ConfiguredTaskAwaitable`1.ConfiguredTaskAwaiter.GetResult()

at Google.Apis.Upload.ResumableUpload`1.<UploadCoreAsync>d__e.MoveNext() in c:\code\github\google-api-dotnet-client\Tools\Google.Apis.Release\bin\Release\1.9.2\default\Src\GoogleApis\Apis\[Media]\Upload\ResumableUpload.cs:line 460

How can I get the custom ExponentialBackOff policy and the ResumableUpload to work together?


For completeness, I'll add in the upload code (I've used two, neither of which work):

Upload sample 1:

FilesResource.InsertMediaUpload request = service.Files.Insert(driveAttachment.Body, driveAttachment.Stream, attachment.MimeType);

request.Upload();

Upload sample 2:

FilesResource.InsertMediaUpload insertRequest = service.Files.Insert(driveAttachment.Body, uploadStream, gmailAttachment.MimeType);

insertRequest.ProgressChanged += Upload_ProgressChanged;

insertRequest.ResponseReceived += Upload_ResponseReceived;

var task = insertRequest.UploadAsync();

task.ContinueWith(t =>

{

// Remeber to clean the stream.

driveAttachment.Stream.Dispose();

});

private static void Upload_ProgressChanged(IUploadProgress progress)

{

Debug.WriteLine(string.Format("Status: {0} Bytes: {1} Exception: {2}", progress.Status, progress.BytesSent, progress.Exception));

}

private static void Upload_ResponseReceived(Google.Apis.Drive.v2.Data.File file)

{

Debug.WriteLine(file.Title + " was uploaded successfully");

}

相关阅读:
Top