Skip to content

Commit 387f4fb

Browse files
committed
Tweak that when the EnablePublishParallelSend option is true, it will be put task into the .NET thread pool in batches rather than all at once. (#1540)
1 parent 51453d5 commit 387f4fb

File tree

1 file changed

+42
-27
lines changed

1 file changed

+42
-27
lines changed

src/DotNetCore.CAP/Processor/IDispatcher.Default.cs

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ namespace DotNetCore.CAP.Processor;
1818

1919
public class Dispatcher : IDispatcher
2020
{
21-
private CancellationTokenSource? _tasksCts;
2221
private readonly CancellationTokenSource _delayCts = new();
2322
private readonly ISubscribeExecutor _executor;
2423
private readonly ILogger<Dispatcher> _logger;
@@ -28,16 +27,15 @@ public class Dispatcher : IDispatcher
2827
private readonly PriorityQueue<MediumMessage, long> _schedulerQueue;
2928
private readonly bool _enableParallelExecute;
3029
private readonly bool _enableParallelSend;
30+
private readonly int _pChannelSize;
3131

32+
private CancellationTokenSource? _tasksCts;
3233
private Channel<MediumMessage> _publishedChannel = default!;
3334
private Channel<(MediumMessage, ConsumerExecutorDescriptor?)> _receivedChannel = default!;
3435
private long _nextSendTime = DateTime.MaxValue.Ticks;
3536

36-
public Dispatcher(ILogger<Dispatcher> logger,
37-
IMessageSender sender,
38-
IOptions<CapOptions> options,
39-
ISubscribeExecutor executor,
40-
IDataStorage storage)
37+
public Dispatcher(ILogger<Dispatcher> logger, IMessageSender sender, IOptions<CapOptions> options,
38+
ISubscribeExecutor executor, IDataStorage storage)
4139
{
4240
_logger = logger;
4341
_sender = sender;
@@ -47,6 +45,7 @@ public Dispatcher(ILogger<Dispatcher> logger,
4745
_storage = storage;
4846
_enableParallelExecute = options.Value.EnableSubscriberParallelExecute;
4947
_enableParallelSend = options.Value.EnablePublishParallelSend;
48+
_pChannelSize = Environment.ProcessorCount * 500;
5049
}
5150

5251
public async Task Start(CancellationToken stoppingToken)
@@ -55,14 +54,13 @@ public async Task Start(CancellationToken stoppingToken)
5554
_tasksCts = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken, CancellationToken.None);
5655
_tasksCts.Token.Register(() => _delayCts.Cancel());
5756

58-
_publishedChannel = Channel.CreateBounded<MediumMessage>(
59-
new BoundedChannelOptions(5000)
60-
{
61-
AllowSynchronousContinuations = true,
62-
SingleReader = true,
63-
SingleWriter = true,
64-
FullMode = BoundedChannelFullMode.Wait
65-
});
57+
_publishedChannel = Channel.CreateBounded<MediumMessage>(new BoundedChannelOptions(_pChannelSize)
58+
{
59+
AllowSynchronousContinuations = true,
60+
SingleReader = true,
61+
SingleWriter = true,
62+
FullMode = BoundedChannelFullMode.Wait
63+
});
6664

6765
await Task.Run(Sending, _tasksCts.Token).ConfigureAwait(false); //here return valuetask
6866

@@ -207,28 +205,45 @@ private async ValueTask Sending()
207205
try
208206
{
209207
while (await _publishedChannel.Reader.WaitToReadAsync(_tasksCts!.Token).ConfigureAwait(false))
210-
while (_publishedChannel.Reader.TryRead(out var message))
211-
try
208+
{
209+
if (_enableParallelSend)
210+
{
211+
var tasks = new List<Task>();
212+
var batchSize = _pChannelSize / 50;
213+
for (var i = 0; i < batchSize && _publishedChannel.Reader.TryRead(out var message); i++)
212214
{
213215
var item = message;
214-
if (_enableParallelSend)
216+
tasks.Add(Task.Run(async () =>
215217
{
216-
_ = Task.Run(async () =>
218+
try
217219
{
218220
var result = await _sender.SendAsync(item).ConfigureAwait(false);
219221
if (!result.Succeeded) _logger.MessagePublishException(item.Origin.GetId(), result.ToString(), result.Exception);
220-
});
222+
}
223+
catch (Exception ex)
224+
{
225+
_logger.LogError(ex, $"An exception occurred when sending a message to the transport. Id:{message.DbId}");
226+
}
227+
}));
228+
}
229+
230+
await Task.WhenAll(tasks);
231+
}
232+
else
233+
{
234+
while (_publishedChannel.Reader.TryRead(out var message))
235+
try
236+
{
237+
var result = await _sender.SendAsync(message).ConfigureAwait(false);
238+
if (!result.Succeeded) _logger.MessagePublishException(message.Origin.GetId(), result.ToString(), result.Exception);
221239
}
222-
else
240+
catch (Exception ex)
223241
{
224-
var result = await _sender.SendAsync(item).ConfigureAwait(false);
225-
if (!result.Succeeded) _logger.MessagePublishException(item.Origin.GetId(), result.ToString(), result.Exception);
242+
_logger.LogError(ex, $"An exception occurred when sending a message to the transport. Id:{message.DbId}");
226243
}
227-
}
228-
catch (Exception ex)
229-
{
230-
_logger.LogError(ex, "An exception occurred when sending a message to the transport. Id:{MessageId}", message.DbId);
231-
}
244+
}
245+
}
246+
232247
}
233248
catch (OperationCanceledException)
234249
{

0 commit comments

Comments
 (0)