簡體   English   中英

如何重試直到滿足某些條件

[英]How to retry until some condition is met

我需要重試某種方法,直到它返回非空的Guid。

有一個很棒的答案會根據是否存在異常重試。 但是,我想將此類通用化,以便能夠處理任何指定的條件。

當前用法將執行特定次數的操作,直到沒有例外:

Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1));

要么:

Retry.Do(SomeFunctionThatCanFail, TimeSpan.FromSeconds(1));

要么:

int result = Retry.Do(SomeFunctionWhichReturnsInt, TimeSpan.FromSeconds(1), 4);

如何修改此類,使其根據傳入的函數的返回值重試?

例如,如果我想重試直到函數返回3:

Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1)).Until(3);

這意味着每隔1秒執行SomeFunctionThatCanFail()直到SomeFunctionThatCanFail()= 3?

在滿足條件之前,我如何概括Retry.Do的用法?

public static class Retry
{
   public static void Do(
       Action action,
       TimeSpan retryInterval,
       int retryCount = 3)
   {
       Do<object>(() => 
       {
           action();
           return null;
       }, retryInterval, retryCount);
   }

   public static T Do<T>(
       Func<T> action, 
       TimeSpan retryInterval,
       int retryCount = 3)
   {
       var exceptions = new List<Exception>();

       for (int retry = 0; retry < retryCount; retry++) //I would like to change this logic so that it will retry not based on whether there is an exception but based on the return value of Action
       {
          try
          { 
              if (retry > 0)
                  Thread.Sleep(retryInterval);
              return action();
          }
          catch (Exception ex)
          { 
              exceptions.Add(ex);
          }
       }

       throw new AggregateException(exceptions);
   }
}

如何創建以下界面:

public interface IRetryCondition<TResult>
{
     TResult Until(Func<TResult, bool> condition);
}

public class RetryCondition<TResult> : IRetryCondition<TResult>
{
     private TResult _value;
     private Func<IRetryCondition<TResult>> _retry;

     public RetryCondition(TResult value, Func<IRetryCondition<TResult>> retry)
     {
         _value = value;
         _retry = retry;
     }

     public TResult Until(Func<TResult, bool> condition)
     {
         return condition(_value) ? _value : _retry().Until(condition);
     }
}

然后,您將更新Retry靜態類:

public static class Retry
{
    // This method stays the same
    // Returning an IRetryCondition does not make sense in a "void" action
    public static void Do(
       Action action,
       TimeSpan retryInterval,
       int retryCount = 3)
    {
        Do<object>(() => 
        {
            action();
            return null;
        }, retryInterval, retryCount);
    }

    // Return an IRetryCondition<T> instance
    public static IRetryCondition<T> Do<T>(
       Func<T> action, 
       TimeSpan retryInterval,
       int retryCount = 3)
    {
        var exceptions = new List<Exception>();

        for (int retry = 0; retry < retryCount; retry++)
        {
            try
            { 
               if (retry > 0)
                  Thread.Sleep(retryInterval);

               // We return a retry condition loaded with the return value of action() and telling it to execute this same method again if condition is not met.
               return new RetryCondition<T>(action(), () => Do(action, retryInterval, retryCount));
            }
            catch (Exception ex)
            { 
                exceptions.Add(ex);
            }
        }

        throw new AggregateException(exceptions);
    }
}

您將能夠實現以下目標:

int result = Retry.Do(() => SomeFunctionThatCanFail(), TimeSpan.FromSeconds(1)).Until(r => r == 3);

更實用的方法

我試圖提出一個更加“面向功能”的解決方案(有點類似於LINQ):

首先,我們將有兩個接口來執行操作:

public interface IRetryResult
{
    void Execute();
}

public interface IRetryResult<out TResult>
{
    TResult Execute();
}

然后,我們需要兩個接口來配置重試操作:

public interface IRetryConfiguration : IRetryResult
{
    IRetryConfiguration Times(int times);
    IRetryConfiguration Interval(TimeSpan interval);
}

public interface IRetryConfiguration<out TResult> : IRetryResult<TResult>
{
    IRetryConfiguration<TResult> Times(int times);
    IRetryConfiguration<TResult> Interval(TimeSpan interval);
    IRetryConfiguration<TResult> Until(Function<TResult, bool> condition);
}

最后,兩個接口都需要兩種實現:

public class ActionRetryConfiguration : IRetryConfiguration
{
    private readonly Action _action;
    private readonly int? _times;
    private readonly TimeSpan? _interval;

    public ActionRetryConfiguration(Action action, int? times, TimeSpan? interval)
    {
        _action = action;
        _times = times;
        _interval = interval;
    }

