2

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?

JSteward
  • 6,833
  • 2
  • 21
  • 30
PICyourBrain
  • 9,976
  • 26
  • 91
  • 136

1 Answers1

2

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.

JSteward
  • 6,833
  • 2
  • 21
  • 30
  • Thanks JSteward. I didn't realize the ActionBlock was buffering things as well. What I actually want is for only 2 processes to be running in parallel but I don't want any additional jobs to be bufferable. So if I call SendAsync twice, I want the third call to wait until one of the first is finished. Is that possible? – PICyourBrain Apr 26 '17 at 15:40
  • 4
    Ok, if you drop the `BufferBlock` the single `ActionBlock` configured as above will have a capacity of 4. Two in processing and two buffered, only two jobs will ever be processing at a given time though. Unfortunately with blocks setting the `BoundedCapacity` to one still gives you a block with capacity of 2, one in processing and one in the buffer and that's the lowest you can go. – JSteward Apr 26 '17 at 15:45