Skip to content

Commit

Permalink
handle shutdown (#2070)
Browse files Browse the repository at this point in the history
  • Loading branch information
rogeralsing authored Oct 26, 2023
1 parent 082ff80 commit 07ef068
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/Proto.Actor/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ public sealed class ActorSystem : IAsyncDisposable
{
public const string NoHost = "nonhost";
public const string Client = "$client";
#pragma warning disable CS0618 // Type or member is obsolete
private readonly ILogger _logger = Log.CreateLogger<ActorSystem>();
#pragma warning restore CS0618 // Type or member is obsolete
private string _host = NoHost;
private int _port;

Expand All @@ -41,7 +43,11 @@ public ActorSystem(ActorSystemConfig config)
Diagnostics = new DiagnosticsStore(this);
ProcessRegistry = new ProcessRegistry(this);
Root = NewRoot();
DeadLetter = new DeadLetterProcess(this).Configure();
var dl = new DeadLetterProcess(this);
var dlPid = new PID(Address, "$deadletter", dl);
DeadLetterPid = dlPid;
DeadLetter = dl.Configure();
ProcessRegistry.TryAdd("$deadletter", DeadLetter);
Guardians = new Guardians(this);
EventStream = new EventStream(this);
Metrics = new ProtoMetrics(config.MetricsEnabled);
Expand Down Expand Up @@ -108,6 +114,11 @@ public ActorSystem(ActorSystemConfig config)
/// DeadLetter process that receives all messages that could not be delivered to an actor.
/// </summary>
public Process DeadLetter { get; }

/// <summary>
/// Pid for the DeadLetter process.
/// </summary>
public PID DeadLetterPid { get; }

/// <summary>
/// Allows to broadcast messages across the actor system to anyone who explicitly subscribed.
Expand Down
2 changes: 1 addition & 1 deletion src/Proto.Actor/Context/ActorContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private Task HandleProcessDiagnosticsRequest(ProcessDiagnosticsRequest processDi
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private Task InternalInvokeUserMessageAsync(object msg)
{
if (_state == ContextState.Stopped)
if (_state == ContextState.Stopped || System.Shutdown.IsCancellationRequested)
{
//already stopped, send message to deadletter process
System.DeadLetter.SendUserMessage(Self, msg);
Expand Down
6 changes: 6 additions & 0 deletions src/Proto.Actor/Props/Props.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,12 @@ public sealed record Props
public static PID DefaultSpawner(ActorSystem system, string name, Props props, PID? parent,
Action<IContext>? callback)
{
//if system is shutting down. don't spawn new actors
if (system.Shutdown.IsCancellationRequested)
{
return system.DeadLetterPid;
}

//Ordering is important here
//first we create a mailbox and attach it to a process
props = system.ConfigureProps(props);
Expand Down

0 comments on commit 07ef068

Please sign in to comment.