Index: Src/GoogleApis/Apis/Http/BackOffHandler.cs =================================================================== --- a/Src/GoogleApis/Apis/Http/BackOffHandler.cs +++ b/Src/GoogleApis/Apis/Http/BackOffHandler.cs @@ -18,49 +18,50 @@ using System.Net.Http; using System.Threading; using System.Threading.Tasks; + using Google.Apis.Logging; using Google.Apis.Util; namespace Google.Apis.Http { /// - /// A thread-safe back-off handler which handles an abnormal Http response or an exception with + /// A thread-safe back-off handler which handles an abnormal HTTP response or an exception with /// . /// public class BackOffHandler : IHttpUnsuccessfulResponseHandler, IHttpExceptionHandler { private static readonly ILogger Logger = ApplicationContext.Logger.ForType(); - /// An initializer class to initialize a back-off handler. + /// An initializer class to initialize a back-off handler. public class Initializer { - /// Gets the back-off policy used by this back-off handler. + /// Gets the back-off policy used by this back-off handler. public IBackOff BackOff { get; private set; } - /// + /// /// Gets or sets the maximum time span to wait. If the back-off instance returns a greater time span then - /// this value, this handler returns false to both and - /// . Default value is 5 seconds per a retry request. + /// this value, this handler returns false to both and + /// . Default value is 5 seconds per a retry request. /// public TimeSpan MaxTimeSpan { get; set; } - /// + /// /// Gets or sets a delegate function which indicates if this back-off handler should handle an abnormal - /// Http response. The default is . + /// HTTP response. The default is . /// public Func HandleUnsuccessfulResponseFunc { get; set; } - /// + /// /// Gets or sets a delegate function which indicates if this back-off handler should handle an exception. - /// The default is . + /// The default is . /// public Func HandleExceptionFunc { get; set; } - /// Default function which handles server errors (503). + /// Default function which handles server errors (503). public static readonly Func DefaultHandleUnsuccessfulResponseFunc = (r) => (int)r.StatusCode == 503; - /// + /// /// Default function which handles exception which aren't /// or /// . Those exceptions represent a task or an operation @@ -69,7 +70,7 @@ public static readonly Func DefaultHandleExceptionFunc = (ex) => !(ex is TaskCanceledException || ex is OperationCanceledException); - /// Constructs a new initializer by the given back-off. + /// Constructs a new initializer by the given back-off. public Initializer(IBackOff backOff) { BackOff = backOff; @@ -79,34 +80,35 @@ } } - /// Gets the back-off policy used by this back-off handler. + /// Gets the back-off policy used by this back-off handler. public IBackOff BackOff { get; private set; } - /// + /// /// Gets the maximum time span to wait. If the back-off instance returns a greater time span, the handle method /// returns false. Default value is 5 seconds per a retry request. /// public TimeSpan MaxTimeSpan { get; private set; } - /// - /// Gets a delegate function which indicates if this back-off handler should handle an abnormal Http response. - /// The default is . + /// + /// Gets a delegate function which indicates if this back-off handler should handle an abnormal HTTP response. + /// The default is . /// public Func HandleUnsuccessfulResponseFunc { get; private set; } - /// + /// /// Gets a delegate function which indicates if this back-off handler should handle an exception. The - /// default is . + /// default is . /// public Func HandleExceptionFunc { get; private set; } - /// Constructs a new back-off handler with the given back-off. + /// Constructs a new back-off handler with the given back-off. + /// The back-off policy public BackOffHandler(IBackOff backOff) : this(new Initializer(backOff)) { } - /// Constructs a new back-off handler with the given initializer. + /// Constructs a new back-off handler with the given initializer. public BackOffHandler(Initializer initializer) { BackOff = initializer.BackOff; @@ -117,22 +119,22 @@ #region IHttpUnsuccessfulResponseHandler - public virtual bool HandleResponse(HandleUnsuccessfulResponseArgs args) + public virtual async Task HandleResponseAsync(HandleUnsuccessfulResponseArgs args) { // if the func returns true try to handle this current failed try return HandleUnsuccessfulResponseFunc != null && HandleUnsuccessfulResponseFunc(args.Response) && - Handle(args.SupportsRetry, args.CurrentFailedTry, args.CancellationToken); + await HandleAsync(args.SupportsRetry, args.CurrentFailedTry, args.CancellationToken); } #endregion #region IHttpExceptionHandler - public virtual bool HandleException(HandleExceptionArgs args) + public virtual async Task HandleExceptionAsync(HandleExceptionArgs args) { // if the func returns true try to handle this current failed try return HandleExceptionFunc != null && HandleExceptionFunc(args.Exception) && - Handle(args.SupportsRetry, args.CurrentFailedTry, args.CancellationToken); + await HandleAsync(args.SupportsRetry, args.CurrentFailedTry, args.CancellationToken); } #endregion @@ -143,7 +145,8 @@ /// block for x milliseconds (x is defined by the instance), and this handler returns /// true. /// - private bool Handle(bool supportsRetry, int currentFailedTry, CancellationToken cancellationToken) + private async Task HandleAsync(bool supportsRetry, int currentFailedTry, + CancellationToken cancellationToken) { if (!supportsRetry || BackOff.MaxNumOfRetries < currentFailedTry) { @@ -156,22 +159,18 @@ return false; } - Wait(ts, cancellationToken); + await Wait(ts, cancellationToken); Logger.Debug("Back-Off handled the error. Waited {0}ms before next retry...", ts.TotalMilliseconds); return true; } - /// Waits the given time span. Override this method is recommended for mocking purposes. + /// Waits the given time span. Override this method is recommended for mocking purposes. /// TimeSpan to wait (and block the current thread) /// The cancellation token in case the user wants to cancel the operation in /// the middle - protected virtual void Wait(TimeSpan ts, CancellationToken cancellationToken) + protected virtual async Task Wait(TimeSpan ts, CancellationToken cancellationToken) { - try - { - TaskEx.Delay(ts, cancellationToken).Wait(); - } - catch (Exception) { } + await TaskEx.Delay(ts, cancellationToken); } } }