Skip to content

Commit

Permalink
Stop all timers when shutting down actors
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgutierrez committed Sep 28, 2024
1 parent acb4135 commit 2869a2f
Show file tree
Hide file tree
Showing 8 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
- name: Run Tests
run: (cd Nixie.Tests && dotnet dotcover test --dcOutput=NixieCoverageReport.html --dcReportType=HTML)
- name: Archive code coverage results
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: code-coverage-report
path: |
Expand Down
3 changes: 2 additions & 1 deletion Nixie/ActorRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public async Task<bool> GracefulShutdown(string name, TimeSpan maxWait)
if (actors.TryGetValue(name, out Lazy<(ActorRunner<TActor, TRequest> runner, ActorRef<TActor, TRequest> actorRef)>? actor))
{
bool result = await actor.Value.runner.GracefulShutdown(maxWait);
actorSystem.StopAllTimers(actor.Value.actorRef);
actors.TryRemove(name, out _);
return result;
}
Expand All @@ -252,8 +253,8 @@ public async Task<bool> GracefulShutdown(IActorRef<TActor, TRequest> actorRef, T
if (actors.TryGetValue(name, out Lazy<(ActorRunner<TActor, TRequest> runner, ActorRef<TActor, TRequest> actorRef)>? actor))
{
bool success = await actor.Value.runner.GracefulShutdown(maxWait);
actors.TryRemove(name, out _);
actorSystem.StopAllTimers(actor.Value.actorRef);
actors.TryRemove(name, out _);
return success;
}

Expand Down
2 changes: 1 addition & 1 deletion Nixie/ActorRepositoryReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,8 @@ public bool Shutdown(string name)
{
if (actor.Value.runner.Shutdown())
{
actors.TryRemove(name, out _);
actorSystem.StopAllTimers(actor.Value.actorRef);
actors.TryRemove(name, out _);
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Nixie/ActorRepositoryStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public bool IsProcessing(out string? actorName)
{
ActorRunnerStruct<TActor, TRequest> 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 IActorRefStruct<TActor, TRequest> Spawn(string? name = null, params objec

Lazy<(ActorRunnerStruct<TActor, TRequest> runner, ActorRefStruct<TActor, TRequest> actorRef)> actor = actors.GetOrAdd(
name,
(string name) => new Lazy<(ActorRunnerStruct<TActor, TRequest>, ActorRefStruct<TActor, TRequest>)>(() => CreateInternal(name, args))
(string name) => new(() => CreateInternal(name, args))
);

return actor.Value.actorRef;
Expand Down
4 changes: 3 additions & 1 deletion Nixie/ActorRepositoryStructReply.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public IActorRefStruct<TActor, TRequest, TResponse> Spawn(string? name = null, p

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

return actor.Value.actorRef;
Expand Down Expand Up @@ -188,6 +188,7 @@ public bool Shutdown(string name)
{
if (actor.Value.runner.Shutdown())
{
actorSystem.StopAllTimers(actor.Value.actorRef);
actors.TryRemove(name, out _);
return true;
}
Expand All @@ -209,6 +210,7 @@ public bool Shutdown(IActorRefStruct<TActor, TRequest, TResponse> actorRef)
{
if (actor.Value.runner.Shutdown())
{
actorSystem.StopAllTimers(actor.Value.actorRef);
actors.TryRemove(name, out _);
return true;
}
Expand Down
3 changes: 1 addition & 2 deletions Nixie/ActorScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ public Timer ScheduleShutdown<TActor, TRequest>(IActorRef<TActor, TRequest> acto
/// </summary>
/// <param name="name"></param>
/// <exception cref="NixieException"></exception>
public void StopPeriodicTimer<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef, string name)
where TActor : IActor<TRequest> where TRequest : class
public void StopPeriodicTimer<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef, string name) where TActor : IActor<TRequest> where TRequest : class
{
if (periodicTimers.TryGetValue(actorRef, out Lazy<ConcurrentDictionary<string, Lazy<Timer>>>? timers))
{
Expand Down
9 changes: 3 additions & 6 deletions Nixie/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,8 +652,7 @@ public void ScheduleShutdown<TActor, TRequest>(IActorRef<TActor, TRequest> actor
/// </summary>
/// <param name="name"></param>
/// <exception cref="NixieException"></exception>
public void StopPeriodicTimer<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef, string name)
where TActor : IActor<TRequest> where TRequest : class
public void StopPeriodicTimer<TActor, TRequest>(IActorRef<TActor, TRequest> actorRef, string name) where TActor : IActor<TRequest> where TRequest : class
{
scheduler.StopPeriodicTimer(actorRef, name);
}
Expand All @@ -663,8 +662,7 @@ public void StopPeriodicTimer<TActor, TRequest>(IActorRef<TActor, TRequest> acto
/// </summary>
/// <param name="name"></param>
/// <exception cref="NixieException"></exception>
public void StopPeriodicTimer<TActor, TRequest, TResponse>(IActorRef<TActor, TRequest, TResponse> actorRef, string name)
where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class?
public void StopPeriodicTimer<TActor, TRequest, TResponse>(IActorRef<TActor, TRequest, TResponse> actorRef, string name) where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class?
{
scheduler.StopPeriodicTimer(actorRef, name);
}
Expand All @@ -676,8 +674,7 @@ public void StopPeriodicTimer<TActor, TRequest, TResponse>(IActorRef<TActor, TRe
/// <typeparam name="TRequest"></typeparam>
/// <typeparam name="TResponse"></typeparam>
/// <param name="actorRef"></param>
public void StopAllTimers<TActor, TRequest, TResponse>(IActorRef<TActor, TRequest, TResponse> actorRef)
where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class?
public void StopAllTimers<TActor, TRequest, TResponse>(IActorRef<TActor, TRequest, TResponse> actorRef) where TActor : IActor<TRequest, TResponse> where TRequest : class where TResponse : class?
{
scheduler.StopAllTimers(actorRef);
}
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Nixie is a lightweight, high-performance implementation of the [actor model](htt

### Prerequisites

- .NET SDK 6.0 or later
- .NET SDK 8.0 or later
- A suitable IDE (e.g., Visual Studio, Visual Studio Code, or Rider)

### Installation
Expand All @@ -48,15 +48,15 @@ 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.8
dotnet add package Nixie --version 1.0.9
```

### Using NuGet Package Manager

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.8
Install-Package Nixie -Version 1.0.9
```

## Usage
Expand Down

0 comments on commit 2869a2f

Please sign in to comment.