Implementing correct completion of a retryable block

后端 未结 2 1537
栀梦
栀梦 2021-02-01 01:15

Teaser: guys, this question is not about how to implement retry policy. It\'s about correct completion of a TPL Dataflow block.

This question is mostly

2条回答
  •  情深已故
    2021-02-01 01:36

    Combining hwcverwe answer and JamieSee comment could be the ideal solution.

    First, you need to create more than one event:

    var signal  = new ManualResetEvent(false);
    var completedEvent = new ManualResetEvent(false);
    

    Then, you have to create an observer, and subscribe to the TransformManyBlock, so you are notified when a relevant event happens:

    var observer = new RetryingBlockObserver(completedEvent);
    var observable = target.AsObservable();
    observable.Subscribe(observer);
    

    The observable can be quite easy:

    private class RetryingBlockObserver : IObserver {
            private ManualResetEvent completedEvent;
    
            public RetryingBlockObserver(ManualResetEvent completedEvent) {                
                this.completedEvent = completedEvent;
            }
    
            public void OnCompleted() {
                completedEvent.Set();
            }
    
            public void OnError(Exception error) {
                //TODO
            }
    
            public void OnNext(T value) {
                //TODO
            }
        }
    

    And you can wait for either the signal, or completion (exhaustion of all the source items), or both

     source.Completion.ContinueWith(async _ => {
    
                WaitHandle.WaitAll(completedEvent, signal);
                // Or WaitHandle.WaitAny, depending on your needs!
    
                target.Complete();
            });
    

    You can inspect the result value of WaitAll to understand which event was set, and react accordingly. You can also add other events to the code, passing them to the observer, so that it can set them when needed. You can differentiate your behaviour and respond differently when an error is raised, for example

提交回复
热议问题