Skip to content

Instantly share code, notes, and snippets.

@aidapsibr
Last active December 16, 2020 20:32
Show Gist options
  • Save aidapsibr/5220f21d75da088f1aca2143e5258385 to your computer and use it in GitHub Desktop.
Save aidapsibr/5220f21d75da088f1aca2143e5258385 to your computer and use it in GitHub Desktop.
using static ResilientOperation;
int failureCount = 0;
var result = await Try(() =>
{
// Fail 3 times
// return 404 (which is somehow transient, think getting a transient 404)
// after 4 retries finally get the correct value: 5 on iteration 5
if (failureCount < 3)
{
failureCount++;
throw new Exception("transient");
}
if (failureCount < 4)
{
failureCount++;
return 5;
}
return 404;
})
.Catch<Exception>(when: ex => ex.Message == "transient", async (op, ex) =>
{
if (op.CurrentAttempt < 4)
{
await op.RetryAfterAsync(TimeSpan.FromMilliseconds(100));
}
})
.CatchResult(when: result => result == 404, async (op, result) =>
{
await op.RetryAfterAsync(TimeSpan.FromMilliseconds(100));
})
.InvokeAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment