BroadcastBlock with guaranteed delivery in TPL Dataflow

前端 未结 2 1234
后悔当初
后悔当初 2020-12-03 12:24

I have a stream of data that I process in several different ways... so I would like to send a copy of each message I get to multiple targets so that these targets may execut

2条回答
  •  时光取名叫无心
    2020-12-03 12:28

    It is fairly simple to build what you're asking using ActionBlock and SendAsync(), something like:

    public static ITargetBlock CreateGuaranteedBroadcastBlock(
        IEnumerable> targets)
    {
        var targetsList = targets.ToList();
    
        return new ActionBlock(
            async item =>
            {
                foreach (var target in targetsList)
                {
                    await target.SendAsync(item);
                }
            }, new ExecutionDataflowBlockOptions { BoundedCapacity = 1 });
    }
    

    This is the most basic version, but extending it to support mutable list of targets, propagating completion or cloning function should be easy.

提交回复
热议问题