Dodo.HttpClient.ResiliencePolicies 2.0.1

The main goal of this library is to provide unified http request retrying policies for the HttpClient that just works. Actually this library wraps awesome Polly library with the predefined settings to allow developers to use it as is without a deep dive to Polly.

Install-Package Dodo.HttpClient.ResiliencePolicies -Version 2.0.1
dotnet add package Dodo.HttpClient.ResiliencePolicies --version 2.0.1
<PackageReference Include="Dodo.HttpClient.ResiliencePolicies" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dodo.HttpClient.ResiliencePolicies --version 2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Dodo.HttpClient.ResiliencePolicies, 2.0.1"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dodo.HttpClient.ResiliencePolicies as a Cake Addin
#addin nuget:?package=Dodo.HttpClient.ResiliencePolicies&version=2.0.1

// Install Dodo.HttpClient.ResiliencePolicies as a Cake Tool
#tool nuget:?package=Dodo.HttpClient.ResiliencePolicies&version=2.0.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Dodo.HttpClient.ResiliencePolicies library

nuget
master

Dodo.HttpClient.ResiliencePolicies library extends IHttpClientBuilder with easy to use resilience policies for the HttpClient.

In the world of microservices it is quite important to pay attention to resilience of communications between services. You have to think about things like retries, timeouts, circuit breakers, etc.
We already have a great library for this class of problems called Polly. It is really powerful. Polly is like a Swiss knife gives you a lot of functionality, but you should know how and when to use it. It could be a complicated task.

Main goal of our library is to hide this complexity from the end-users. It uses Polly under the hood and provides some pre-defined functionality with reasonable defaults and minimal settings to setup resilience policies atop of HttpClient.
You can just plug the with single line of code and your HttpClient will become much more robust than before.

Functionality provided by the library

Library provides few methods which returns IHttpClientBuilder and you may chain it with other HttpMessageHandler.

There are list of public methods to use:

// Pre-defined policies with defaults settings
IHttpClientBuilder AddResiliencePolicies(this IHttpClientBuilder clientBuilder);

// Pre-defined policies with custom settings
IHttpClientBuilder AddResiliencePolicies(this IHttpClientBuilder clientBuilder, ResiliencePoliciesSettings settings)

AddResiliencePolicies wraps HttpClient with four policies:

  • Overall Timeout policy – timeout for entire request, after this time we are not interested in the result anymore.
  • Retry policy – defines how much and how often we will attempt to send request again on failures.
  • Circuit Breaker policy – defines when we should take a break in our retries if the upstream service doesn't respond.
  • Timeout Per Try policy - timeout for each try (defined in Retry policy), after this time attempt considered as failure.

Library also provides pre-configured HttpClient:

// Pre-defined HttpClientFactory which is configured to work with `application/json` MIME media type and uses default ResiliencePolicies
IHttpClientBuilder AddJsonClient<TClientInterface, TClientImplementation>(
			this IServiceCollection sc,
			Uri baseAddress,
			string clientName = null)

// Pre-defined HttpClientFactory which is configured to work with `application/json` MIME media type and uses ResiliencePolicies with custom settings
IHttpClientBuilder AddJsonClient<TClientInterface, TClientImplementation>(
			this IServiceCollection sc,
			Uri baseAddress,
			ResiliencePoliciesSettings settings,
			string clientName = null)

Custom settings can be provided via ResiliencePoliciesSettings (see examples below).
Also you may check the defaults provided by the library (all of this can be overriden in custom settings).

Usage examples

  1. Using default client provided by the library and add it to the ServiceCollection in the Startup like this:

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    service                         // IServiceCollection
        .AddJsonClient(...)         // HttpClientFactory to build JsonClient provided by the library with all defaults
    
  2. Add resilience policies with default settings to existing HttpClient

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    service                         // IServiceCollection
        .AddHttpClient(...)         // Existing HttpClientFactory
        .AddResiliencePolicies()    // Pre-defined resilience policies with all defaults
    
  3. Define custom settings for resilience policies:

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    var settings = new ResiliencePoliciesSettings
    {
        OverallTimeout = TimeSpan.FromSeconds(50),
        TimeoutPerTry = TimeSpan.FromSeconds(2),
        RetryPolicySettings = RetryPolicySettings.Jitter(2, TimeSpan.FromMilliseconds(50)),
        CircuitBreakerPolicySettings = new CircuitBreakerPolicySettings(
            failureThreshold: 0.5,
            minimumThroughput: 10,
            durationOfBreak: TimeSpan.FromSeconds(5),
            samplingDuration: TimeSpan.FromSeconds(30)
        ),
        OnRetry = (response, time) => { ... },      // Handle retry event. For example you may add logging here
        OnBreak = (response, time) => { ... },      // Handle CircuitBreaker break event. For example you may add logging here
        OnReset = () => {...},                      // Handle CircuitBreaker reset event. For example you may add logging here
        OnHalfOpen = () => {...},                   // Handle CircuitBreaker reset event. For example you may add logging here
    }
    

    You may provide only properties which you want to customize, the defaults will be used for the rest.
    You may choose different retry strategies. RetryPolicySettings provides static methods to choose Constant, Linear, Exponential or Jitter (exponential with jitter backoff) strategies. Jitter is used as default strategy.

    You may provide settings as a parameter to .AddJsonClient(...) or .AddResiliencePolicies() to override default settings.

