Skip to content

Instantly share code, notes, and snippets.

@wingrime
wingrime / backoff.cs
Created May 27, 2019 08:59
C# exponential backoff
public static class Retry
{
public static async Task<T> DoAsync<T>(Func<Task<T>> action,
Func<T, bool> validateResult = null,
int maxRetries = 10, int maxDelayMilliseconds = 2000, int delayMilliseconds = 200)
{
var backoff = new ExponentialBackoff(delayMilliseconds, maxDelayMilliseconds);
var exceptions = new List<Exception>();
using System;
using System.Runtime.CompilerServices;
// our code
MyType obj = new();
MyExtension ext = (MyExtension)obj;
ext.MyField = 42;
// other code
ext = (MyExtension)obj;