I'm calling a remote service that is rate-limited. If you invoke a method lots of times, it ends up throwing a Exception. I would like to be able to retry a call until it finished successfully. The current code I have is this (notice that I only added a do-loop around your original code :)
public class ExceptionHandlingInterceptor : AsyncInterceptorBase
{
protected override async Task InterceptAsync(IInvocation invocation, Func<IInvocation, Task> proceed)
{
bool rateLimit;
do
{
try
{
// Cannot simply return the the task, as any exceptions would not be caught below.
await proceed(invocation).ConfigureAwait(false);
rateLimit = false;
}
catch (FaceAPIException ex)
{
if (ex.ErrorCode == "RateLimitExceeded")
{
rateLimit = true;
}
else
{
Log.Error($"Error calling {invocation.Method.Name}.", ex);
throw;
}
}
catch (Exception ex)
{
Log.Error($"Error calling {invocation.Method.Name}.", ex);
throw;
}
} while (rateLimit);
}
protected override async Task<T> InterceptAsync<T>(IInvocation invocation, Func<IInvocation, Task<T>> proceed)
{
bool ratelimit = false;
T retValue = default(T);
do
{
try
{
// Cannot simply return the the task, as any exceptions would not be caught below.
retValue = await proceed(invocation).ConfigureAwait(false);
}
catch (FaceAPIException ex)
{
if (ex.ErrorCode == "RateLimitExceeded")
{
ratelimit = true;
}
else
{
Log.Error($"Error calling {invocation.Method.Name}.", ex);
throw;
}
}
catch (Exception ex)
{
Log.Error($"Error calling {invocation.Method.Name}.", ex);
throw;
}
} while (ratelimit);
return retValue;
}
}
I'm calling a remote service that is rate-limited. If you invoke a method lots of times, it ends up throwing a Exception. I would like to be able to retry a call until it finished successfully. The current code I have is this (notice that I only added a do-loop around your original code :)
Thanks in advance!