From 34fd12469fac121c2bdd859a579a96d7ab90d3cf Mon Sep 17 00:00:00 2001 From: Andres Gutierrez Date: Fri, 26 Jul 2024 10:09:25 -0500 Subject: [PATCH] Added missing ScheduleOnceStruct/GracefulShutdownStruct flavors --- Nixie/ActorRepositoryStruct.cs | 21 ++++++++++++++ Nixie/ActorScheduler.cs | 49 ++++++++++++++++++++++++++++++++ Nixie/ActorSystem.cs | 52 ++++++++++++++++++++++++++++++++-- Nixie/Nixie.csproj | 2 +- README.md | 4 +-- 5 files changed, 122 insertions(+), 6 deletions(-) diff --git a/Nixie/ActorRepositoryStruct.cs b/Nixie/ActorRepositoryStruct.cs index 0ba5872..f3a9d37 100644 --- a/Nixie/ActorRepositoryStruct.cs +++ b/Nixie/ActorRepositoryStruct.cs @@ -239,4 +239,25 @@ public async Task GracefulShutdown(string name, TimeSpan maxWait) return true; } + + /// + /// Tries to shutdown an actor by its name and returns a task whose result confirms shutdown within the specified timespan + /// + /// + /// + /// + public async Task GracefulShutdown(IActorRefStruct actorRef, TimeSpan maxWait) + { + string name = actorRef.Runner.Name; + + if (actors.TryGetValue(name, out Lazy<(ActorRunnerStruct runner, ActorRefStruct actorRef)>? actor)) + { + bool success = await actor.Value.runner.GracefulShutdown(maxWait); + actors.TryRemove(name, out _); + actorSystem.StopAllTimers(actor.Value.actorRef); + return success; + } + + return true; + } } diff --git a/Nixie/ActorScheduler.cs b/Nixie/ActorScheduler.cs index 8d2f126..70c64ab 100644 --- a/Nixie/ActorScheduler.cs +++ b/Nixie/ActorScheduler.cs @@ -149,6 +149,43 @@ public Timer ScheduleOnce(IActorRef actorRef return timer.Value; } + /// + /// Schedules a message to be sent to an actor once after a specified delay. + /// + /// + /// + /// + /// + /// + /// + /// + public Timer ScheduleOnceStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay) + where TActor : IActorStruct where TRequest : struct where TResponse : struct + { + long seq = Interlocked.Increment(ref sequence); + Lazy>> timers = onceTimers.GetOrAdd(actorRef, (object _) => new()); + Lazy timer = timers.Value.GetOrAdd(seq, (long key) => new(() => ScheduleOnceTimerStruct(actorRef, request, delay, seq))); + return timer.Value; + } + + /// + /// Schedules a message to be sent to an actor once after a specified delay. + /// + /// + /// + /// + /// + /// + /// + public Timer ScheduleOnceStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay) + where TActor : IActorStruct where TRequest : struct + { + long seq = Interlocked.Increment(ref sequence); + Lazy>> timers = onceTimers.GetOrAdd(actorRef, (object _) => new()); + Lazy timer = timers.Value.GetOrAdd(seq, (long key) => new(() => ScheduleOnceTimerStruct(actorRef, request, delay, seq))); + return timer.Value; + } + /// /// Stops a periodic timer /// @@ -275,6 +312,18 @@ private Timer ScheduleOnceTimer(IActorRef SendScheduledMessage(actorRef, request, random), null, delay, TimeSpan.Zero); } + private Timer ScheduleOnceTimerStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay, long random) + where TActor : IActorStruct where TRequest : struct + { + return new((state) => SendScheduledMessage(actorRef, request, random), null, delay, TimeSpan.Zero); + } + + private Timer ScheduleOnceTimerStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay, long random) + where TActor : IActorStruct where TRequest : struct where TResponse : struct + { + return new((state) => SendScheduledMessage(actorRef, request, random), null, delay, TimeSpan.Zero); + } + private void SendScheduledMessage(IActorRef actorRef, TRequest request, long random) where TActor : IActor where TRequest : class { diff --git a/Nixie/ActorSystem.cs b/Nixie/ActorSystem.cs index 1ff6583..fb25efa 100644 --- a/Nixie/ActorSystem.cs +++ b/Nixie/ActorSystem.cs @@ -1,9 +1,8 @@  -using Microsoft.Extensions.Logging; using Nixie.Actors; using Nixie.Utils; +using Microsoft.Extensions.Logging; using System.Collections.Concurrent; -using System.Diagnostics; namespace Nixie; @@ -351,7 +350,7 @@ public async Task GracefulShutdownStruct(string name, Ti /// /// /// - /// + /// /// /// public async Task GracefulShutdownStruct(IActorRefStruct actorRef, TimeSpan maxWait) @@ -362,6 +361,22 @@ public async Task GracefulShutdownStruct(IAct return await repository.GracefulShutdown(actorRef, maxWait); } + /// + /// Shutdowns an actor by its reference + /// + /// + /// + /// + /// + /// + public async Task GracefulShutdownStruct(IActorRefStruct actorRef, TimeSpan maxWait) + where TActor : IActorStruct where TRequest : struct + { + ActorRepositoryStruct repository = GetRepositoryStruct(); + + return await repository.GracefulShutdown(actorRef, maxWait); + } + /// /// Returns the repository where the current references to request/response actors are stored. /// @@ -569,6 +584,37 @@ public void ScheduleOnce(IActorRef actorRef, scheduler.ScheduleOnce(actorRef, request, delay); } + /// + /// Schedules a message to be sent to an actor once after a specified delay. + /// + /// + /// + /// + /// + /// + /// + /// + public void ScheduleOnceStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay) + where TActor : IActorStruct where TRequest : struct where TResponse : struct + { + scheduler.ScheduleOnceStruct(actorRef, request, delay); + } + + /// + /// Schedules a message to be sent to an actor once after a specified delay. + /// + /// + /// + /// + /// + /// + /// + public void ScheduleOnceStruct(IActorRefStruct actorRef, TRequest request, TimeSpan delay) + where TActor : IActorStruct where TRequest : struct + { + scheduler.ScheduleOnceStruct(actorRef, request, delay); + } + /// /// Stops a periodic timer /// diff --git a/Nixie/Nixie.csproj b/Nixie/Nixie.csproj index 40cab64..0c3b7b7 100644 --- a/Nixie/Nixie.csproj +++ b/Nixie/Nixie.csproj @@ -9,7 +9,7 @@ enable enable Nixie - 1.0.2 + 1.0.3 A Lightweight Actor Model Implementation for C#/.NET Andres Gutierrez Andres Gutierrez diff --git a/README.md b/README.md index bf8eb0e..a6242eb 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ To install Nixie into your C#/.NET project, you can use the .NET CLI or the NuGe #### Using .NET CLI ```shell -dotnet add package Nixie --version 1.0.2 +dotnet add package Nixie --version 1.0.3 ``` ### Using NuGet Package Manager @@ -39,7 +39,7 @@ dotnet add package Nixie --version 1.0.2 Search for Nixie and install it from the NuGet package manager UI, or use the Package Manager Console: ```shell -Install-Package Nixie -Version 1.0.2 +Install-Package Nixie -Version 1.0.3 ``` ## Usage