Skip to content

Commit

Permalink
Merge pull request #18 from LiamMorrow/expose-message-sending
Browse files Browse the repository at this point in the history
Expose an IActorProviderFactory
  • Loading branch information
LiamMorrow authored May 15, 2022
2 parents 92fb01c + 4a1d0f6 commit 01158c0
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 122 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
dotnet-version: 6.0.300
- name: Build SignalR Lib
run: dotnet build --configuration Release ./src/OrgnalR.SignalR
- name: Build Orleans Lib
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
dotnet-version: 6.0.300
- name: Pack Backplane
run: dotnet pack --configuration Release ./src/OrgnalR.Backplane
- name: Pack GrainAdaptors
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.201
dotnet-version: 6.0.300
- name: Run tests
run: dotnet test ./test/OrgnalR.Tests /p:CollectCoverage=true
10 changes: 5 additions & 5 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project>
<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;netstandard2.0</TargetFrameworks>
<Version>1.4.1</Version>
<TargetFrameworks>net6.0;netstandard2.0</TargetFrameworks>
<Version>1.5.0</Version>
<Authors>Liam Morrow</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSource>true</IncludeSource>
<RepositoryUrl>https://github.com/LiamMorrow/OrgnalR</RepositoryUrl>
<PackageReleaseNotes>
Multi target package to alleviate System.Text.Encoding conflict between netstandard2.0 and netcoreapp3.1 default service fabric project.
https://github.com/LiamMorrow/OrgnalR/issues/13</PackageReleaseNotes>
Add the ability to send messages to clients from outside of the hub, in the silo (like the IHubContext interface)
</PackageReleaseNotes>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>8.0</LangVersion>
<LangVersion>10.0</LangVersion>
<Nullable>enable</Nullable>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
</PropertyGroup>
Expand Down
27 changes: 27 additions & 0 deletions src/OrgnalR.Backplane.GrainAdaptors/GrainActorProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Globalization;
using OrgnalR.Core.Provider;
using OrgnalR.Core.State;
using Orleans;

namespace OrgnalR.Backplane.GrainAdaptors;


public class GrainActorProviderFactory : IActorProviderFactory
{
private readonly IGrainFactory grainFactory;

public GrainActorProviderFactory(IGrainFactory grainFactory)
{
this.grainFactory = grainFactory;
}

public IGroupActor GetGroupActor(string hubName, string groupName)
{
return new GrainActorProvider(hubName, grainFactory).GetGroupActor(groupName);
}

public IUserActor GetUserActor(string hubName, string userId)
{
return new GrainActorProvider(hubName, grainFactory).GetUserActor(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
<ProjectReference Include="..\OrgnalR.Core\OrgnalR.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
<ProjectReference Include="..\OrgnalR.Core\OrgnalR.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<ProjectReference Include="..\OrgnalR.Core\OrgnalR.Core.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="3.6.2" />
</ItemGroup>
</Project>
33 changes: 15 additions & 18 deletions src/OrgnalR.Backplane/OrgnalRHubLifetimeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,23 @@ public class OrgnalRHubLifetimeManager<THub> : HubLifetimeManager<THub>, IDispos
private const string CONNECTION_LATEST_MESSAGE_KEY = "ORGNALR_LatestClientMessageHandle";
private bool disposed;
private readonly HubConnectionStore hubConnectionStore = new HubConnectionStore();
private readonly IGroupActorProvider groupActorProvider;
private readonly IUserActorProvider userActorProvider;
private readonly IActorProviderFactory actorProviderFactory;
private readonly IMessageObservable messageObservable;
private readonly IMessageObserver messageObserver;
private readonly ILogger<OrgnalRHubLifetimeManager<THub>> logger;
private readonly string hubName = typeof(THub).Name;
private SubscriptionHandle? allSubscriptionHandle;

private MessageHandle latestAllMessageHandle;

private OrgnalRHubLifetimeManager(
IGroupActorProvider groupActorProvider,
IUserActorProvider userActorProvider,
IActorProviderFactory actorProviderFactory,
IMessageObservable messageObservable,
IMessageObserver messageObserver,
ILogger<OrgnalRHubLifetimeManager<THub>> logger
)
{
this.groupActorProvider = groupActorProvider ?? throw new ArgumentNullException(nameof(groupActorProvider));
this.userActorProvider = userActorProvider ?? throw new ArgumentNullException(nameof(userActorProvider));
this.actorProviderFactory = actorProviderFactory ?? throw new ArgumentNullException(nameof(actorProviderFactory));
this.messageObservable = messageObservable ?? throw new ArgumentNullException(nameof(messageObservable));
this.messageObserver = messageObserver ?? throw new ArgumentNullException(nameof(messageObserver));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
Expand All @@ -54,15 +52,14 @@ ILogger<OrgnalRHubLifetimeManager<THub>> logger
/// <param name="cancellationToken"></param>
/// <returns>A new instance of <see cref="OrgnalRHubLifetimeManager<THub>" /> that is subscribed to the anonymous message broadcasts</returns>
public static async Task<OrgnalRHubLifetimeManager<THub>> CreateAsync(
IGroupActorProvider groupActorProvider,
IUserActorProvider userActorProvider,
IActorProviderFactory actorProviderFactory,
IMessageObservable messageObservable,
IMessageObserver messageObserver,
ILogger<OrgnalRHubLifetimeManager<THub>> logger,
CancellationToken cancellationToken = default
)
{
var manager = new OrgnalRHubLifetimeManager<THub>(groupActorProvider, userActorProvider, messageObservable, messageObserver, logger);
var manager = new OrgnalRHubLifetimeManager<THub>(actorProviderFactory, messageObservable, messageObserver, logger);
manager.allSubscriptionHandle = await messageObservable.SubscribeToAllAsync(manager.OnAnonymousMessageReceived, manager.OnAnonymousSubscriptionEnd, default, cancellationToken).ConfigureAwait(false);
return manager;
}
Expand All @@ -73,7 +70,7 @@ public override async Task OnConnectedAsync(HubConnectionContext connection)

if (connection.UserIdentifier != null)
{
await userActorProvider.GetUserActor(connection.UserIdentifier)
await actorProviderFactory.GetUserActor(hubName, connection.UserIdentifier)
.AddToUserAsync(connection.ConnectionId);
}
try
Expand All @@ -82,7 +79,7 @@ await userActorProvider.GetUserActor(connection.UserIdentifier)
}
catch (ArgumentOutOfRangeException e)
{
logger.LogWarning(e, "Unable to replay client messages since last connect for client {0}", connection.ConnectionId);
logger.LogWarning(e, "Unable to replay client messages since last connect for client {connectionId}", connection.ConnectionId);
await messageObservable.SubscribeToConnectionAsync(connection.ConnectionId, OnAddressedMessageReceived, OnClientSubscriptionEnd, default);
}
}
Expand All @@ -93,21 +90,21 @@ public override async Task OnDisconnectedAsync(HubConnectionContext connection)
hubConnectionStore.Remove(connection);
if (connection.UserIdentifier != null)
{
await userActorProvider.GetUserActor(connection.UserIdentifier)
await actorProviderFactory.GetUserActor(hubName, connection.UserIdentifier)
.RemoveFromUserAsync(connection.ConnectionId);
}
await messageObservable.UnsubscribeFromConnectionAsync(connection.ConnectionId);
}

public override Task AddToGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
{
var group = groupActorProvider.GetGroupActor(groupName);
var group = actorProviderFactory.GetGroupActor(hubName, groupName);
return group.AddToGroupAsync(connectionId, cancellationToken);
}

public override Task RemoveFromGroupAsync(string connectionId, string groupName, CancellationToken cancellationToken = default)
{
var group = groupActorProvider.GetGroupActor(groupName);
var group = actorProviderFactory.GetGroupActor(hubName, groupName);
return group.RemoveFromGroupAsync(connectionId, cancellationToken);
}

Expand Down Expand Up @@ -139,21 +136,21 @@ public override Task SendConnectionsAsync(IReadOnlyList<string> connectionIds, s
}
else
{
toAwait.Add(messageObserver.SendAddressedMessageAsync(msg));
toAwait.Add(messageObserver.SendAddressedMessageAsync(msg, cancellationToken));
}
}
return Task.WhenAll(toAwait);
}

public override Task SendGroupAsync(string groupName, string methodName, object[] args, CancellationToken cancellationToken = default)
{
var group = groupActorProvider.GetGroupActor(groupName);
var group = actorProviderFactory.GetGroupActor(hubName, groupName);
return group.AcceptMessageAsync(new AnonymousMessage(EmptySet<string>.Instance, new MethodMessage(methodName, args)), cancellationToken);
}

public override Task SendGroupExceptAsync(string groupName, string methodName, object[] args, IReadOnlyList<string> excludedConnectionIds, CancellationToken cancellationToken = default)
{
var group = groupActorProvider.GetGroupActor(groupName);
var group = actorProviderFactory.GetGroupActor(hubName, groupName);
return group.AcceptMessageAsync(new AnonymousMessage(excludedConnectionIds.ToSet(), new MethodMessage(methodName, args)), cancellationToken);
}

Expand All @@ -166,7 +163,7 @@ public override Task SendGroupsAsync(IReadOnlyList<string> groupNames, string me

public override Task SendUserAsync(string userId, string methodName, object[] args, CancellationToken cancellationToken = default)
{
var user = userActorProvider.GetUserActor(userId);
var user = actorProviderFactory.GetUserActor(hubName, userId);
return user.AcceptMessageAsync(new AnonymousMessage(EmptySet<string>.Instance, new MethodMessage(methodName, args)), cancellationToken);
}

Expand Down
6 changes: 3 additions & 3 deletions src/OrgnalR.Core/OrgnalR.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
</Description>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.3.0">
<PackageReference Include="Microsoft.Orleans.CodeGenerator.MSBuild" Version="3.6.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.6.2" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<!-- We have to explicitly target these two System packages. There is a conflict between two of our packages. See: https://github.com/LiamMorrow/OrgnalR/issues/13 -->
<PackageReference Include="System.IO" Version="4.3.0" />
Expand Down
9 changes: 9 additions & 0 deletions src/OrgnalR.Core/Provider/IActorProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OrgnalR.Core.State;

namespace OrgnalR.Core.Provider;

public interface IActorProviderFactory
{
IGroupActor GetGroupActor(string hubName, string groupName);
IUserActor GetUserActor(string hubName, string userId);
}
4 changes: 4 additions & 0 deletions src/OrgnalR.OrleansSilo/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
using Orleans;
using OrgnalR.Core;
using Microsoft.Extensions.DependencyInjection;
using OrgnalR.Core.Provider;
using OrgnalR.Backplane.GrainAdaptors;

namespace OrgnalR.Silo
{
Expand Down Expand Up @@ -110,6 +112,8 @@ public static ISiloBuilder AddOrgnalR(this ISiloBuilder builder, Action<OrgnalRS
var conf = new OrgnalRSiloConfig();
configure?.Invoke(conf);
services.Add(new ServiceDescriptor(typeof(OrgnalRSiloConfig), conf));

services.AddSingleton<IActorProviderFactory, GrainActorProviderFactory>();
});
builder.ConfigureApplicationParts(parts => parts.AddApplicationPart(typeof(AnonymousMessageGrain).Assembly).WithReferences());
return builder;
Expand Down
5 changes: 3 additions & 2 deletions src/OrgnalR.OrleansSilo/OrgnalR.OrleansSilo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\OrgnalR.Backplane.GrainImplementations\OrgnalR.Backplane.GrainImplementations.csproj" />
<ProjectReference Include="..\OrgnalR.Backplane.GrainAdaptors\OrgnalR.Backplane.GrainAdaptors.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.3.0" />
<PackageReference Include="Microsoft.Orleans.Runtime.Abstractions" Version="3.6.2" />
<PackageReference Include="Microsoft.Orleans.OrleansProviders" Version="3.6.2" />
</ItemGroup>
</Project>
Loading

0 comments on commit 01158c0

Please sign in to comment.