Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does BoundedCapacity include items currently being processed in TPL Dataflow?

Does the BoundedCapacity limit only includes items in the input queue waiting to be processed or does it also count items being processed at the moment?

Lets take for example this ActionBlock:

var block = new ActionBlock<int>(
    i => Console.WriteLine(i),
    new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 1000,
        MaxDegreeOfParallelism = 10,
    });

If there are currently 5 items being processed in parallel. Does that mean the input queue can hold 1000 items more on top of these, or just 995?

like image 770
i3arnon Avatar asked Oct 27 '14 18:10

i3arnon


1 Answers

Evidently, BoundedCapacity indeed includes the items being processed on top of the items waiting in the input queue. This can be easily demonstrated with an ActionBlock that has the same ExecutionDataflowBlockOptions with an action that never finishes:

var block = new ActionBlock<int>(
    _ => Task.Delay(-1),
    new ExecutionDataflowBlockOptions
    {
        BoundedCapacity = 1000,
        MaxDegreeOfParallelism = 10,
    });

for (int i = 0; i < 1001; i++)
{
    Console.WriteLine("#{0} - InputCount={1}", i, block.InputCount);
    await block.SendAsync(i);
}

The output would be as follows and then the application would block indefinitely:

...
...
#990 - InputCount=980
#991 - InputCount=981
#992 - InputCount=982
#993 - InputCount=983
#994 - InputCount=984
#995 - InputCount=985
#996 - InputCount=986
#997 - InputCount=987
#998 - InputCount=988
#999 - InputCount=989
#1000 - InputCount=990

That's because 1000 items were added, 10 of them (MaxDegreeOfParallelism) are being processed concurrently, the other 990 are waiting in the input queue and the 1001st item could never get in.

like image 58
i3arnon Avatar answered Oct 11 '22 08:10

i3arnon