Ejemplo n.º 1
0
 internal RetryHttpClientInitializer(
     RetryOptions retryOptions, IClock clock = null, IWaiter waiter = null)
 {
     this.retryOptions   = retryOptions.ThrowIfNull(nameof(retryOptions));
     this.backOffHandler = new RetryAfterAwareBackOffHandler(
         this.retryOptions, clock, waiter);
 }
Ejemplo n.º 2
0
        public GoogleDriveService(IOptions <GoogleDriveOptions> config, ILoggerFactory loggerFactory)
        {
            _logger = loggerFactory.CreateLogger <GoogleDriveService>();
            _config = config?.Value ?? throw new ArgumentNullException(nameof(config));

            var httpInitializer = ValidateAndGetInitializer(_config);

            var svcInit = new BaseClientService.Initializer()
            {
                HttpClientInitializer           = httpInitializer(),
                ApplicationName                 = _config.ApplicationName,
                DefaultExponentialBackOffPolicy = ExponentialBackOffPolicy.None
            };

            _driveService = new DriveService(svcInit)
            {
                HttpClient =
                {
                    Timeout        = _config.TimeOut,
                    MessageHandler = { NumTries = 20 }
                }
            };

            var retryStatusCodes = new[] { HttpStatusCode.InternalServerError, HttpStatusCode.BadGateway, HttpStatusCode.ServiceUnavailable };

            var backoffHandler = new BackOffHandler(new BackOffHandler.Initializer(new ExponentialBackOff(TimeSpan.FromMilliseconds(500), 20))
            {
                MaxTimeSpan         = TimeSpan.FromSeconds(65),
                HandleExceptionFunc = exception =>
                {
                    var willDoBackoff = BackOffHandler.Initializer.DefaultHandleExceptionFunc(exception);
                    _logger.LogInformation($"'Exception' request: {exception.Message}. Try backoff: {willDoBackoff}");
                    return(willDoBackoff);
                },
                HandleUnsuccessfulResponseFunc = response =>
                {
                    var msg           = $"'Unsuccessful' request: {response.StatusCode}. ";
                    var willDoBackoff = retryStatusCodes.Contains(response.StatusCode);

                    if (!willDoBackoff)
                    {
                        try
                        {
                            var e = _driveService.DeserializeError(response).Result;
                            msg  += $"Reason: {(e.Errors?.Any() == true ? e.Errors[0].Reason : "unknown")}";

                            willDoBackoff = response.StatusCode == HttpStatusCode.Forbidden && (e.Errors[0].Reason == "rateLimitExceeded" || e.Errors[0].Reason == "userRateLimitExceeded");
                        }
                        catch
                        {
                        }
                    }
                    ;
                    _logger.LogInformation($"{msg} Backoff: {willDoBackoff}");
                    return(willDoBackoff);
                }
            });

            _driveService.HttpClient.MessageHandler.AddUnsuccessfulResponseHandler(backoffHandler);
            _driveService.HttpClient.MessageHandler.AddExceptionHandler(backoffHandler);
        }
Ejemplo n.º 3
0
 static ClientServiceRequest <T> AddBackOffHandler <T>(this ClientServiceRequest <T> request, BackOffHandler handler = null)
 {
     request.AddExceptionHandler(handler ?? new BackOffHandler(new ExponentialBackOff(TimeSpan.FromMilliseconds(250), 5)));
     return(request);
 }