Skip to content

Commit

Permalink
System props (#1601)
Browse files Browse the repository at this point in the history
* allow configure of system props
* fix log messages
  • Loading branch information
rogeralsing authored May 25, 2022
1 parent 476b08d commit ebf1afa
Show file tree
Hide file tree
Showing 15 changed files with 235 additions and 178 deletions.
6 changes: 4 additions & 2 deletions src/Proto.Actor/ActorSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Proto;
[PublicAPI]
public sealed class ActorSystem : IAsyncDisposable
{
private static readonly ILogger Logger = Log.CreateLogger<ActorSystem>();
private readonly ILogger _logger = Log.CreateLogger<ActorSystem>();
public const string NoHost = "nonhost";
public const string Client = "$client";
private string _host = NoHost;
Expand Down Expand Up @@ -101,7 +101,7 @@ public Task ShutdownAsync(string reason="")
{
try
{
Logger.LogInformation("Shutting down actor system {Id}", Id);
_logger.LogInformation("Shutting down actor system {Id}", Id);
Stopper.Stop(reason);
}
catch
Expand Down Expand Up @@ -132,6 +132,8 @@ public RootContext NewRoot(MessageHeader? headers = null, params Func<Sender, Se
public (string Host, int Port) GetAddress() => (_host, _port);

public Props ConfigureProps(Props props) => Config.ConfigureProps(props);

public Props ConfigureSystemProps(string name, Props props) => Config.ConfigureSystemProps(name, props);

public async ValueTask DisposeAsync()
{
Expand Down
15 changes: 15 additions & 0 deletions src/Proto.Actor/Configuration/ActorSystemConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using JetBrains.Annotations;
using Microsoft.Extensions.Logging;
using Proto.Context;

// ReSharper disable once CheckNamespace
namespace Proto;
Expand Down Expand Up @@ -47,6 +48,18 @@ public record ActorSystemConfig
/// All props are translated via this function
/// </summary>
public Func<Props, Props> ConfigureProps { get; init; } = props => props;

/// <summary>
/// Allows ActorSystem-wide augmentation of system Props
/// All system props are translated via this function
/// </summary>
public Func<string, Props, Props> ConfigureSystemProps { get; init; } = (_,props) => {
var logger = Log.CreateLogger("SystemActors");
return props
.WithDeadlineDecorator(TimeSpan.FromSeconds(1), logger)
.WithLoggingContextDecorator(logger, LogLevel.None, LogLevel.Information, LogLevel.Error )
.WithGuardianSupervisorStrategy(Supervision.AlwaysRestartStrategy);
};

/// <summary>
/// Enables SharedFutures
Expand Down Expand Up @@ -106,6 +119,8 @@ public ActorSystemConfig WithDeadLetterThrottleCount(int deadLetterThrottleCount
public ActorSystemConfig WithDiagnosticsSerializer(Func<IActor, string> serializer) => this with {DiagnosticsSerializer = serializer};

public ActorSystemConfig WithConfigureProps(Func<Props, Props> configureProps) => this with {ConfigureProps = configureProps};

public ActorSystemConfig WithConfigureSystemProps(Func<string, Props, Props> configureSystemProps) => this with {ConfigureSystemProps = configureSystemProps};

public ActorSystemConfig WithThreadPoolStatsTimeout(TimeSpan threadPoolStatsTimeout)
=> this with {ThreadPoolStatsTimeout = threadPoolStatsTimeout};
Expand Down
13 changes: 9 additions & 4 deletions src/Proto.Actor/Context/ActorLoggingContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,24 @@ public override async Task Receive(MessageEnvelope envelope)

public override void ReenterAfter<T>(Task<T> target, Func<Task<T>, Task> action)
{
if (_logger.IsEnabled(_logLevel))
{
_logger.Log(_logLevel, "Actor {Self} {ActorType} ReenterAfter {Action}", Self, ActorType, action.Method.Name);
}
base.ReenterAfter(target, action);
}

public override void ReenterAfter(Task target, Action action)
{
if (_logger.IsEnabled(_logLevel))
{
_logger.Log(_logLevel, "Actor {Self} {ActorType} ReenterAfter {Action}", Self, ActorType, action.Method.Name);
}
base.ReenterAfter(target, action);
}

public override async Task<T> RequestAsync<T>(PID target, object message, CancellationToken cancellationToken)
{
T response;
if (_logger.IsEnabled(_logLevel))
{
_logger.Log(_logLevel, "Actor {Self} {ActorType} Sending ReqeustAsync {MessageType}:{Message} to {Target}", Self, ActorType,
Expand All @@ -108,7 +115,7 @@ public override async Task<T> RequestAsync<T>(PID target, object message, Cancel

try
{
response = await base.RequestAsync<T>(target, message, cancellationToken);
var response = await base.RequestAsync<T>(target, message, cancellationToken);

if (_logger.IsEnabled(_logLevel))
{
Expand Down Expand Up @@ -182,8 +189,6 @@ public override void Respond(object message)

base.Respond(message);
}



private string ActorType => Actor?.GetType().Name ?? "None";
}
4 changes: 3 additions & 1 deletion src/Proto.Actor/Context/RootContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,12 @@ public PID SpawnNamed(Props props, string name)
}
catch (Exception x)
{
Logger.LogError(x, "RootContext Failed to spawn child actor {Name}", name);
Logger.LogError(x, "RootContext Failed to spawn root level actor {Name}", name);
throw;
}
}



public object? Message => null;

Expand Down
33 changes: 33 additions & 0 deletions src/Proto.Actor/Context/SystemContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// -----------------------------------------------------------------------
// <copyright file = "SystemContext.cs" company = "Asynkron AB">
// Copyright (C) 2015-2022 Asynkron AB All rights reserved
// </copyright>
// -----------------------------------------------------------------------
using System;
using Microsoft.Extensions.Logging;

namespace Proto;

public static class SystemContext
{
private static readonly ILogger Logger = Log.CreateLogger(nameof(SystemContext));

public static PID SpawnNamedSystem(this RootContext self, Props props, string name)
{
try
{
var parent = props.GuardianStrategy is not null
? self.System.Guardians.GetGuardianPid(props.GuardianStrategy)
: null;

//augment props with system actor specific settings
props = self.System.ConfigureSystemProps(name, props);
return props.Spawn(self.System, name, parent);
}
catch (Exception x)
{
Logger.LogError(x, "SystemContext Failed to spawn system actor {Name}", name);
throw;
}
}
}
Loading

0 comments on commit ebf1afa

Please sign in to comment.