Skip to content

Commit

Permalink
Added support for struct based routers
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Jul 28, 2024
1 parent c3c8237 commit 8d9da3b
Show file tree
Hide file tree
Showing 12 changed files with 638 additions and 7 deletions.
60 changes: 60 additions & 0 deletions Nixie.Tests/Actors/RouteeActorStruct.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

using Nixie.Routers;

namespace Nixie.Tests.Actors;

public readonly struct RouterMessageStruct : IConsistentHashable
{
public RouterMessageType Type { get; }

public string Data { get; }

public RouterMessageStruct(RouterMessageType type, string data)
{
Type = type;
Data = data;
}

public int GetHash()
{
return Data switch
{
"aaa" => 0,
"bbb" => 1,
"ccc" => 2,
"ddd" => 3,
"eee" => 4,
_ => 0
};
}
}

public sealed class RouteeActorStruct : IActorStruct<RouterMessageStruct>
{
private int receivedMessages;

private readonly IActorContextStruct<RouteeActorStruct, RouterMessageStruct> context;

public RouteeActorStruct(IActorContextStruct<RouteeActorStruct, RouterMessageStruct> context)
{
this.context = context;
}

public int GetMessages()
{
return receivedMessages;
}

private void IncrMessage()
{
receivedMessages++;
}

public async Task Receive(RouterMessageStruct message)
{
await Task.Yield();

if (message.Type == RouterMessageType.Route)
IncrMessage();
}
}
29 changes: 29 additions & 0 deletions Nixie.Tests/TestRouters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,35 @@ public async Task TestCreateRoundRobinRouter()
Assert.Equal(1, routeeActor.GetMessages());
}
}

[Fact]
public async Task TestCreateRoundRobinRouterStruct()
{
using ActorSystem asx = new();

IActorRefStruct<RoundRobinActorStruct<RouteeActorStruct, RouterMessageStruct>, RouterMessageStruct> router =
asx.SpawnStruct<RoundRobinActorStruct<RouteeActorStruct, RouterMessageStruct>, RouterMessageStruct>("my-router", 5);

router.Send(new RouterMessageStruct(RouterMessageType.Route, "aaa"));
router.Send(new RouterMessageStruct(RouterMessageType.Route, "bbb"));
router.Send(new RouterMessageStruct(RouterMessageType.Route, "ccc"));
router.Send(new RouterMessageStruct(RouterMessageType.Route, "ddd"));
router.Send(new RouterMessageStruct(RouterMessageType.Route, "eee"));

await asx.Wait();

Assert.IsAssignableFrom<RoundRobinActorStruct<RouteeActorStruct, RouterMessageStruct>>(router.Runner.Actor);

RoundRobinActorStruct<RouteeActorStruct, RouterMessageStruct> routerActor = (RoundRobinActorStruct<RouteeActorStruct, RouterMessageStruct>)router.Runner.Actor!;

foreach (IActorRefStruct<RouteeActorStruct, RouterMessageStruct> routee in routerActor.Instances)
{
Assert.IsAssignableFrom<RouteeActorStruct>(routee.Runner.Actor);

RouteeActorStruct routeeActor = (RouteeActorStruct)routee.Runner.Actor!;
Assert.Equal(1, routeeActor.GetMessages());
}
}

[Fact]
public async Task TestCreateRoundRobinRouterExt()
Expand Down
4 changes: 2 additions & 2 deletions Nixie/ActorRepositoryReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public bool IsProcessing(out string? actorName)
{
ActorRunner<TActor, TRequest, TResponse> runner = lazyValue.Value.runner;

if (!runner.IsShutdown && runner.IsProcessing)
if (runner is { IsShutdown: false, IsProcessing: true })
{
actorName = runner.Name;
return true;
Expand Down Expand Up @@ -110,7 +110,7 @@ public IActorRef<TActor, TRequest, TResponse> Spawn(string? name = null, params

Lazy<(ActorRunner<TActor, TRequest, TResponse> runner, ActorRef<TActor, TRequest, TResponse> actorRef)> actor = actors.GetOrAdd(
name,
(string name) => new Lazy<(ActorRunner<TActor, TRequest, TResponse>, ActorRef<TActor, TRequest, TResponse>)>(() => CreateInternal(name, args))
(string _) => new(() => CreateInternal(name, args))
);

return actor.Value.actorRef;
Expand Down
2 changes: 1 addition & 1 deletion Nixie/Nixie.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PackageId>Nixie</PackageId>
<Version>1.0.3</Version>
<Version>1.0.4</Version>
<Description>A Lightweight Actor Model Implementation for C#/.NET</Description>
<Authors>Andres Gutierrez</Authors>
<Company>Andres Gutierrez</Company>
Expand Down
2 changes: 1 addition & 1 deletion Nixie/Routers/ActorSystemExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ List<IActorRef<TActor, TRequest, TResponse>> instances
}

/// <summary>
/// Creates a Round-Robin router specifying number of instances
/// Creates a Consistent Hash router specifying name and number of instances
/// </summary>
/// <typeparam name="TActor"></typeparam>
/// <typeparam name="TRequest"></typeparam>
Expand Down
Loading

0 comments on commit 8d9da3b

Please sign in to comment.