Exemplo n.º 1
0
        public S3Client(S3Config config, HttpMessageHandler messageHandler)
        {
            ServiceCollection services = new ServiceCollection();

            services.AddSingleton(x => Options.Create(config));

            IS3ClientBuilder   builder     = services.AddSimpleS3Core();
            IHttpClientBuilder httpBuilder = builder.UseHttpClientFactory();

            if (messageHandler != null)
            {
                httpBuilder.ConfigurePrimaryHttpMessageHandler(x => messageHandler);
            }

            httpBuilder.SetHandlerLifetime(TimeSpan.FromMinutes(5));

            Random random = new Random();

            // Policy is:
            // Retries: 3
            // Timeout: 2^attempt seconds (2, 4, 8 seconds) + -100 to 100 ms jitter
            httpBuilder.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3,
                                                                             retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))
                                                                             + TimeSpan.FromMilliseconds(random.Next(-100, 100))));

            _provider      = services.BuildServiceProvider();
            _objectClient  = _provider.GetRequiredService <IS3ObjectClient>();
            _bucketClient  = _provider.GetRequiredService <IS3BucketClient>();
            _serviceClient = _provider.GetRequiredService <IS3ServiceClient>();
        }
Exemplo n.º 2
0
        private static IHttpClientBuilder SetupHandlersAndPolicies(this IHttpClientBuilder clientBuilder, PolicyConfiguration policyConfiguration)
        {
            clientBuilder
            //.ConfigurePrimaryHttpMessageHandler(x => new HttpClientHandler(x.GetRequiredService<ISOAPMessageLogger>()))
            .SetHandlerLifetime(policyConfiguration.HttpClientRenewalInterval)
            .AddPolicyHandler(
                RetryPolicyHandler.WaitAndRetry(policyConfiguration.HttpRetry)
                )
            .AddPolicyHandler(
                CircuitBreakerPolicyHandler.Break(policyConfiguration.HttpCircuitBreaker)
                );

            return(clientBuilder);
        }
Exemplo n.º 3
0
        public static S3Client Create()
        {
            //In this example we are using Dependency Injection (DI) using Microsoft's DI framework
            ServiceCollection services = new ServiceCollection();

            //We use Microsoft.Extensions.Configuration framework here to load our config file
            ConfigurationBuilder configBuilder = new ConfigurationBuilder();

            configBuilder.AddJsonFile("Config.json", false);
            IConfigurationRoot root = configBuilder.Build();

            //We use Microsoft.Extensions.Logging here to add support for logging
            services.AddLogging(x =>
            {
                x.AddConsole();
                x.AddConfiguration(root.GetSection("Logging"));
            });

            //Here we setup our S3Client
            IS3ClientBuilder clientBuilder = services.AddSimpleS3Core(s3Config =>
            {
                root.Bind(s3Config);

                s3Config.Credentials = new StringAccessKey("Q3AM3UQ867SPQQA43P2F", "zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG");
                s3Config.Region      = AwsRegion.USEast1;
                s3Config.Endpoint    = new Uri("https://play.min.io:9000/");
            });

            //We enable HTTP Factory support here.
            IHttpClientBuilder httpBuilder = clientBuilder.UseHttpClientFactory();

            //Every 5 minutes we create a new connection, thereby reacting to DNS changes
            httpBuilder.SetHandlerLifetime(TimeSpan.FromMinutes(5));

            //Uncomment this line if you want to use a proxy
            //httpBuilder.ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler { Proxy = new WebProxy("http://<YourProxy>:<ProxyPort>") });

            //Here we enable retrying. We retry 3 times with a delay of 600 ms between attempts. For more examples, see https://github.com/App-vNext/Polly
            httpBuilder.AddTransientHttpErrorPolicy(p => p.WaitAndRetryAsync(3, _ => TimeSpan.FromMilliseconds(600)));

            //Finally we build the service provider and return the S3Client
            IServiceProvider serviceProvider = services.BuildServiceProvider();

            return(serviceProvider.GetRequiredService <S3Client>());
        }
        public static IServiceCollection AddFundingDataServiceInterServiceClient(
            this IServiceCollection builder,
            IConfiguration config,
            TimeSpan[] retryTimeSpans = null,
            int numberOfExceptionsBeforeCircuitBreaker = 100,
            TimeSpan circuitBreakerFailurePeriod       = default,
            TimeSpan handlerLifetime = default)
        {
            retryTimeSpans ??= new[]
            {
                TimeSpan.FromMilliseconds(500), TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(5)
            };


            if (circuitBreakerFailurePeriod == default)
            {
                circuitBreakerFailurePeriod = TimeSpan.FromMinutes(1);
            }

            IHttpClientBuilder httpBuilder = builder.AddHttpClient(HttpClientKeys.FDZ,
                                                                   (httpClient) =>
            {
                ApiOptions apiOptions = new ApiOptions();

                config.Bind(ClientName, apiOptions);

                ApiClientConfigurationOptions.SetDefaultApiClientConfigurationOptions(httpClient, apiOptions);
            })
                                             .ConfigurePrimaryHttpMessageHandler(() => new ApiClientHandler())
                                             .AddTransientHttpErrorPolicy(c => c.WaitAndRetryAsync(retryTimeSpans))
                                             .AddTransientHttpErrorPolicy(c => c.CircuitBreakerAsync(numberOfExceptionsBeforeCircuitBreaker, circuitBreakerFailurePeriod))
                                             .AddUserProfilerHeaderPropagation();

            // if a life time for the handler has been set then set it on the client builder
            if (handlerLifetime != default)
            {
                httpBuilder.SetHandlerLifetime(Timeout.InfiniteTimeSpan);
            }

            builder.AddSingleton <IFundingDataZoneApiClient, FundingDataZoneApiClient>();

            return(builder);
        }
Exemplo n.º 5
0
        public static IHttpClientBuilder AddResilience(this IHttpClientBuilder clientBuilder, IPolicyConfig policyConfig)
        {
            if (policyConfig.HasResilicence)
            {
                clientBuilder.AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(
                                                              new[] {
                    TimeSpan.FromSeconds(1),
                    TimeSpan.FromSeconds(5),
                    TimeSpan.FromSeconds(10),
                }));

                clientBuilder.AddTransientHttpErrorPolicy(builder => builder.CircuitBreakerAsync(
                                                              handledEventsAllowedBeforeBreaking: 3,
                                                              durationOfBreak: TimeSpan.FromSeconds(30)));

                if (policyConfig.HandlerLifetime.Ticks > 0)
                {
                    clientBuilder.SetHandlerLifetime(policyConfig.HandlerLifetime);
                }
            }

            return(clientBuilder);
        }
Exemplo n.º 6
0
 public static IHttpClientBuilder ConfigureDefaultPolicyHandler(this IHttpClientBuilder clientBuilder) =>
 clientBuilder
 .SetHandlerLifetime(TimeSpan.FromMinutes(5))
 .AddPolicyHandler(GetRetryPolicy());
Exemplo n.º 7
0
        public IHttpServiceBuilder SetHandlerLifetime(TimeSpan handlerLifetime)
        {
            _httpClientBuilder.SetHandlerLifetime(handlerLifetime);

            return(this);
        }