Skip to content

Instantly share code, notes, and snippets.

@AlexArchive
Created January 10, 2015 05:46
Show Gist options
  • Save AlexArchive/ae255891ad7d319f6eda to your computer and use it in GitHub Desktop.
Save AlexArchive/ae255891ad7d319f6eda to your computer and use it in GitHub Desktop.
Exponential back off
private static async Task<string> DownloadStringWithRetries(string location)
{
using (var client = new HttpClient())
{
var nextDelay = TimeSpan.FromSeconds(1);
for (int i = 0; i != 3; i++)
{
try
{
Console.WriteLine("trying to download {0}.", location);
return await client.GetStringAsync(location);
}
catch
{
}
Console.WriteLine("trying again in {0}.", nextDelay.Seconds);
await Task.Delay(nextDelay);
nextDelay = nextDelay + nextDelay;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment