Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ protected async IAsyncEnumerable<T> TraceEnumerable<T>(

var enumerator = source.GetAsyncEnumerator(cancellationToken);

await using (enumerator.ConfigureAwait(false)) {
await using (enumerator.NoContext()) {
while (true) {
bool moved;

try {
moved = await enumerator.MoveNextAsync().ConfigureAwait(false);
moved = await enumerator.MoveNextAsync().NoContext();
} catch (Exception e) {
activity?.SetActivityStatus(ActivityStatus.Error(e));
measure.SetError();
Expand Down
30 changes: 16 additions & 14 deletions src/Core/src/Eventuous.Persistence/EventStore/StoreFunctions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ CancellationToken cancellationToken
try {
var result = new List<StreamEvent>();

await foreach (var evt in eventReader.ReadEventsBackwards(stream, start, count, cancellationToken).ConfigureAwait(false)) {
await foreach (var evt in eventReader.ReadEventsBackwards(stream, start, count, cancellationToken).NoContext(cancellationToken)) {
result.Add(evt);
}

Expand Down Expand Up @@ -218,24 +218,26 @@ [EnumeratorCancellation] CancellationToken cancellationToken
var yielded = 0;
long lastRevision = 0;

await using var enumerator = eventReader.ReadEvents(streamName, position, pageSize, cancellationToken).GetAsyncEnumerator(cancellationToken);
var enumerator = eventReader.ReadEvents(streamName, position, pageSize, cancellationToken).GetAsyncEnumerator(cancellationToken);

while (true) {
bool moved;
await using (enumerator.NoContext()) {
while (true) {
bool moved;

try {
moved = await enumerator.MoveNextAsync().NoContext();
} catch (StreamNotFound) when (!failIfNotFound) {
yield break;
}
try {
moved = await enumerator.MoveNextAsync().NoContext();
} catch (StreamNotFound) when (!failIfNotFound) {
yield break;
}

if (!moved) break;
if (!moved) break;

var evt = enumerator.Current;
yielded++;
lastRevision = evt.Revision;
var evt = enumerator.Current;
yielded++;
lastRevision = evt.Revision;

yield return evt;
yield return evt;
}
}

if (yielded < pageSize) yield break;
Expand Down
3 changes: 3 additions & 0 deletions src/Core/src/Eventuous.Shared/Tools/TaskExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ static class TaskExtensions {
public static ConfiguredCancelableAsyncEnumerable<T> NoContext<T>(this IAsyncEnumerable<T> source, CancellationToken cancellationToken)
=> source.WithCancellation(cancellationToken).ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static ConfiguredAsyncDisposable NoContext(this IAsyncDisposable disposable) => disposable.ConfigureAwait(false);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Task WhenAll(this IEnumerable<Task> tasks) => Task.WhenAll(tasks);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ [EnumeratorCancellation] CancellationToken cancellationToken

// Propagate possible failure of the channel.
if (source.Completion.IsCompleted)
await source.Completion.ConfigureAwait(false);
await source.Completion.NoContext();
} finally { timerCts.Dispose(); }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ protected void On<T>(ProjectToSqlServerAsync<T> handler) where T : class
=> base.On<T>(async ctx => await Handle(ctx, handler).NoContext());

async Task Handle<T>(MessageConsumeContext<T> context, ProjectToSqlServerAsync<T> handler) where T : class {
await using var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken);
var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken).NoContext();

var cmd = await handler(connection, context).ConfigureAwait(false);
await cmd.ExecuteNonQueryAsync(context.CancellationToken).ConfigureAwait(false);
await using (connection.NoContext()) {
var cmd = await handler(connection, context).NoContext();
await cmd.ExecuteNonQueryAsync(context.CancellationToken).NoContext();
}
}

protected static SqlCommand Project(SqlConnection connection, string commandText, params SqlParameter[] parameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ protected void On<T>(ProjectToSqliteAsync<T> handler) where T : class
=> base.On<T>(async ctx => await Handle(ctx, handler).NoContext());

async Task Handle<T>(MessageConsumeContext<T> context, ProjectToSqliteAsync<T> handler) where T : class {
await using var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken);
var connection = await ConnectionFactory.GetConnection(_connectionString, context.CancellationToken).NoContext();

var cmd = await handler(connection, context).ConfigureAwait(false);
await cmd.ExecuteNonQueryAsync(context.CancellationToken).ConfigureAwait(false);
await using (connection.NoContext()) {
var cmd = await handler(connection, context).NoContext();
await cmd.ExecuteNonQueryAsync(context.CancellationToken).NoContext();
}
}

protected static SqliteCommand Project(SqliteConnection connection, string commandText, params SqliteParameter[] parameters) {
Expand Down
Loading