Exemplo n.º 1
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);
        }