Skip to content

Instantly share code, notes, and snippets.

@ilopez
Created December 19, 2015 21:56
Show Gist options
  • Save ilopez/b4be3f7d95998ec29eb1 to your computer and use it in GitHub Desktop.
Save ilopez/b4be3f7d95998ec29eb1 to your computer and use it in GitHub Desktop.
Implementing Exponential Backoff in RestSharp for Execute<T> Method
public class KRestClient : RestClient
{
public Int32 RequestRetryCount = 5; // Attempt Types
public Int32 BackoffRate = 100; // Milliseconds
public KRestClient(string baseUrl) : base(baseUrl)
{
}
public override IRestResponse<T> Execute<T>(IRestRequest request)
{
IRestResponse<T> rs;
int count = 0;
rs = base.Execute<T>(request);
if ((int)rs.StatusCode == 429)
{
while (count <= RequestRetryCount && (int)rs.StatusCode == 429 )
{
count++;
Debug.WriteLine("Attempting Request. Count: " + count);
Thread.Sleep((count ^ 2)* BackoffRate ); // Exponential Backoff
rs = base.Execute<T>(request);
}
}
return rs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment