Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SignalR performance: track groups per connection, remove on disconnect #53486

Merged
merged 10 commits into from
Feb 7, 2024
23 changes: 6 additions & 17 deletions src/SignalR/server/Core/src/DefaultHubLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Concurrent;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.SignalR.Internal;
using Microsoft.AspNetCore.SignalR.Protocol;
using Microsoft.Extensions.Logging;
Expand Down Expand Up @@ -44,10 +43,9 @@ public override Task AddToGroupAsync(string connectionId, string groupName, Canc
}

// Track groups in the connection object
var groupNames = connection.Features.GetRequiredFeature<GroupTrackerFeature>().Groups;
lock (groupNames)
lock (connection.GroupNames)
{
if (!groupNames.Add(groupName))
if (!connection.GroupNames.Add(groupName))
{
// Connection already in group
return Task.CompletedTask;
Expand Down Expand Up @@ -77,10 +75,9 @@ public override Task RemoveFromGroupAsync(string connectionId, string groupName,
}

// Remove from previously saved groups
var groupNames = connection.Features.GetRequiredFeature<GroupTrackerFeature>().Groups;
lock (groupNames)
lock (connection.GroupNames)
{
if (!groupNames.Remove(groupName))
if (!connection.GroupNames.Remove(groupName))
{
// Connection not in group
return Task.CompletedTask;
Expand Down Expand Up @@ -294,19 +291,16 @@ public override Task SendUserAsync(string userId, string methodName, object?[] a
public override Task OnConnectedAsync(HubConnectionContext connection)
{
_connections.Add(connection);
// Add a group tracker to every connection
connection.Features.Set(new GroupTrackerFeature());
return Task.CompletedTask;
}

/// <inheritdoc />
public override Task OnDisconnectedAsync(HubConnectionContext connection)
{
var groupNames = connection.Features.GetRequiredFeature<GroupTrackerFeature>().Groups;
lock (groupNames)
lock (connection.GroupNames)
{
// Remove from tracked groups one by one
foreach (var groupName in groupNames)
foreach (var groupName in connection.GroupNames)
{
_groups.Remove(connection.ConnectionId, groupName);
}
Expand Down Expand Up @@ -385,11 +379,6 @@ public override async Task<T> InvokeConnectionAsync<T>(string connectionId, stri
}
}

private sealed class GroupTrackerFeature
{
public HashSet<string> Groups { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}

/// <inheritdoc/>
public override Task SetConnectionResultAsync(string connectionId, CompletionMessage result)
{
Expand Down
4 changes: 4 additions & 0 deletions src/SignalR/server/Core/src/HubConnectionContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics.CodeAnalysis;
using System.IO.Pipelines;
using System.Security.Claims;
using System.Text.RegularExpressions;
alex-jitbit marked this conversation as resolved.
Show resolved Hide resolved
using Microsoft.AspNetCore.Connections;
using Microsoft.AspNetCore.Connections.Abstractions;
using Microsoft.AspNetCore.Connections.Features;
Expand Down Expand Up @@ -56,6 +57,9 @@ public partial class HubConnectionContext
[MemberNotNullWhen(true, nameof(_messageBuffer))]
internal bool UsingStatefulReconnect() => _useStatefulReconnect;

// Tracks groups that the connection has been added to
internal HashSet<string> GroupNames { get; } = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
alex-jitbit marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// Initializes a new instance of the <see cref="HubConnectionContext"/> class.
/// </summary>
Expand Down