AsyncDolls.MessageSenderExtensions.RetryOnThrottle C# (CSharp) Method

RetryOnThrottle() public static method

public static RetryOnThrottle ( this sender, Task>.Func action, System.TimeSpan delay, int maxRetryAttempts, int retryAttempts ) : Task
sender this
action Task>.Func
delay System.TimeSpan
maxRetryAttempts int
retryAttempts int
return Task
        public static Task RetryOnThrottle(this IMessageSender sender, Func<IMessageSender, Task> action, TimeSpan delay, int maxRetryAttempts, int retryAttempts = 0)
        {
            var task = action(sender);

            return task.ContinueWith(async t =>
            {
                var exception = ExceptionDispatchInfo.Capture(t.Exception?.InnerException);
                var serverBusy = exception.SourceException is InvalidOperationException;

                if (serverBusy && retryAttempts < maxRetryAttempts)
                {
                    await Task.Delay(delay);
                    await sender.RetryOnThrottle(action, delay, maxRetryAttempts, ++retryAttempts);
                }
                else if(t.IsFaulted)
                {
                    exception.Throw();
                }
            })
            .Unwrap();
        }
    }
MessageSenderExtensions