Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle shutdown #2070

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading