Skip to content

Commit

Permalink
PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
martintmk committed Jun 16, 2023
1 parent 2f7fe4a commit 58b662a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 6 additions & 5 deletions samples/GenericStrategies/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
{
FallbackAction = async _ =>
{
await Task.Yield();
await Task.Delay(10);

// return fallback result
// Return fallback result
return new Outcome<HttpResponseMessage>(new HttpResponseMessage(System.Net.HttpStatusCode.OK));
},
// You can also use switch expressions for succinct syntax
ShouldHandle = outcome => outcome switch
{
// The "PredicateResult.True" is shorthand to "new ValueTask<bool>(true)"
{ Exception: HttpRequestException } => PredicateResult.True,
{ Result: HttpResponseMessage response } when response.StatusCode == HttpStatusCode.InternalServerError => PredicateResult.True,
_ => PredicateResult.False
Expand All @@ -35,9 +36,8 @@
ShouldRetry = outcome =>
{
// We can handle specific result
if (outcome.Result?.StatusCode == System.Net.HttpStatusCode.InternalServerError)
if (outcome.Result?.StatusCode == HttpStatusCode.InternalServerError)
{
// The "PredicateResult.True" is shorthand to "new ValueTask<bool>(true)"
return PredicateResult.True;
}

Expand Down Expand Up @@ -66,7 +66,8 @@
var response = await strategy.ExecuteAsync(
async token =>
{
await Task.Yield();
await Task.Delay(10);
// This causes the action fail, thus using the fallback strategy above
return new HttpResponseMessage(HttpStatusCode.InternalServerError);
},
CancellationToken.None);
Expand Down
6 changes: 3 additions & 3 deletions samples/Intro/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
strategy.Execute(() => { });

// Asynchronously
await strategy.ExecuteAsync(async token => { await Task.Yield(); }, CancellationToken.None);
await strategy.ExecuteAsync(async token => { await Task.Delay(10); }, CancellationToken.None);

// Synchronously with result
strategy.Execute(token => "some-result");

// Asynchronously with result
await strategy.ExecuteAsync(async token => { await Task.Yield(); return "some-result"; }, CancellationToken.None);
await strategy.ExecuteAsync(async token => { await Task.Delay(10); return "some-result"; }, CancellationToken.None);

// Use state to avoid lambda allocation
strategy.Execute(static state => state, "my-state");
Expand All @@ -53,7 +53,7 @@
return PredicateResult.False;
},
// Register user callback called whenever retry occurs
OnRetry = _ => { Console.WriteLine("Retrying..."); return default; },
OnRetry = args => { Console.WriteLine($"Retrying...{args.Arguments.Attempt} attempt"); return default; },
BaseDelay = TimeSpan.FromMilliseconds(400),
BackoffType = RetryBackoffType.Constant,
RetryCount = 3
Expand Down

0 comments on commit 58b662a

Please sign in to comment.