Skip to content

Commit

Permalink
refactor: use ConfigureAwait(false) + added inspection settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ArneD committed Feb 7, 2024
1 parent af2027f commit bb81de7
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UseConfigureAwaitFalse/@EntryIndexedValue">ERROR</s:String>
<s:Boolean x:Key="/Default/CodeStyle/CSharpUsing/AddImportsToDeepestScope/@EntryValue">True</s:Boolean>
<s:String x:Key="/Default/CodeStyle/Naming/JavaScriptNaming/UserRules/=TS_005FMIXED_005FENUM/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/JavaScriptNaming/UserRules/=TS_005FTYPE_005FALIAS/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static ConnectedProjectionTestSpecification<TContext> Expect<TContext, TE
{
return scenario.Verify(async ctx =>
{
var actualEntities = await query(ctx).ToListAsync();
var actualEntities = await query(ctx).ToListAsync().ConfigureAwait(false);
if (actualEntities.Count != expectedEntities.Length)
return VerificationResult.Fail(
$" Expected {expectedEntities.Length} entities, but found {actualEntities.Count} entities. {actualEntities.ToLogStringLimited(max: 10)}");
Expand Down Expand Up @@ -66,7 +66,7 @@ public static ConnectedProjectionTestSpecification<TContext> ExpectNone<TContext
{
return scenario.Verify(async ctx =>
{
var actualEntities = await query(ctx).ToListAsync();
var actualEntities = await query(ctx).ToListAsync().ConfigureAwait(false);
if (actualEntities.Count > 0)
return VerificationResult.Fail(
$" Expected none, but found {actualEntities.Count} entities. {actualEntities.ToLogStringLimited(max: 10)}");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ private void CreateSubscription(IReadonlyStreamStore streamStore, Func<Owned<TCo
if (_envelopeFactory.TryCreate(message, out var envelope))
{
await using var context = contextFactory().Value;
await context.UpdateProjectionState(RunnerName, message.Position, cancellationToken);
await context.UpdateProjectionState(RunnerName, message.Position, cancellationToken).ConfigureAwait(false);

await _projector.ProjectAsync(context, envelope, cancellationToken);
await _projector.ProjectAsync(context, envelope, cancellationToken).ConfigureAwait(false);

await context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -117,7 +117,7 @@ private void CreateSubscription(IReadonlyStreamStore streamStore, Func<Owned<TCo
{
_logger.LogWarning(exception, "Subscription {SubscriptionName} was dropped. Reason: {Reason}", subscription.Name, reason);

await Task.Delay(TimeSpan.FromSeconds(5));
await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);

// When Handle throws, we will no longer be subscribed (and no subsequent subscribe will be attempted).
Handle(streamStore, contextFactory);
Expand All @@ -130,7 +130,7 @@ public async Task StartAsync(IReadonlyStreamStore streamStore, Func<Owned<TConte
{
cancellationToken.Register(CleanUp);

await StartAsyncInternal(streamStore, contextFactory, cancellationToken);
await StartAsyncInternal(streamStore, contextFactory, cancellationToken).ConfigureAwait(false);
}

private async Task StartAsyncInternal(
Expand All @@ -146,13 +146,13 @@ private async Task StartAsyncInternal(
{
var dbPosition = await context
.ProjectionStates
.SingleOrDefaultAsync(p => p.Name == RunnerName, cancellationToken);
.SingleOrDefaultAsync(p => p.Name == RunnerName, cancellationToken).ConfigureAwait(false);

position = dbPosition?.Position;
}

// discover head position
var head = await streamStore.ReadHeadPosition(cancellationToken);
var head = await streamStore.ReadHeadPosition(cancellationToken).ConfigureAwait(false);

// determine whether to play catch up or start subscribing
var shouldCatchUp = position.HasValue
Expand Down Expand Up @@ -181,11 +181,11 @@ private async Task StartAsyncInternal(
if (_envelopeFactory.TryCreate(message, out var envelope))
{
await using var context = contextFactory().Value;
await context.UpdateProjectionState(RunnerName, message.Position, cancellationToken);
await context.UpdateProjectionState(RunnerName, message.Position, cancellationToken).ConfigureAwait(false);

await _projector.ProjectAsync(context, envelope, cancellationToken);
await _projector.ProjectAsync(context, envelope, cancellationToken).ConfigureAwait(false);

await context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -201,9 +201,9 @@ private async Task StartAsyncInternal(
{
_logger.LogWarning(exception, "Subscription {SubscriptionName} was dropped. Reason: {Reason}", subscription.Name, reason);

await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);

await StartAsyncInternal(streamStore, contextFactory, cancellationToken);
await StartAsyncInternal(streamStore, contextFactory, cancellationToken).ConfigureAwait(false);
}));
}
}
Expand All @@ -220,7 +220,7 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo

var fromPositionInclusive = position ?? global::SqlStreamStore.Streams.Position.Start;

var page = await streamStore.ReadAllForwards(fromPositionInclusive, CatchupPageSize, true, cancellationToken);
var page = await streamStore.ReadAllForwards(fromPositionInclusive, CatchupPageSize, true, cancellationToken).ConfigureAwait(false);

_logger.LogDebug(
"Processing page of {PageSize} starting at POS {FromPosition}",
Expand All @@ -246,7 +246,7 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo

if (_envelopeFactory.TryCreate(message, out var envelope))
{
await _projector.ProjectAsync(context, envelope, cancellationToken);
await _projector.ProjectAsync(context, envelope, cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -261,15 +261,15 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo

if (positionOfLastMessageOnPage.HasValue)
{
await context.UpdateProjectionState(RunnerName, positionOfLastMessageOnPage.Value, cancellationToken);
await context.UpdateProjectionState(RunnerName, positionOfLastMessageOnPage.Value, cancellationToken).ConfigureAwait(false);
}

await context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}

while (!page.IsEnd)
{
page = await page.ReadNext(cancellationToken);
page = await page.ReadNext(cancellationToken).ConfigureAwait(false);
_logger.LogDebug(
"Processing page of {PageSize} starting at POS {FromPosition}",
page.Messages.Length,
Expand All @@ -289,7 +289,7 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo

if (_envelopeFactory.TryCreate(message, out var envelope))
{
await _projector.ProjectAsync(context, envelope, cancellationToken);
await _projector.ProjectAsync(context, envelope, cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -304,13 +304,13 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo

if (positionOfLastMessageOnPage.HasValue)
{
await context.UpdateProjectionState(RunnerName, positionOfLastMessageOnPage.Value, cancellationToken);
await context.UpdateProjectionState(RunnerName, positionOfLastMessageOnPage.Value, cancellationToken).ConfigureAwait(false);
}

await context.SaveChangesAsync(cancellationToken);
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}

await StartAsyncInternal(streamStore, contextFactory, cancellationToken); // subscribe when done catching up
await StartAsyncInternal(streamStore, contextFactory, cancellationToken).ConfigureAwait(false); // subscribe when done catching up
}
catch (Exception exception)
{
Expand All @@ -320,9 +320,9 @@ private async Task CatchUpAsync(IReadonlyStreamStore streamStore, Func<Owned<TCo
RunnerName,
positionOfLastMessageOnPage ?? -1L);

await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken).ConfigureAwait(false);

await StartAsyncInternal(streamStore, contextFactory, cancellationToken);
await StartAsyncInternal(streamStore, contextFactory, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,20 @@ public virtual async Task UpdateProjectionState(string? projectionName, long pos
return;
}

var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken);
var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken).ConfigureAwait(false);

if (projectionStateItem == null)
{
projectionStateItem = new ProjectionStateItem { Name = projectionName };
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken);
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken).ConfigureAwait(false);
}

projectionStateItem.Position = position;
}

public virtual async Task SetErrorMessage(string projectionName, string? errorMessage, CancellationToken cancellationToken)
{
var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken);
var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken).ConfigureAwait(false);

if (projectionStateItem == null)
{
Expand All @@ -70,15 +70,15 @@ public virtual async Task SetErrorMessage(string projectionName, string? errorMe
Name = projectionName,
Position = -1L
};
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken);
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken).ConfigureAwait(false);
}

projectionStateItem.ErrorMessage = errorMessage;
}

public virtual async Task UpdateProjectionDesiredState(string projectionName, string desiredState, CancellationToken cancellationToken)
{
var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken);
var projectionStateItem = await ProjectionStates.SingleOrDefaultAsync(item => item.Name == projectionName, cancellationToken).ConfigureAwait(false);

if (projectionStateItem == null)
{
Expand All @@ -87,7 +87,7 @@ public virtual async Task UpdateProjectionDesiredState(string projectionName, st
Name = projectionName,
Position = -1L
};
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken);
await ProjectionStates.AddAsync(projectionStateItem, cancellationToken).ConfigureAwait(false);
}

projectionStateItem.DesiredState = desiredState;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/Daemon/ConfigureAwaitAnalysisMode/@EntryValue">Library</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class FeedProjectionRunner<TMessage, TContent, TContext> : IFeedProjectio
private readonly IRegistryAtomFeedReader _atomFeedReader;
private readonly DataContractSerializer _dataContractSerializer;
private readonly AtomEntryProjectionHandlerResolver<TMessage, TContext> _atomEntryProjectionHandlerResolver;

public string RunnerName { get; }
public Uri FeedUri { get; }
public string FeedUserName { get; }
Expand Down Expand Up @@ -73,15 +73,15 @@ public async Task CatchUpAsync(
{
var dbPosition = await context
.ProjectionStates
.SingleOrDefaultAsync(p => p.Name == RunnerName, cancellationToken);
.SingleOrDefaultAsync(p => p.Name == RunnerName, cancellationToken).ConfigureAwait(false);

position = dbPosition?.Position + 1;
}

while (!cancellationToken.IsCancellationRequested)
{
// Read new events
var entries = (await _atomFeedReader.ReadEntriesAsync(FeedUri, position, FeedUserName, FeedPassword, EmbedEvent, EmbedObject)).ToList();
var entries = (await _atomFeedReader.ReadEntriesAsync(FeedUri, position, FeedUserName, FeedPassword, EmbedEvent, EmbedObject).ConfigureAwait(false)).ToList();

while (entries.Any())
{
Expand All @@ -92,14 +92,14 @@ public async Task CatchUpAsync(

await using (var context = contextFactory().Value)
{
await ProjectAtomEntriesAsync(entries, context, cancellationToken);
await ProjectAtomEntriesAsync(entries, context, cancellationToken).ConfigureAwait(false);

await context.UpdateProjectionState(RunnerName, lastEntryId, cancellationToken);
await context.SaveChangesAsync(cancellationToken);
await context.UpdateProjectionState(RunnerName, lastEntryId, cancellationToken).ConfigureAwait(false);
await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
}

position = lastEntryId + 1;
entries = (await _atomFeedReader.ReadEntriesAsync(FeedUri, position, FeedUserName, FeedPassword, EmbedEvent, EmbedObject)).ToList();
entries = (await _atomFeedReader.ReadEntriesAsync(FeedUri, position, FeedUserName, FeedPassword, EmbedEvent, EmbedObject).ConfigureAwait(false)).ToList();
}

Thread.Sleep(_pollingInMilliseconds);
Expand Down Expand Up @@ -128,7 +128,7 @@ private async Task ProjectAtomEntriesAsync(
{
await resolvedProjectionHandler
.Handler
.Invoke(atomEntry, context, cancellationToken);
.Invoke(atomEntry, context, cancellationToken).ConfigureAwait(false);
}
}
}
Expand Down

0 comments on commit bb81de7

Please sign in to comment.