-
-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed Batching and disabled AsyncDaemon for EVentStoreDB + Marten con…
…figuration
- Loading branch information
1 parent
2f51b6e
commit 3e59018
Showing
19 changed files
with
76 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,36 @@ | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading.Channels; | ||
using Open.ChannelExtensions; | ||
|
||
namespace Core.Extensions; | ||
|
||
public static class AsyncEnumerableExtensions | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
public static class AsyncEnumerableExtensions | ||
{ | ||
public static async IAsyncEnumerable<T[]> BatchAsync<T>( | ||
this IAsyncEnumerable<T> source, | ||
public static async Task Pipe<T, TResult>( | ||
this IAsyncEnumerable<T> enumerable, | ||
ChannelWriter<TResult> cw, | ||
Func<List<T>, TResult> transform, | ||
int batchSize, | ||
TimeSpan maxBatchTime, | ||
[EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
int timeout, | ||
CancellationToken ct | ||
) | ||
{ | ||
var batch = new List<T>(); | ||
var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken); | ||
|
||
try | ||
{ | ||
await foreach (var item in source.WithCancellation(cts.Token).ConfigureAwait(false)) | ||
var channel = Channel.CreateUnbounded<T>( | ||
new UnboundedChannelOptions | ||
{ | ||
batch.Add(item); | ||
if (batch.Count == 1) // Reset the timer when the first item is added | ||
{ | ||
cts.CancelAfter(maxBatchTime); // Set or reset the deadline | ||
} | ||
|
||
if (batch.Count >= batchSize) | ||
{ | ||
yield return batch.ToArray(); | ||
batch.Clear(); | ||
cts.CancelAfter(maxBatchTime); // Reset the deadline for the new batch | ||
} | ||
SingleWriter = false, SingleReader = true, AllowSynchronousContinuations = false | ||
} | ||
); | ||
|
||
if (batch.Count > 0) | ||
{ | ||
yield return batch.ToArray(); // Return any remaining items as a batch | ||
} | ||
} | ||
catch (OperationCanceledException) | ||
channel.Reader.Batch(batchSize).WithTimeout(timeout).PipeAsync(async batch => | ||
{ | ||
if (batch.Count > 0) | ||
{ | ||
yield return batch.ToArray(); // Yield whatever is in the batch when the timeout occurs | ||
} | ||
// Optionally, rethrow or handle the cancellation if needed | ||
} | ||
finally | ||
await cw.WriteAsync(transform(batch), ct).ConfigureAwait(false); | ||
return batch; | ||
}); | ||
|
||
await foreach (var @event in enumerable.WithCancellation(ct)) | ||
{ | ||
cts.Dispose(); // Ensure the CancellationTokenSource is disposed to free resources | ||
await channel.Writer.WriteAsync(@event, ct).ConfigureAwait(false); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters