Skip to content

A c# library to retry code using a truncated binary exponential backoff algorithm

License

Notifications You must be signed in to change notification settings

JorritSalverda/RetryMagic

Repository files navigation

RetryMagic

A c# library to retry code using a truncated binary exponential backoff algorithm

Build Status .NET Version License

Why?

To make more robust applications make sure to retry any external call, whether it's a database call or a web request. The truncated binary exponential backoff algorithm is used to avoid congestion and is truncated/capped by default to keep the maximum interval within reason

Usage

You can retry either an Action or a Func<T>

Retry.Action(() => { _databaseRepository.Update(databaseObject); });
return Retry.Function(() => { return _databaseRepository.Get(id); });

Changing defaults

The following default settings are used and can be changed by using the following code with different values

Retry.UpdateSettings(new RetrySettings(
	jitterSettings: new JitterSettings(percentage: 25), 
	maximumNumberOfAttempts: 5, 
	millisecondsPerSlot: 32, 
	truncateNumberOfSlots: true, 
	maximumNumberOfSlotsWhenTruncated: 16));

Validation of settings always takes place during construction of the object so it fails as early as possible.

Non-static usage

If you wish to be able to inject it - for example for having different settings in different places - you can use the RetryInstance class:

IRetryInstance instance = new RetryInstance(new RetrySettings(
	jitterSettings:new JitterSettings(percentage: 25), 
	maximumNumberOfAttempts: 5, 
	millisecondsPerSlot: 32, 
	truncateNumberOfSlots: true, 
	maximumNumberOfSlotsWhenTruncated: 16));

This interface and class only has the Action and Function methods without the settings parameter besides the Action or Func<T>, because you provide those during construction.

instance.Action(() => { _databaseRepository.Update(databaseObject); });
return instance.Function(() => { return _databaseRepository.Get(id); });

Get it

First, install NuGet. Then, install RetryMagic from the package manager console:

PM> Install-Package RetryMagic

RetryMagic is Copyright © 2015 Jorrit Salverda and other contributors under the MIT license.

About

A c# library to retry code using a truncated binary exponential backoff algorithm

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages