Skip to content

Simmy API Review and Feedback #1901

@martintmk

Description

@martintmk

I was playing with the new chaos strategies and I think we can simplify the setup of common faults by introducing something similar to PredicateBuilder, but this time for faults.

Instead of:

new ResiliencePipelineBuilder<string>()
    .AddChaosFault(new()
    {
        FaultGenerator = _ =>
        {
            Exception? ex = Random.Shared.Next(380) switch
            {
                < 100 => new InvalidOperationException(),
                < 180 => new HttpRequestException(),
                < 280 => new TimeoutException(),
                < 380 => new TimeoutException(),
                _ => null,
            };

            return ValueTask.FromResult(ex);
        }
    })
    .AddChaosResult(new()
    {
        OutcomeGenerator = _ =>
        {
            Outcome<string>? result = Random.Shared.Next(420) switch
            {
                < 100 => Outcome.FromException<string>(new InvalidOperationException()),
                < 200 => Outcome.FromException<string>(new HttpRequestException()),
                < 300 => Outcome.FromException<string>(new TimeoutException()),
                < 320 => Outcome.FromException<string>(new SocketException()),
                < 420 => Outcome.FromResult("Hello World"),
                _ => null,
            };

            return ValueTask.FromResult(result);
        }
    });

We can allow this simplified API for common scenarios:

new ResiliencePipelineBuilder<string>()
    .AddChaosFault(new()
    {
        FaultGenerator = new FaultGenerator()
            .AddFault<InvalidOperationException>() // default weight is 100
            .AddFault<HttpRequestException>(weight: 80)
            .AddFault<TimeoutException>()
            .AddFault<SocketException>()
    })
    .AddChaosResult(new()
    {
        OutcomeGenerator = new OutcomeGenerator<string>()
            .AddException<InvalidOperationException>() // default weight is 100
            .AddException<HttpRequestException>()
            .AddException<TimeoutException>()
            .AddException<SocketException>(weight: 20)
            .AddResult(() => "Hello World!")
    });

We can also use this issue for additional API feedback.

cc @martincostello , @vany0114

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions