https://github.com/z4kn4fein/trybot
A transient fault handling framework including such resiliency solutions as Retry, Timeout, Fallback, Rate Limit and Circuit Breaker.
https://github.com/z4kn4fein/trybot
circuit-breaker fallback netstandard rate-limiter resilience retry-strategies timeout transient-fault-handling
Last synced: 7 months ago
JSON representation
A transient fault handling framework including such resiliency solutions as Retry, Timeout, Fallback, Rate Limit and Circuit Breaker.
- Host: GitHub
- URL: https://github.com/z4kn4fein/trybot
- Owner: z4kn4fein
- License: mit
- Created: 2015-09-03T22:01:02.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2021-04-04T11:55:13.000Z (over 4 years ago)
- Last Synced: 2025-02-28T21:24:39.046Z (7 months ago)
- Topics: circuit-breaker, fallback, netstandard, rate-limiter, resilience, retry-strategies, timeout, transient-fault-handling
- Language: C#
- Homepage:
- Size: 322 KB
- Stars: 18
- Watchers: 5
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Trybot
[](https://ci.appveyor.com/project/pcsajtai/trybot/branch/master) [](https://travis-ci.org/z4kn4fein/trybot) [](https://ci.appveyor.com/project/pcsajtai/trybot-1453m/build/tests) [](https://codecov.io/gh/z4kn4fein/trybot) [](https://github.com/dotnet/sourcelink)Trybot is a transient fault handling framework including such resiliency solutions as **Retry**, **Timeout**, **Fallback**, **Rate limit**, and **Circuit Breaker**. The framework is extendable with **custom, user-defined** bots.
Github (stable) | NuGet (stable) | Fuget (stable) | NuGet (pre-release)
--- | --- | --- | ---
[](https://github.com/z4kn4fein/trybot/releases) | [](https://www.nuget.org/packages/trybot/) | [](https://www.fuget.org/packages/Trybot) | [](https://www.nuget.org/packages/Trybot/)## Bots
- **[Retry](https://github.com/z4kn4fein/trybot/wiki/Retry)** - Allows configuring auto re-execution of an operation based on exceptions it throws or on its return value.- **[Timeout](https://github.com/z4kn4fein/trybot/wiki/Timeout)** - Ensures that the caller won't have to wait indefinitely for an operation to finish by setting a maximum time range within the given operation should be executed.
- **[Fallback](https://github.com/z4kn4fein/trybot/wiki/Fallback)** - Handles faults by executing an alternative operation when the original one is failing, also provides the ability to produce an alternative result value when the actual operation is not able to do it.
- **[Circuit breaker](https://github.com/z4kn4fein/trybot/wiki/Circuit-breaker)** - When the number of failures exceeds a given threshold, this bot prevents the continuous re-execution of the failing operation by blocking the traffic for a configured amount of time. This usually could give some break to the remote resource to heal itself properly.
- **[Rate limit](https://github.com/z4kn4fein/trybot/wiki/Rate-limit)** - Controls the rate of the operations by specifying a maximum amount of executions within a given time window.
## Supported Platforms
- .NET 4.5 and above
- .NET Core
- Mono
- Universal Windows Platform
- Xamarin (Android/iOS/Mac)
- Unity## Usage
During the configuration of a bot policy, you can chain different bots to each other.```c#
policy.Configure(policyConfig => policyConfig
.CircuitBreaker(circuitBreakerConfig => circuitBreakerConfig
.DurationOfOpen(TimeSpan.FromSeconds(10))
.BrakeWhenExceptionOccurs(exception => exception is HttpRequestException),
strategyConfig => strategyConfig
.FailureThresholdBeforeOpen(5)
.SuccessThresholdInHalfOpen(2)).Retry(retryConfig => retryConfig
.WithMaxAttemptCount(5)
.WhenExceptionOccurs(exception => exception is HttpRequestException)
.WaitBetweenAttempts((attempt, exception) =>
{
if(exception is CircuitOpenException cbException)
return TimeSpan.FromSeconds(cbException.OpenDuration);return TimeSpan.FromSeconds(Math.Pow(2, attempt);
}))).Timeout(timeoutConfig => timeoutConfig
.After(TimeSpan.FromSeconds(120))));
```
> The handling order of the given operation would be the same as the configuration order from top to bottom. That means in the example above that the circuit breaker will try to execute the given operation first, then if it fails, the retry bot will start to re-execute it until the timeout bot is not signaling a cancellation.Then you can execute the configured policy:
- With cancellation:
```c#
var tokenSource = new CancellationTokenSource();
policy.Execute((context, cancellationToken) => DoSomeCancellableOperation(cancellationToken), tokenSource.Token);
```- With a custom correlation id:
```c#
var correlationId = Guid.NewGuid();
policy.Execute((context, cancellationToken) => DoSomeOperationWithCorrelationId(context.CorrelationId), correlationId);
```
> Without setting a custom correlation id, the framework will always generate a unique one for every policy execution.- Synchronously:
```c#
// Without lambda parameters
policy.Execute(() => DoSomeOperation());// Or with lambda parameters
policy.Execute((context, cancellationToken) => DoSomeOperation());
```- Asynchronously:
```c#
// Without lambda parameters
await policy.ExecuteAsync(() => DoSomeAsyncOperation());// Or with lambda parameters
await policy.ExecuteAsync((context, cancellationToken) => DoSomeAsyncOperation());
```> You can also create your custom bots as described [here](https://github.com/z4kn4fein/trybot/wiki/Custom-bots).
## Contact & Support
- [](https://gitter.im/z4kn4fein/trybot)
- Create an [issue](https://github.com/z4kn4fein/trybot/issues) for bug reports, feature requests, or questions.
- Add a ⭐️ to support the project!## Extensions
- ASP.NET Core
- [Trybot.Extensions.Http](https://github.com/z4kn4fein/trybot-extensions-http)
- Other
- [Distributed Circuit Breaker simulation](https://github.com/z4kn4fein/trybot/tree/master/sandbox/trybot.distributedcb)## Documentation
- [Wiki](https://github.com/z4kn4fein/trybot/wiki)
- [Resiliency patterns](https://docs.microsoft.com/en-us/azure/architecture/patterns/category/resiliency)