Search code examples
c#task-parallel-librarytpl-dataflowbufferblock

Are items removed from BufferBlock when ActionBlock starts or ends?


Let's say I have a BufferBlock with a bounded capacity of 2 and I link it (using LinkTo()) an ActionBlock with MaxDegreeOfParallelism = 2. Now, I know that I will immediately be able to send 2 items to the buffer and the ActionBlock will begin processing them immediately. But let's say the actions take a few seconds to complete. Will I be able to send 2 more items into the buffer while those first to actions are running or is an item only removed from the buffer after the action that is consuming it completes?


Solution

  • If your ActionBlock has an unbound capacity the bounded capacity on your BufferBlock won't matter, the ActionBlock will buffer all of your items until you run out of memory. If, however, you've set the BoundedCapcity on your ActionBlock to 2 as well as MaxDegreeParallelism to 2 it will be processing two messages and holding 2 messages in it's buffer. Then your buffer block will buffer 2 additional messages. Any additional messages will need to wait for capacity in your pipeline. The best way to wait for space to free up is using await myPipeline.SendAsync(data). In total you would have a capacity of 6 for the entire pipeline.