Code Monkey home page Code Monkey logo

httpclient-resilience-policies's Introduction

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
        ExtraBreakCondition = BreakConditions.OnTooManyRequests // Extra condition for CircuitBreaker to open (opens on TooManyRequests by default)
    }

    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

httpclient-resilience-policies's People

Contributors

ahydrax avatar ceridan avatar dashabulanova avatar divanov1980 avatar evjenio avatar nymezide avatar rasteniy avatar spacentropy avatar tatianaepifanova avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

httpclient-resilience-policies's Issues

Add `AddResiliencePolicies` extension method

Previous AddDefaultPolicies method was the most frequently used extension method. We should return it, but we want to rename it to AddResiliencePolicies because word "default" has unclear meaning.

Make clean API for end-users without strict bound to Polly

Currently, we have strict bounds with Polly library. For example, in the settings classes we have code like this Action<DelegateResult<HttpResponseMessage>, TimeSpan> DoNothingOnBreak, where DelegateResult is a Polly class which exposed to the end-users. We want to get rid of it in the public classes to separate library API and details of it implementation.
It gives us huge benefits:

  • We can replace Polly with something else without breaking changes for users.
  • It relaxes the requirement to have the same version of Polly in the client code and our library, which is quite important to integration.

The downside of this solution is bunch of boilerplate code for wrapping some Polly classes.

Out of support .NET Core 2.1

.NET Core 2.1 is out of support. We should remove its support too. Additionally, it allows us to get rid of conditionals in csproj.

Test `Should_break_after_4_concurrent_calls` looks flaky

Test Should_break_after_4_concurrent_calls looks flaky. It usually represents on my local machine. 2 of 3 tests are failed for netcoreapp2.1:

Dodo.HttpClientResiliencePolicies.Tests.CircuitBreakerTests.Should_break_after_4_concurrent_calls

  Expected: 2
  But was:  3

AddJsonClient doesn't respect clientName parameter

AddJsonClient extension method provided by the library has clientName argument to create named clients but actually never use it.

Actually it is not a bug, it was never implemented. We should fix it.

"The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout." exception occurs on small OverallTimeout

Exception:
Polly.Timeout.TimeoutRejectedException : The delegate executed asynchronously through TimeoutPolicy did not complete within the timeout.
----> System.OperationCanceledException : The operation was canceled.

Way to reproduce:

		[Test]
		public async Task OverallTimeoutError()
		{
			var settings = new ResiliencePoliciesSettings
			{
				OverallTimeout = TimeSpan.FromMilliseconds(10)
			};
			var wrapper = Create.HttpClientWrapperWrapperBuilder
				.WithStatusCode(HttpStatusCode.ServiceUnavailable)
				.WithResiliencePolicySettings(settings)
				.Please();

			await wrapper.Client.GetAsync("http://localhost");
		}

Support net 4.6.1

Add support for full framework. Check library on Windows with full framework.

Add net5 support

.NET 5 finally released. We should add support for this target.

implement Phi-Accrual Failure Detector for adjust timeout at runtime

Problem description
The choice of the correct timeout is always a complicated task.

The idea is rather than using a configured constant timeout continually measure response times and automatically adjust timeouts according to the observation of response time distribution. This can be done with a Phi Accrual failure detector algorithm.

Fix deprecated csproj fields

Problem description
Release pipeline mark some fields as deprecated. See logs below:

##[warning]/opt/hostedtoolcache/dncs/3.1.100/x64/sdk/3.1.100/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(198,5): warning NU5125: The 'licenseUrl' element will be deprecated. Consider using the 'license' element instead. [/home/runner/work/httpclient-resilience-policies/httpclient-resilience-policies/src/Dodo.HttpClient.ResiliencePolicies/Dodo.HttpClient.ResiliencePolicies.csproj]
##[warning]/opt/hostedtoolcache/dncs/3.1.100/x64/sdk/3.1.100/Sdks/NuGet.Build.Tasks.Pack/build/NuGet.Build.Tasks.Pack.targets(198,5): warning NU5048: The 'PackageIconUrl'/'iconUrl' element is deprecated. Consider using the 'PackageIcon'/'icon' element instead. Learn more at https://aka.ms/deprecateIconUrl [/home/runner/work/httpclient-resilience-policies/httpclient-resilience-policies/src/Dodo.HttpClient.ResiliencePolicies/Dodo.HttpClient.ResiliencePolicies.csproj]

HttpClientSettings refactoring

Problem description

The first problem is the name of HttpClientSettings does not reflect the core idea of the library. It would be better PollyDefaultSettings or something like this.

The second problem is according to framework design guidelines using constructors is not very usable in this type of object. It would be better to make public setters for all settings.

Add PDB files to NuGet package

Add PDB to NuGet package to support code navigation.
See AllowedOutputExtensionsInPackageBuildOutputFolder csproj property.

Add wiki documentation

Problem description
Add wiki documentation to project and move specified files to wiki:

  • Contributing.md
  • README.md
  • CI_AND_RELEASE.md

Update README to clarify repository main goal

Update README to more precisely describe the problem which is solved by this library. The essential part of this repo is not only provide wraps of Polly policies, but also create proper order in which policies should applied to provide correct request/response flow. Otherwise you may get unexpected behavior of you http client.

Reporting a vulnerability

Hello!

I hope you are doing well!

We are a security research team. Our tool automatically detected a vulnerability in this repository. We want to disclose it responsibly. GitHub has a feature called Private vulnerability reporting, which enables security research to privately disclose a vulnerability. Unfortunately, it is not enabled for this repository.

Can you enable it, so that we can report it?

Thanks in advance!

PS: you can read about how to enable private vulnerability reporting here: https://docs.github.com/en/code-security/security-advisories/repository-security-advisories/configuring-private-vulnerability-reporting-for-a-repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.