    public void Execute()
    {
        Execute(_action, _times, _interval);
    }

    private void Execute(Action action, int? times, TimeSpan? interval)
    {
        action();
        if (times.HasValue && times.Value <= 1) return;
        if (times.HasValue && interval.HasValue) Thread.Sleep(interval.Value);
        Execute(action, times - 1, interval);
    }

    public IRetryConfiguration Times(int times)
    {
        return new ActionRetryConfiguration(_action, times, _interval);
    }

    public IRetryConfiguration Interval(TimeSpan interval)
    {
        return new ActionRetryConfiguration(_action, _times, interval);
    }
}


public class FunctionRetryConfiguration<TResult> : IRetryConfiguration<TResult>
{
    private readonly Func<TResult> _function;
    private readonly int? _times;
    private readonly TimeSpan? _interval;
    private readonly Func<TResult, bool> _condition;

    public FunctionRetryConfiguration(Func<TResult> function, int? times, TimeSpan? interval, Func<TResult, bool> condition)
    {
        _function = function;
        _times = times;
        _interval = interval;
        _condition = condition;
    }

    public TResult Execute()
    {
        return Execute(_function, _times, _interval, _condition);
    }

    private TResult Execute(Func<TResult> function, int? times, TimeSpan? interval, Func<TResult, bool> condition)
    {
        TResult result = function();
        if (condition != null && condition(result)) return result;
        if (times.HasValue && times.Value <= 1) return result;
        if ((times.HasValue || condition != null) && interval.HasValue) Thread.Sleep(interval.Value);
        return Execute(function, times - 1, interval, condition);
    }

    public IRetryConfiguration<TResult> Times(int times)
    {
        return new FunctionRetryConfiguration<TResult>(_function, times, _interval, _condition);
    }

    public IRetryConfiguration<TResult> Interval(TimeSpan interval)
    {
        return new FunctionRetryConfiguration<TResult>(_function, _times, interval, _condition);
    }

    public IRetryConfiguration<TResult> Until(Func<TResult, bool> condition)
    {
        return new FunctionRetryConfiguration<TResult>(_function, _times, _interval, condition);
    }
}

最后, Retry靜態類的入口點:

public static class Retry
{
    public static IRetryConfiguration Do(Action action)
    {
        return new ActionRetryConfiguration(action, 1, null);
    }

    public static IRetryConfiguration<TResult> Do<TResult>(Func<TResult> action)
    {
        return new FunctionRetryConfiguration<TResult>(action, 1, null, null);
    }
}

我認為這種方法比較容易出錯,而且更清潔。

此外,它還允許您執行以下操作:

int result = Retry.Do(SomeIntMethod).Interval(TimeSpan.FromSeconds(1)).Until(n => n > 20).Execute();

Retry.Do(SomeVoidMethod).Times(4).Execute();

微軟的Reactive Framework(NuGet“ Rx-Main”)具有開箱即用的所有操作程序。

嘗試這個:

IObservable<int> query =
    Observable
        .Defer(() =>
            Observable.Start(() => GetSomeValue()))
        .Where(x => x == 1)
        .Timeout(TimeSpan.FromSeconds(0.1))
        .Retry()
        .Take(1);

query
    .Subscribe(x =>
    {
        // Can only be a `1` if produced in less than 0.1 seconds
        Console.WriteLine(x);
    });

好吧,如果我正確理解了所有內容,那么類似的事情應該可以解決您的問題:

public static T Do<T>(Func<T> action, TimeSpan retryInterval, Predicate<T> predicate)
{
    var exceptions = new List<Exception>();
    try
    {
        bool succeeded;
        T result;
        do
        {
            result = action();
            succeeded = predicate(result);
        } while (!succeeded);

        return result;
    }
    catch (Exception ex)
    {
        exceptions.Add(ex);
    }
    throw new AggregateException(exceptions);
}

將此方法添加到您的重試類。

我已經使用示例ConsoleApplication嘗試了以下代碼:

class Program
{
    static void Main(string[] args)
    {
        var _random = new Random();

        Func<int> func = () =>
        {
            var result = _random.Next(10);
            Console.WriteLine(result);
            return result;
        };

        Retry.Do(func, TimeSpan.FromSeconds(1), i => i == 5);

        Console.ReadLine();
    }
}

實際上,它在隨機數為5時停止。

看來您想得太多了:

int returnValue = -1;
while (returnValue != 3)
{
    returnValue = DoStuff();
    // DoStuff should include a step to avoid maxing out cpu
}
return returnValue;

當然,“ 3”可能是您傳遞給函數的變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM