Created
December 19, 2015 21:56
-
-
Save ilopez/b4be3f7d95998ec29eb1 to your computer and use it in GitHub Desktop.
Implementing Exponential Backoff in RestSharp for Execute<T> Method
This file contains hidden or 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
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