Created
January 10, 2015 05:46
-
-
Save AlexArchive/ae255891ad7d319f6eda to your computer and use it in GitHub Desktop.
Exponential back off
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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