Skip to content

Resilience

moattarwork edited this page Dec 14, 2022 · 1 revision

LittleBlocks provides a set of extensions to facilitate the resiliency of the distributed applications. It uses Polly underneath which provides the core functionality.

This can be helpful in some scenarios such as:

  • Retry operation on running different operations on databases or external services
  • Timeout on long-running services
  • Circuit Broker

Here is the link to the documentation for Polly and Distributed Architectural Patterns

Installation

To use this set of extension methods you must install LittleBlocks.Resilience package

Install-Package LittleBlocks.Resilience

or

dotnet add package LittleBlocks.Resilience

Usage

The package offers the following extension method:

public static void ExecuteWithPolicy<T>(this T instance, Action<T> action, Policy policy) {}
public static TResult ExecuteWithPolicy<T, TResult>(this T instance, Func<T, TResult> func, Policy policy) {}
public static Task ExecuteWithPolicyAsync<T>(this T instance, Func<T, Task> func, AsyncPolicy policy) {}
public static Task<TResult> ExecuteWithPolicyAsync<T, TResult>(this T instance, Func<T, Task<TResult>> func, AsyncPolicy policy) {}

Also, It offers the same methods with a policy name that can be resolved from the Policy Registry.

Policy Patterns

The component provides the base configuration for some policies and strategies for support of basic usages:

// Some composite strategies
public static Policy DataStoreResilienceStrategy<TException>(Action<PolicyOptions> configure = null) {}
public static Policy ServiceCallResilienceStrategy<TException>(Action<PolicyOptions> configure = null) {}
public static AsyncPolicy DataStoreResilienceStrategyAsync<TException>(Action<PolicyOptions> configure = null) {}
public static AsyncPolicy ServiceCallResilienceStrategyAsync<TException>(Action<PolicyOptions> configure = null) {}

// Or some basic ones such as

public static Policy RetryPolicy<TException>(PolicyRetryOptions options) {}
public static AsyncPolicy RetryPolicyAsync<TException>(PolicyRetryOptions options) {}

Finally, you can configure the PolicyRegistry to store the standard policies in the ServiceCollection container and resolve the policies dynamically to be used in different usages. Here is a sample:

// In startup.cs

var services = new ServiceCollection();
services.AddPolicySupport(m =>
{
	m.Add(ResiliencePolicy, PolicyFor.ServiceCallResilienceStrategy<Exception>(o => o.CircuitBreaker.NumberOfExceptionsBefore = 4));
	m.Add(AsyncResiliencePolicy, PolicyFor.ServiceCallResilienceStrategyAsync<Exception>(o => o.CircuitBreaker.NumberOfExceptionsBefore = 4));
});


...

// Usage 

public class CallContainer
{
	private IReadOnlyPolicyRegistry<string> _registry;
	private IExternalClient _client;

	public CallContainer(IReadOnlyPolicyRegistry<string> registry, IExternalClient client)
	{
		_registry = registry;
		_client = client;
	}

	public Task CallToExternalAsync() 
	{
		return _client.ExecuteWithPolicyAsync(c => c.GetAsync(), AsyncResiliencePolicy, _registry);
	}
}
> > 
Clone this wiki locally