Popularity
1.2
Stable
Activity
5.7
Declining
8
3
3

Description

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 as well.

Programming language: C#
Tags: Misc     Handling     Exception     Policy     Retry     Rate-limit     Resilience     CircuitBreaker     Fallback     Timeout    
Latest version: v2.4.3

trybot alternatives and similar packages

Based on the "Misc" category

Do you think we are missing an alternative of trybot or a related project?

Add another 'Misc' Package

README

trybot Appveyor build status Travis CI build status Tests coverage 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 as well. Join the chat at https://gitter.im/z4kn4fein/stashbox Slack

Github (stable) NuGet (stable) MyGet (pre-release) Open Hub
Github release NuGet Version MyGet package OpenHub

Supported platforms

  • .NET 4.5 and above
  • Windows 8/8.1/10
  • Windows Phone Silverlight 8/8.1
  • Windows Phone 8.1
  • Xamarin (Android/iOS/iOS Classic)
  • .NET Standard 1.0/2.0

Bots

  • Retry - Allows to configure auto re-execution of an operation based on exceptions it throws, or on its return value.

  • 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 - 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 original operation is not able to do it.

  • Circuit breaker - Prevents the continuous re-execution of a failing operation by blocking the traffic for a configured amount of time, when the number of failures exceed a given threshold. This usually could give some break to the remote resource to heal itself properly.

  • Rate limit - Controls the rate of the operations by specifying a maximum amount of executions within a given time window.

Usage

During the configuration of a bot policy you can chain different bots to eachother.

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 the top to the 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

    // Without lambda parameters
    policy.Execute(() => DoSomeOperation());
    
    // Or with lambda parameters
    policy.Execute((context, cancellationToken) => DoSomeOperation());
    
  • Asynchronously

    // 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.

Extensions

Documentation