Skip to content

How would I retry a failed method? #25

Description

@SuperJMN

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;
            }            
        }

Thanks in advance!

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions