簡體   English   中英

c# 如何防止 output 在鏈接的 BufferBlock 的有界容量小於 output TPL 時丟棄 TransformManyBlock

[英]c# How to prevent output from a TransformManyBlock being discarded when the linked BufferBlock's bounded capacity is less than the output TPL

假設 TransformManyBlock 中的 output 是 1000 個項目,但鏈接的 BufferBlock 的有界容量是 500。

您將如何防止其他 500 個項目被丟棄,而是阻止 TransformManyBlocks function 直到 BufferBlock 中的容量可用。

var transformBlockOptions = new ExecutionDataflowBlockOptions
        {
            MaxDegreeOfParallelism = dataFlowBlockOptions.TransformBlockMaxDop,
            BoundedCapacity = dataFlowBlockOptions.TransformBlockBoundedCapacity,
            EnsureOrdered = dataFlowBlockOptions.EnsureOrdered
        };

        var transform = new TransformManyBlock<TIn, TOut>(transformBody, transformBlockOptions);

        var actionBlockOptions = new ExecutionDataflowBlockOptions
        {
            MaxDegreeOfParallelism = dataFlowBlockOptions.ActionBlockMaxDop,
            BoundedCapacity = dataFlowBlockOptions.ActionBlockBoundedCapicity
        };

        var action = new ActionBlock<TOut>(actionBody, actionBlockOptions);

        //Enables propegation of commands and exceptions to and from linked blocks;
        var dataflowLinkOptions = new DataflowLinkOptions { PropagateCompletion = false };

        var buffer = new BufferBlock<TOut>();

        //Only forward responses that aren't null to the ActionBlock
        if (dataFlowBlockOptions.SetNullPredicate)
        {
            transform.LinkTo(buffer, dataflowLinkOptions, p => p != null);

            //Null responses need to be consumed by something otherwise it causes a deadlock
            transform.LinkTo(DataflowBlock.NullTarget<TOut>());
        }
        else
        {
            transform.LinkTo(buffer, dataflowLinkOptions);
        }

        buffer.LinkTo(action, dataflowLinkOptions);

        foreach (var item in source)
        {
            await transform.SendAsync(item, dataFlowBlockOptions.CancellationToken);
        }

        transform.Complete();
        await transform.Completion;

        buffer.Complete();
        await buffer.Completion;

        action.Complete();
        await action.Completion;

沒關系。 我發現當達到 BufferBlocks 容量時,數據流入 NullTargetBlock 並且消息被丟棄。

暫無
暫無

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

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