private TResponse DoActionWithExponentialBackoff <TResponse>(CalendarBaseServiceRequest <TResponse> request, HttpStatusCode[] otherAllowedCodes, HttpStatusCode[] otherBackoffCodes) { int delay = 100; while (delay < 1000) //If the delay gets above 1 second, give up { try { return(request.Execute()); } catch (GoogleApiException ex) { if (ex.HttpStatusCode == HttpStatusCode.Forbidden || //Rate limit exceeded ex.HttpStatusCode == HttpStatusCode.ServiceUnavailable || //Backend error ex.HttpStatusCode == HttpStatusCode.NotFound || ex.Message.Contains("That’s an error") || //Handles the Google error pages like https://i.imgur.com/lFDKFro.png otherAllowedCodes.Contains(ex.HttpStatusCode)) { Common.Log($"Request failed. Waiting {delay} ms before trying again"); Thread.Sleep(delay); delay += 100; } else { throw; } } } throw new Exception("Retry attempts failed"); }
/// <summary>Execute asyncronius request.</summary> /// <typeparam name="T">The type of the request result.</typeparam> protected virtual Task <T> ExecuteAsync <T>(CalendarBaseServiceRequest <T> request) where T : class { return(request.ExecuteAsync()); }
private TResponse DoActionWithExponentialBackoff <TResponse>(CalendarBaseServiceRequest <TResponse> request) { return(DoActionWithExponentialBackoff(request, new HttpStatusCode[0], new HttpStatusCode[0])); }
protected override Task <T> ExecuteAsync <T>(CalendarBaseServiceRequest <T> request) => Task.FromResult((T)_action());