简体   繁体   English

如何避免批处理块中的重置超时

[英]How to avoid reset timeout in batchblock

I'm using TPL DataFlow with BatchBlock which is triggered automatically when the number of currently queued or postponed items is less than the BatchSize after a timeout as given below:我将 TPL DataFlow 与 BatchBlock 一起使用,当当前排队或推迟的项目数在超时后小于 BatchSize 时自动触发,如下所示:

Timer triggerBatchTimer = new(_ => _batchBlock.TriggerBatch());

TransformBlock<string, string> timeoutTransformBlock = new((value) =>
{
    triggerBatchTimer.Change(_options.Value.TriggerBatchTime, Timeout.Infinite);
    return value;
});

var actionBlock = new ActionBlock<IEnumerable<string>>(action =>
{
    GenerateFile(action);
});

_buffer.LinkTo(timeoutTransformBlock);
timeoutTransformBlock.LinkTo(_batchBlock);
_batchBlock.LinkTo(actionBlock);

Ie with max batch size: 4 and timeout 10sec即最大批量大小:4 和超时 10 秒

Current behavior
BatchBlock (items): +---------1------------2------3---------------------------------|
Timeout (sec)     : +-10--9--10--9--8--7--10--9--10--9--8--7--6--5--4--3--2--1--0---|
ActionBlock       : +----------------------------------------------------------Call-|

Expected
BatchBlock (items): +--------1-----------2-----3---------|
Timeout (sec)     : +-10--9--8--7--6--5--4--3--2--1--0---|
ActionBlock       : +-------------------------------Call-|

But my problem is how to avoid timeout to be reset to 0 each time the block receive a new item?但我的问题是如何避免每次块收到新项目时超时都重置为 0?

Thanks to @Theodor Zoulias for help, this is solution:感谢@Theodor Zoulias 的帮助,这是解决方案:

Just initialize the timer with the same value for both dueTime and period and remove triggerBatchTimer.Change in TransformBlock .只需为dueTimeperiod使用相同的值初始化计时器,然后删除TransformBlock中的triggerBatchTimer.Change Thus batchblock fires every X seconds or when the batchSize has been reached and the timer is not reset for each new item in the batchblock.因此,batchblock 每 X 秒触发一次,或者在达到 batchSize 时触发,并且不会为 batchblock 中的每个新项目重置计时器。

Timer triggerBatchTimer = new(_ => _batchBlock.TriggerBatch(), null, options.Value.TriggerBatchTime, options.Value.TriggerBatchTime);

TransformBlock<string, string> timeoutTransformBlock = new((value) =>
{
    return value;
});

var actionBlock = new ActionBlock<IEnumerable<string>>(action =>
{
    GenerateFile(action);
});

_buffer.LinkTo(timeoutTransformBlock);
timeoutTransformBlock.LinkTo(_batchBlock);
_batchBlock.LinkTo(actionBlock);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM