public static IHttpClientBuilder AddSearchPolicyHandlers(
     this IHttpClientBuilder builder,
     ILogger logger,
     string searchName,
     ITelemetryService telemetryService,
     IAppConfiguration config)
 {
     return(builder
            .AddPolicyHandler(SearchClientPolicies.SearchClientFallBackCircuitBreakerPolicy(
                                  logger,
                                  searchName,
                                  telemetryService))
            .AddPolicyHandler(SearchClientPolicies.SearchClientWaitAndRetryPolicy(
                                  config.SearchCircuitBreakerWaitAndRetryCount,
                                  config.SearchCircuitBreakerWaitAndRetryIntervalInMilliseconds,
                                  logger,
                                  searchName,
                                  telemetryService))
            .AddPolicyHandler(SearchClientPolicies.SearchClientCircuitBreakerPolicy(
                                  config.SearchCircuitBreakerBreakAfterCount,
                                  TimeSpan.FromSeconds(config.SearchCircuitBreakerDelayInSeconds),
                                  logger,
                                  searchName,
                                  telemetryService))
            .AddPolicyHandler(SearchClientPolicies.SearchClientTimeoutPolicy(
                                  TimeSpan.FromMilliseconds(config.SearchHttpRequestTimeoutInMilliseconds),
                                  logger,
                                  searchName,
                                  telemetryService)));
 }
Beispiel #2
0
        private static ServiceCollection ConfigureServices()
        {
            Mock <ITelemetryService> mockITelemetryService    = new Mock <ITelemetryService>();
            ITelemetryService        telemetryServiceResolver = mockITelemetryService.Object;

            ServiceCollection services = new ServiceCollection();

            _loggerFor_InvalidTestSearchHttpClientWithLongCircuitBreakDelay  = new LoggerFor_TestSearchHttpClient();
            _loggerFor_InvalidTestSearchHttpClientWithShortCircuitBreakDelay = new LoggerFor_TestSearchHttpClient();
            _loggerFor_ValidTestSearchHttpClient = new LoggerFor_TestSearchHttpClient();
            _loggerFor_InvalidTestSearchHttpClientRetryCountExpires = new LoggerFor_TestSearchHttpClient();

            services.AddHttpClient <TestSearchHttpClient>(_nameFor_InvalidTestSearchHttpClientWithLongCircuitBreakDelay, c => c.BaseAddress = new Uri(_longInvalidAddress))
            .AddPolicyHandler(SearchClientPolicies.SearchClientFallBackCircuitBreakerPolicy(_loggerFor_InvalidTestSearchHttpClientWithLongCircuitBreakDelay, "InvalidTestSearchHttpClientWithLongCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientWaitAndRetryPolicy(_retryCount, _waitBetweenRetriesInMilliseconds, _loggerFor_InvalidTestSearchHttpClientWithLongCircuitBreakDelay, "InvalidTestSearchHttpClientWithLongCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientCircuitBreakerPolicy(
                                  _circuitBreakerFailAfter,
                                  TimeSpan.FromSeconds(_circuitBreakerLongDelaySeconds),
                                  _loggerFor_InvalidTestSearchHttpClientWithLongCircuitBreakDelay,
                                  "InvalidTestSearchHttpClientWithLongCircuitBreakDelay", telemetryServiceResolver));

            services.AddHttpClient <TestSearchHttpClient>(_nameFor_InvalidTestSearchHttpClientWithShortCircuitBreakDelay, c => c.BaseAddress = new Uri(_shortInvalidAddress))
            .AddPolicyHandler(SearchClientPolicies.SearchClientFallBackCircuitBreakerPolicy(_loggerFor_InvalidTestSearchHttpClientWithShortCircuitBreakDelay, "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientWaitAndRetryPolicy(_retryCount, _waitBetweenRetriesInMilliseconds, _loggerFor_InvalidTestSearchHttpClientWithShortCircuitBreakDelay, "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientCircuitBreakerPolicy(
                                  _circuitBreakerFailAfter,
                                  TimeSpan.FromSeconds(_circuitBreakerShortDelaySeconds),
                                  _loggerFor_InvalidTestSearchHttpClientWithShortCircuitBreakDelay,
                                  "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver));

            services.AddHttpClient <TestSearchHttpClient>(_nameFor_ValidTestSearchHttpClient, c => c.BaseAddress = new Uri(_validAddress))
            .AddPolicyHandler(SearchClientPolicies.SearchClientFallBackCircuitBreakerPolicy(_loggerFor_ValidTestSearchHttpClient, "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientWaitAndRetryPolicy(_retryCount, _waitBetweenRetriesInMilliseconds, _loggerFor_ValidTestSearchHttpClient, "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientCircuitBreakerPolicy(
                                  _circuitBreakerFailAfter,
                                  TimeSpan.FromSeconds(_circuitBreakerShortDelaySeconds),
                                  _loggerFor_ValidTestSearchHttpClient,
                                  "InvalidTestSearchHttpClientWithShortCircuitBreakDelay", telemetryServiceResolver));

            services.AddHttpClient <TestSearchHttpClient>(_nameFor_InvalidTestSearchHttpClientRetryCountExpires, c => c.BaseAddress = new Uri(_shortInvalidAddress_2))
            .AddPolicyHandler(SearchClientPolicies.SearchClientFallBackCircuitBreakerPolicy(_loggerFor_InvalidTestSearchHttpClientRetryCountExpires, "InvalidTestSearchHttpClientRetryCountExpires", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientWaitAndRetryPolicy(_retryCount_2, _waitBetweenRetriesInMilliseconds, _loggerFor_InvalidTestSearchHttpClientRetryCountExpires, "InvalidTestSearchHttpClientRetryCountExpires", telemetryServiceResolver))
            .AddPolicyHandler(SearchClientPolicies.SearchClientCircuitBreakerPolicy(
                                  _circuitBreakerFailAfter_2,
                                  TimeSpan.FromSeconds(_circuitBreakerShortDelaySeconds),
                                  _loggerFor_InvalidTestSearchHttpClientRetryCountExpires,
                                  "InvalidTestSearchHttpClientRetryCountExpires", telemetryServiceResolver));

            return(services);
        }