References

Dodo.HttpClient.ResiliencePolicies library

nuget
master

Dodo.HttpClient.ResiliencePolicies library extends IHttpClientBuilder with easy to use resilience policies for the HttpClient.

In the world of microservices it is quite important to pay attention to resilience of communications between services. You have to think about things like retries, timeouts, circuit breakers, etc.
We already have a great library for this class of problems called Polly. It is really powerful. Polly is like a Swiss knife gives you a lot of functionality, but you should know how and when to use it. It could be a complicated task.

Main goal of our library is to hide this complexity from the end-users. It uses Polly under the hood and provides some pre-defined functionality with reasonable defaults and minimal settings to setup resilience policies atop of HttpClient.
You can just plug the with single line of code and your HttpClient will become much more robust than before.

Functionality provided by the library

Library provides few methods which returns IHttpClientBuilder and you may chain it with other HttpMessageHandler.

There are list of public methods to use:

// Pre-defined policies with defaults settings
IHttpClientBuilder AddResiliencePolicies(this IHttpClientBuilder clientBuilder);

// Pre-defined policies with custom settings
IHttpClientBuilder AddResiliencePolicies(this IHttpClientBuilder clientBuilder, ResiliencePoliciesSettings settings)

AddResiliencePolicies wraps HttpClient with four policies:

  • Overall Timeout policy – timeout for entire request, after this time we are not interested in the result anymore.
  • Retry policy – defines how much and how often we will attempt to send request again on failures.
  • Circuit Breaker policy – defines when we should take a break in our retries if the upstream service doesn't respond.
  • Timeout Per Try policy - timeout for each try (defined in Retry policy), after this time attempt considered as failure.

Library also provides pre-configured HttpClient:

// Pre-defined HttpClientFactory which is configured to work with `application/json` MIME media type and uses default ResiliencePolicies
IHttpClientBuilder AddJsonClient<TClientInterface, TClientImplementation>(
			this IServiceCollection sc,
			Uri baseAddress,
			string clientName = null)

// Pre-defined HttpClientFactory which is configured to work with `application/json` MIME media type and uses ResiliencePolicies with custom settings
IHttpClientBuilder AddJsonClient<TClientInterface, TClientImplementation>(
			this IServiceCollection sc,
			Uri baseAddress,
			ResiliencePoliciesSettings settings,
			string clientName = null)

Custom settings can be provided via ResiliencePoliciesSettings (see examples below).
Also you may check the defaults provided by the library (all of this can be overriden in custom settings).

Usage examples

  1. Using default client provided by the library and add it to the ServiceCollection in the Startup like this:

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    service                         // IServiceCollection
        .AddJsonClient(...)         // HttpClientFactory to build JsonClient provided by the library with all defaults
    
  2. Add resilience policies with default settings to existing HttpClient

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    service                         // IServiceCollection
        .AddHttpClient(...)         // Existing HttpClientFactory
        .AddResiliencePolicies()    // Pre-defined resilience policies with all defaults
    
  3. Define custom settings for resilience policies:

    using Dodo.HttpClientResiliencePolicies;
    ...
    
    var settings = new ResiliencePoliciesSettings
    {
        OverallTimeout = TimeSpan.FromSeconds(50),
        TimeoutPerTry = TimeSpan.FromSeconds(2),
        RetryPolicySettings = RetryPolicySettings.Jitter(2, TimeSpan.FromMilliseconds(50)),
        CircuitBreakerPolicySettings = new CircuitBreakerPolicySettings(
            failureThreshold: 0.5,
            minimumThroughput: 10,
            durationOfBreak: TimeSpan.FromSeconds(5),
            samplingDuration: TimeSpan.FromSeconds(30)
        ),
        OnRetry = (response, time) => { ... },      // Handle retry event. For example you may add logging here
        OnBreak = (response, time) => { ... },      // Handle CircuitBreaker break event. For example you may add logging here
        OnReset = () => {...},                      // Handle CircuitBreaker reset event. For example you may add logging here
        OnHalfOpen = () => {...},                   // Handle CircuitBreaker reset event. For example you may add logging here
    }
    

    You may provide only properties which you want to customize, the defaults will be used for the rest.
    You may choose different retry strategies. RetryPolicySettings provides static methods to choose Constant, Linear, Exponential or Jitter (exponential with jitter backoff) strategies. Jitter is used as default strategy.

    You may provide settings as a parameter to .AddJsonClient(...) or .AddResiliencePolicies() to override default settings.

References

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.0.1 4,533 12/7/2020
1.0.3 1,557 10/9/2020
1.0.2 16,330 5/19/2020
1.0.1 214 4/10/2020