Skip to content

Commit bd72227

Browse files
SignalR MessageBuffer fixes (#50315)
* SignalR MessageBuffer fixes * pool * version * Apply suggestions from code review * fb
1 parent 0f999d4 commit bd72227

File tree

13 files changed

+370
-89
lines changed

13 files changed

+370
-89
lines changed

eng/Dependencies.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and are generated based on the last package release.
1717
<LatestPackageReference Include="Microsoft.AspNetCore.Mvc.Razor.Extensions" />
1818
<LatestPackageReference Include="Microsoft.Azure.SignalR" />
1919
<LatestPackageReference Include="Microsoft.Bcl.HashCode" />
20+
<LatestPackageReference Include="Microsoft.Bcl.TimeProvider" />
2021
<LatestPackageReference Include="Microsoft.Css.Parser" />
2122
<LatestPackageReference Include="Microsoft.CodeAnalysis.Common" />
2223
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
@@ -63,6 +64,7 @@ and are generated based on the last package release.
6364
<LatestPackageReference Include="Microsoft.Extensions.Options" />
6465
<LatestPackageReference Include="Microsoft.Extensions.Primitives" />
6566
<LatestPackageReference Include="Microsoft.Extensions.Telemetry.Testing" />
67+
<LatestPackageReference Include="Microsoft.Extensions.TimeProvider.Testing" />
6668
<LatestPackageReference Include="Microsoft.Win32.Registry" />
6769
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" />
6870
<LatestPackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" />

eng/Version.Details.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,10 @@
397397
<Uri>https://github.com/dotnet/extensions</Uri>
398398
<Sha>7c6fa3e84ea0b3d08998726c7cac30e3117ed318</Sha>
399399
</Dependency>
400+
<Dependency Name="Microsoft.Extensions.TimeProvider.Testing" Version="8.0.0-rc.2.23461.3">
401+
<Uri>https://github.com/dotnet/extensions</Uri>
402+
<Sha>7c6fa3e84ea0b3d08998726c7cac30e3117ed318</Sha>
403+
</Dependency>
400404
<Dependency Name="NuGet.Frameworks" Version="6.2.4">
401405
<Uri>https://github.com/nuget/nuget.client</Uri>
402406
<Sha>8fef55f5a55a3b4f2c96cd1a9b5ddc51d4b927f8</Sha>

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138
<SystemRuntimeCachingVersion>8.0.0-rc.2.23457.7</SystemRuntimeCachingVersion>
139139
<!-- Packages from dotnet/extensions -->
140140
<MicrosoftExtensionsTelemetryTestingVersion>8.0.0-rc.2.23461.3</MicrosoftExtensionsTelemetryTestingVersion>
141+
<MicrosoftExtensionsTimeProviderTestingVersion>8.0.0-rc.2.23461.3</MicrosoftExtensionsTimeProviderTestingVersion>
141142
<!-- Packages from dotnet/efcore -->
142143
<dotnetefVersion>8.0.0-rc.2.23462.14</dotnetefVersion>
143144
<MicrosoftEntityFrameworkCoreInMemoryVersion>8.0.0-rc.2.23462.14</MicrosoftEntityFrameworkCoreInMemoryVersion>

src/SignalR/clients/csharp/Client.Core/src/HubConnection.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ private async Task SendHubMessage(ConnectionState connectionState, HubMessage hu
10061006

10071007
if (connectionState.UsingAcks())
10081008
{
1009-
await connectionState.WriteAsync(new SerializedHubMessage(hubMessage), cancellationToken).ConfigureAwait(false);
1009+
await connectionState.WriteAsync(hubMessage, cancellationToken).ConfigureAwait(false);
10101010
}
10111011
else
10121012
{
@@ -1133,7 +1133,7 @@ private async Task SendWithLock(ConnectionState expectedConnectionState, HubMess
11331133
break;
11341134
case AckMessage ackMessage:
11351135
Log.ReceivedAckMessage(_logger, ackMessage.SequenceId);
1136-
connectionState.Ack(ackMessage);
1136+
await connectionState.AckAsync(ackMessage).ConfigureAwait(false);
11371137
break;
11381138
case SequenceMessage sequenceMessage:
11391139
Log.ReceivedSequenceMessage(_logger, sequenceMessage.SequenceId);
@@ -1943,7 +1943,7 @@ public ConnectionState(ConnectionContext connection, HubConnection hubConnection
19431943
{
19441944
_messageBuffer = new MessageBuffer(connection, hubConnection._protocol,
19451945
_hubConnection._serviceProvider.GetService<IOptions<HubConnectionOptions>>()?.Value.StatefulReconnectBufferSize
1946-
?? DefaultStatefulReconnectBufferSize);
1946+
?? DefaultStatefulReconnectBufferSize, _logger);
19471947

19481948
feature.OnReconnected(_messageBuffer.ResendAsync);
19491949
}
@@ -2071,7 +2071,7 @@ public async Task TimerLoop(TimerAwaitable timer)
20712071
}
20722072
}
20732073

2074-
public ValueTask<FlushResult> WriteAsync(SerializedHubMessage message, CancellationToken cancellationToken)
2074+
public ValueTask<FlushResult> WriteAsync(HubMessage message, CancellationToken cancellationToken)
20752075
{
20762076
Debug.Assert(_messageBuffer is not null);
20772077
return _messageBuffer.WriteAsync(message, cancellationToken);
@@ -2090,12 +2090,14 @@ public bool ShouldProcessMessage(HubMessage message)
20902090
return true;
20912091
}
20922092

2093-
public void Ack(AckMessage ackMessage)
2093+
public Task AckAsync(AckMessage ackMessage)
20942094
{
20952095
if (UsingAcks())
20962096
{
2097-
_messageBuffer.Ack(ackMessage);
2097+
return _messageBuffer.AckAsync(ackMessage);
20982098
}
2099+
2100+
return Task.CompletedTask;
20992101
}
21002102

21012103
public void ResetSequence(SequenceMessage sequenceMessage)

src/SignalR/clients/csharp/Client.Core/src/Microsoft.AspNetCore.SignalR.Client.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
<Reference Include="System.Threading.Channels" />
3535
</ItemGroup>
3636

37+
<ItemGroup Condition="'$(TargetFramework)' != '$(DefaultNetCoreTargetFramework)'">
38+
<Reference Include="Microsoft.Bcl.TimeProvider" />
39+
</ItemGroup>
40+
3741
<ItemGroup>
3842
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.FunctionalTests" />
3943
<InternalsVisibleTo Include="Microsoft.AspNetCore.SignalR.Client.Tests" />

src/SignalR/clients/csharp/Http.Connections.Client/src/Internal/WebSocketsTransport.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ private async Task StartSending(WebSocket socket, bool ignoreFirstCanceled)
616616

617617
if (_gracefulClose || !_useStatefulReconnect)
618618
{
619-
_application.Input.Complete(error);
619+
_application.Input.Complete();
620620
}
621621
else
622622
{

src/SignalR/common/Http.Connections/src/Internal/HttpConnectionDispatcher.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,9 @@ private async Task ExecuteAsync(HttpContext context, ConnectionDelegate connecti
295295
{
296296
// If false then the transport was ungracefully closed, this can mean a temporary network disconnection
297297
// We'll mark the connection as inactive and allow the connection to reconnect if that's the case.
298-
// TODO: If acks aren't enabled we can close the connection immediately (not LongPolling)
299-
if (await connection.TransportTask!)
298+
if (await connection.TransportTask!
299+
// If acks aren't enabled we can close the connection immediately (not LongPolling)
300+
|| !connection.ClientReconnectExpected())
300301
{
301302
await _manager.DisposeAndRemoveAsync(connection, closeGracefully: true, HttpConnectionStopStatus.NormalClosure);
302303
}

src/SignalR/common/Http.Connections/src/Internal/Transports/WebSocketsServerTransport.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,10 @@ private async Task StartSending(WebSocket socket, bool ignoreFirstCancel)
288288

289289
if (_gracefulClose)
290290
{
291-
_application.Input.Complete(error);
291+
_application.Input.Complete();
292292
}
293-
else if (error is not null)
293+
294+
if (error is not null)
294295
{
295296
Log.SendErrored(_logger, error);
296297
}

0 commit comments

Comments
 (0)