From 288f1639be5788b6fc207eab310ff8b4d326ea6e Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sun, 8 Mar 2020 14:39:17 +0100 Subject: [PATCH] code cleanup and remove obsolete members --- src/Proto.Actor/ActorContext.cs | 126 +++++++++-------- src/Proto.Actor/ActorContextDecorator.cs | 2 +- src/Proto.Actor/Behavior.cs | 9 +- src/Proto.Actor/Delegates.cs | 4 +- src/Proto.Actor/EmptyActor.cs | 9 +- src/Proto.Actor/EventStream.cs | 57 ++++---- src/Proto.Actor/Exceptions.cs | 6 +- src/Proto.Actor/Extensions.cs | 17 +-- src/Proto.Actor/Futures.cs | 12 +- src/Proto.Actor/Guardians.cs | 2 +- src/Proto.Actor/HashedConcurrentDictionary.cs | 4 +- src/Proto.Actor/IContext.cs | 15 +- src/Proto.Actor/IInfoContext.cs | 4 - src/Proto.Actor/IReceiverContext.cs | 2 +- src/Proto.Actor/ISenderContext.cs | 40 +++--- src/Proto.Actor/ISpawnContext.cs | 4 +- src/Proto.Actor/ISpawnerContext.cs | 4 +- src/Proto.Actor/MessageEnvelope.cs | 16 +-- src/Proto.Actor/MessageExtensions.cs | 2 +- src/Proto.Actor/MessageHeader.cs | 37 +++-- src/Proto.Actor/Messages.cs | 47 +++++-- src/Proto.Actor/NullLoggerFactory.cs | 8 +- src/Proto.Actor/PID.cs | 42 +----- src/Proto.Actor/Process.cs | 10 +- src/Proto.Actor/ProcessRegistry.cs | 8 +- src/Proto.Actor/Props.cs | 67 +++++---- src/Proto.Actor/RestartStatistics.cs | 6 +- src/Proto.Actor/RootContext.cs | 129 +++++++++--------- src/Proto.Actor/RootContextDecorator.cs | 3 +- src/Proto.Actor/Supervision.cs | 33 +++-- src/Proto.Remote/RemoteConfig.cs | 6 +- 31 files changed, 396 insertions(+), 335 deletions(-) diff --git a/src/Proto.Actor/ActorContext.cs b/src/Proto.Actor/ActorContext.cs index 2cfa0a0da1..626c45154e 100644 --- a/src/Proto.Actor/ActorContext.cs +++ b/src/Proto.Actor/ActorContext.cs @@ -22,12 +22,17 @@ internal enum ContextState : byte Alive, Restarting, Stopping, - Stopped, + Stopped } //Angels cry over this code, but it serves a purpose, lazily init of less frequently used features public class ActorContextExtras { + public ActorContextExtras(IContext context) + { + Context = context; + } + public ImmutableHashSet Children { get; private set; } = ImmutableHashSet.Empty; public Timer? ReceiveTimeoutTimer { get; private set; } public RestartStatistics RestartStatistics { get; } = new RestartStatistics(0, null); @@ -35,11 +40,6 @@ public class ActorContextExtras public ImmutableHashSet Watchers { get; private set; } = ImmutableHashSet.Empty; public IContext Context { get; } - public ActorContextExtras(IContext context) - { - Context = context; - } - public void InitReceiveTimeoutTimer(Timer timer) => ReceiveTimeoutTimer = timer; public void ResetReceiveTimeoutTimer(TimeSpan timeout) => ReceiveTimeoutTimer?.Change(timeout, timeout); @@ -76,17 +76,6 @@ public class ActorContext : IMessageInvoker, IContext, ISupervisor private object? _messageOrEnvelope; private ContextState _state; - private ActorContextExtras EnsureExtras() - { - if (_extras == null) - { - var context = _props.ContextDecoratorChain?.Invoke(this) ?? this; - _extras = new ActorContextExtras(context); - } - - return _extras; - } - public ActorContext(Props props, PID parent, PID self) { _props = props; @@ -99,8 +88,6 @@ public ActorContext(Props props, PID parent, PID self) } private static ILogger Logger { get; } = Log.CreateLogger(); - - public IImmutableSet Children => _extras?.Children ?? EmptyChildren; IReadOnlyCollection IContext.Children => Children; public IActor? Actor { get; private set; } @@ -219,12 +206,14 @@ public void Request(PID target, object message, PID sender) SendUserMessage(target, messageEnvelope); } - public Task RequestAsync(PID target, object message, TimeSpan timeout) => RequestAsync(target, message, new FutureProcess(timeout)); + public Task RequestAsync(PID target, object message, TimeSpan timeout) => + RequestAsync(target, message, new FutureProcess(timeout)); public Task RequestAsync(PID target, object message, CancellationToken cancellationToken) => RequestAsync(target, message, new FutureProcess(cancellationToken)); - public Task RequestAsync(PID target, object message) => RequestAsync(target, message, new FutureProcess()); + public Task RequestAsync(PID target, object message) => + RequestAsync(target, message, new FutureProcess()); public void ReenterAfter(Task target, Func, Task> action) { @@ -249,6 +238,40 @@ public void ReenterAfter(Task target, Action action) target.ContinueWith(t => { Self.SendSystemMessage(cont); }); } + public Task Receive(MessageEnvelope envelope) + { + _messageOrEnvelope = envelope; + return DefaultReceive(); + } + + public void Stop(PID pid) + { + var reff = ProcessRegistry.Instance.Get(pid); + reff.Stop(pid); + } + + public Task StopAsync(PID pid) + { + var future = new FutureProcess(); + + pid.SendSystemMessage(new Watch(future.Pid)); + Stop(pid); + + return future.Task; + } + + public void Poison(PID pid) => pid.SendUserMessage(new PoisonPill()); + + public Task PoisonAsync(PID pid) + { + var future = new FutureProcess(); + + pid.SendSystemMessage(new Watch(future.Pid)); + Poison(pid); + + return future.Task; + } + public void EscalateFailure(Exception reason, object message) { var failure = new Failure(Self, reason, EnsureExtras().RestartStatistics, message); @@ -264,12 +287,6 @@ public void EscalateFailure(Exception reason, object message) } } - public void RestartChildren(Exception reason, params PID[] pids) => pids.SendSystemMessage(new Restart(reason)); - - public void StopChildren(params PID[] pids) => pids.SendSystemMessage(Proto.Stop.Instance); - - public void ResumeChildren(params PID[] pids) => pids.SendSystemMessage(ResumeMailbox.Instance); - public Task InvokeSystemMessageAsync(object msg) { try @@ -350,10 +367,23 @@ public Task InvokeUserMessageAsync(object msg) return res; } - public Task Receive(MessageEnvelope envelope) + public IImmutableSet Children => _extras?.Children ?? EmptyChildren; + + public void RestartChildren(Exception reason, params PID[] pids) => pids.SendSystemMessage(new Restart(reason)); + + public void StopChildren(params PID[] pids) => pids.SendSystemMessage(Proto.Stop.Instance); + + public void ResumeChildren(params PID[] pids) => pids.SendSystemMessage(ResumeMailbox.Instance); + + private ActorContextExtras EnsureExtras() { - _messageOrEnvelope = envelope; - return DefaultReceive(); + if (_extras == null) + { + var context = _props.ContextDecoratorChain?.Invoke(this) ?? this; + _extras = new ActorContextExtras(context); + } + + return _extras; } [MethodImpl(MethodImplOptions.AggressiveInlining)] @@ -444,7 +474,9 @@ private void HandleFailure(Failure msg) supervisor.HandleFailure(this, msg.Who, msg.RestartStatistics, msg.Reason, msg.Message); break; default: - _props.SupervisorStrategy.HandleFailure(this, msg.Who, msg.RestartStatistics, msg.Reason, msg.Message); + _props.SupervisorStrategy.HandleFailure(this, msg.Who, msg.RestartStatistics, msg.Reason, + msg.Message + ); break; } } @@ -461,7 +493,9 @@ private async Task HandleTerminatedAsync(Terminated msg) } private void HandleRootFailure(Failure failure) - => Supervision.DefaultStrategy.HandleFailure(this, failure.Who, failure.RestartStatistics, failure.Reason, failure.Message); + => Supervision.DefaultStrategy.HandleFailure(this, failure.Who, failure.RestartStatistics, failure.Reason, + failure.Message + ); //Initiate stopping, not final private async Task InitiateStopAsync() @@ -560,33 +594,5 @@ private void ReceiveTimeoutCallback(object state) CancelReceiveTimeout(); Send(Self, Proto.ReceiveTimeout.Instance); } - - public void Stop(PID pid) - { - var reff = ProcessRegistry.Instance.Get(pid); - reff.Stop(pid); - } - - public Task StopAsync(PID pid) - { - var future = new FutureProcess(); - - pid.SendSystemMessage(new Watch(future.Pid)); - Stop(pid); - - return future.Task; - } - - public void Poison(PID pid) => pid.SendUserMessage(new PoisonPill()); - - public Task PoisonAsync(PID pid) - { - var future = new FutureProcess(); - - pid.SendSystemMessage(new Watch(future.Pid)); - Poison(pid); - - return future.Task; - } } } \ No newline at end of file diff --git a/src/Proto.Actor/ActorContextDecorator.cs b/src/Proto.Actor/ActorContextDecorator.cs index 5815c05ca2..bbb63d4f71 100644 --- a/src/Proto.Actor/ActorContextDecorator.cs +++ b/src/Proto.Actor/ActorContextDecorator.cs @@ -39,7 +39,7 @@ public virtual Task RequestAsync(PID target, object message, CancellationT public virtual MessageHeader Headers => _context.Headers; public virtual object Message => _context.Message; - + public virtual Task Receive(MessageEnvelope envelope) => _context.Receive(envelope); public virtual PID? Parent => _context.Parent; diff --git a/src/Proto.Actor/Behavior.cs b/src/Proto.Actor/Behavior.cs index 5f02b9a794..9a5c8f9d79 100644 --- a/src/Proto.Actor/Behavior.cs +++ b/src/Proto.Actor/Behavior.cs @@ -13,9 +13,14 @@ public class Behavior { private readonly Stack _behaviors = new Stack(); - public Behavior() { } + public Behavior() + { + } - public Behavior(Receive receive) => Become(receive); + public Behavior(Receive receive) + { + Become(receive); + } public void Become(Receive receive) { diff --git a/src/Proto.Actor/Delegates.cs b/src/Proto.Actor/Delegates.cs index b7c84b8f18..dd8315873a 100644 --- a/src/Proto.Actor/Delegates.cs +++ b/src/Proto.Actor/Delegates.cs @@ -3,9 +3,9 @@ namespace Proto { public delegate Task Receive(IContext context); - + //TODO: IReceiveContext ? public delegate Task Receiver(IReceiverContext context, MessageEnvelope envelope); - + public delegate Task Sender(ISenderContext context, PID target, MessageEnvelope envelope); } \ No newline at end of file diff --git a/src/Proto.Actor/EmptyActor.cs b/src/Proto.Actor/EmptyActor.cs index a519d06177..2973530045 100644 --- a/src/Proto.Actor/EmptyActor.cs +++ b/src/Proto.Actor/EmptyActor.cs @@ -2,10 +2,15 @@ namespace Proto { - class EmptyActor : IActor + internal class EmptyActor : IActor { private readonly Receive _receive; - public EmptyActor(Receive receive) => _receive = receive; + + public EmptyActor(Receive receive) + { + _receive = receive; + } + public Task ReceiveAsync(IContext context) => _receive(context); } } \ No newline at end of file diff --git a/src/Proto.Actor/EventStream.cs b/src/Proto.Actor/EventStream.cs index fbf9b3fc1e..c86a8fcf63 100644 --- a/src/Proto.Actor/EventStream.cs +++ b/src/Proto.Actor/EventStream.cs @@ -21,15 +21,19 @@ public class EventStream : EventStream internal EventStream() { Subscribe(msg => - { - if (msg is DeadLetterEvent letter) { - _logger.LogInformation("[DeadLetter] '{0}' got '{1}:{2}' from '{3}'", letter.Pid.ToShortString(), - letter.Message.GetType().Name, letter.Message, letter.Sender?.ToShortString()); + if (msg is DeadLetterEvent letter) + { + _logger.LogInformation("[DeadLetter] '{0}' got '{1}:{2}' from '{3}'", + letter.Pid.ToShortString(), + letter.Message.GetType().Name, letter.Message, letter.Sender?.ToShortString() + ); + } } - }); + ); } } + public class EventStream { private readonly ILogger _logger = Log.CreateLogger>(); @@ -44,10 +48,11 @@ internal EventStream() public Subscription Subscribe(Action action, IDispatcher? dispatcher = null) { var sub = new Subscription(this, dispatcher ?? Dispatchers.SynchronousDispatcher, x => - { - action(x); - return Actor.Done; - }); + { + action(x); + return Actor.Done; + } + ); _subscriptions.TryAdd(sub.Id, sub); return sub; } @@ -62,13 +67,15 @@ public Subscription Subscribe(Func action, IDispatcher? dispatcher = public Subscription Subscribe(Action action, IDispatcher? dispatcher = null) where TMsg : T { var sub = new Subscription(this, dispatcher ?? Dispatchers.SynchronousDispatcher, msg => - { - if (msg is TMsg typed) { - action(typed); + if (msg is TMsg typed) + { + action(typed); + } + + return Actor.Done; } - return Actor.Done; - }); + ); _subscriptions.TryAdd(sub.Id, sub); return sub; @@ -79,17 +86,19 @@ public void Publish(T msg) foreach (var sub in _subscriptions.Values) { sub.Dispatcher.Schedule(() => - { - try - { - sub.Action(msg); - } - catch (Exception ex) { - _logger.LogWarning(0, ex, "Exception has occurred when publishing a message."); + try + { + sub.Action(msg); + } + catch (Exception ex) + { + _logger.LogWarning(0, ex, "Exception has occurred when publishing a message."); + } + + return Actor.Done; } - return Actor.Done; - }); + ); } } @@ -114,4 +123,4 @@ public Subscription(EventStream eventStream, IDispatcher dispatcher, Func _eventStream.Unsubscribe(Id); } -} +} \ No newline at end of file diff --git a/src/Proto.Actor/Exceptions.cs b/src/Proto.Actor/Exceptions.cs index bcfe171470..283e5d2e01 100644 --- a/src/Proto.Actor/Exceptions.cs +++ b/src/Proto.Actor/Exceptions.cs @@ -4,13 +4,13 @@ namespace Proto { public class ProcessNameExistException : Exception { - public string Name { get; } - public PID Pid { get; } - public ProcessNameExistException(string name, PID pid) : base($"a Process with the name '{name}' already exists") { Name = name; Pid = pid; } + + public string Name { get; } + public PID Pid { get; } } } \ No newline at end of file diff --git a/src/Proto.Actor/Extensions.cs b/src/Proto.Actor/Extensions.cs index e8e2836c76..ee57811164 100644 --- a/src/Proto.Actor/Extensions.cs +++ b/src/Proto.Actor/Extensions.cs @@ -4,7 +4,6 @@ // // ----------------------------------------------------------------------- -using System; using System.Collections.Generic; using Proto.Mailbox; @@ -13,13 +12,13 @@ namespace Proto public static class Extensions { public static void Stop(this IEnumerable self) - { + { foreach (var pid in self) { RootContext.Empty.Stop(pid); } } - + public static void SendSystemMessage(this IEnumerable self, SystemMessage message) { foreach (var pid in self) @@ -27,15 +26,9 @@ public static void SendSystemMessage(this IEnumerable self, SystemMessage m pid.SendSystemMessage(message); } } - - - [Obsolete("Replaced with Context.Send(msg)", false)] - public static void Tell(this PID self, object message) - { - self.SendUserMessage(message); - } - - public static void Deconstruct(this KeyValuePair self, out TKey key, out TValue value) + + public static void Deconstruct(this KeyValuePair self, out TKey key, + out TValue value) { key = self.Key; value = self.Value; diff --git a/src/Proto.Actor/Futures.cs b/src/Proto.Actor/Futures.cs index 59ec5eb7c2..02458dae45 100644 --- a/src/Proto.Actor/Futures.cs +++ b/src/Proto.Actor/Futures.cs @@ -15,13 +15,19 @@ internal class FutureProcess : Process private readonly CancellationTokenSource _cts; private readonly TaskCompletionSource _tcs; - internal FutureProcess(TimeSpan timeout) : this(new CancellationTokenSource(timeout)) { } + internal FutureProcess(TimeSpan timeout) : this(new CancellationTokenSource(timeout)) + { + } internal FutureProcess(CancellationToken cancellationToken) : this( CancellationTokenSource.CreateLinkedTokenSource(cancellationToken) - ) { } + ) + { + } - internal FutureProcess() : this(null) { } + internal FutureProcess() : this(null) + { + } private FutureProcess(CancellationTokenSource? cts) { diff --git a/src/Proto.Actor/Guardians.cs b/src/Proto.Actor/Guardians.cs index 78de4f6f62..1b5ae7d74a 100644 --- a/src/Proto.Actor/Guardians.cs +++ b/src/Proto.Actor/Guardians.cs @@ -62,7 +62,7 @@ public void RestartChildren(Exception reason, params PID[] pids) => protected internal override void SendUserMessage(PID pid, object message) { - throw new InvalidOperationException($"Guardian actor cannot receive any user messages."); + throw new InvalidOperationException("Guardian actor cannot receive any user messages."); } protected internal override void SendSystemMessage(PID pid, object message) diff --git a/src/Proto.Actor/HashedConcurrentDictionary.cs b/src/Proto.Actor/HashedConcurrentDictionary.cs index 8b6d6f6419..a05673aa4a 100644 --- a/src/Proto.Actor/HashedConcurrentDictionary.cs +++ b/src/Proto.Actor/HashedConcurrentDictionary.cs @@ -49,6 +49,7 @@ public bool TryAdd(string key, Process reff) { return false; } + p.Add(key, reff); return true; } @@ -73,6 +74,7 @@ public void Remove(string key) } public class Partition : Dictionary - {} + { + } } } \ No newline at end of file diff --git a/src/Proto.Actor/IContext.cs b/src/Proto.Actor/IContext.cs index 59e9c812e4..68f2d5365b 100644 --- a/src/Proto.Actor/IContext.cs +++ b/src/Proto.Actor/IContext.cs @@ -54,20 +54,25 @@ public interface IContext : ISenderContext, IReceiverContext, ISpawnerContext, I void SetReceiveTimeout(TimeSpan duration); void CancelReceiveTimeout(); - + void Forward(PID target); /// - /// Awaits the given target task and once completed, the given action is then completed within the actors concurrency constraint. - /// The concept is called Reentrancy, where an actor can continue to process messages while also awaiting that some asynchronous operation completes. + /// Awaits the given target task and once completed, the given action is then completed within the actors concurrency + /// constraint. + /// The concept is called Reentrancy, where an actor can continue to process messages while also awaiting that some + /// asynchronous operation completes. /// /// the Task to await /// the continuation to call once the task is completed /// The generic type of the task void ReenterAfter(Task target, Func, Task> action); + /// - /// Awaits the given target task and once completed, the given action is then completed within the actors concurrency constraint. - /// The concept is called Reentrancy, where an actor can continue to process messages while also awaiting that some asynchronous operation completes. + /// Awaits the given target task and once completed, the given action is then completed within the actors concurrency + /// constraint. + /// The concept is called Reentrancy, where an actor can continue to process messages while also awaiting that some + /// asynchronous operation completes. /// /// the Task to await /// the continuation to call once the task is completed diff --git a/src/Proto.Actor/IInfoContext.cs b/src/Proto.Actor/IInfoContext.cs index 60d248e9a6..70d2059047 100644 --- a/src/Proto.Actor/IInfoContext.cs +++ b/src/Proto.Actor/IInfoContext.cs @@ -4,10 +4,6 @@ // // ----------------------------------------------------------------------- -using System; -using System.Collections.Generic; -using System.Threading.Tasks; - namespace Proto { public interface IInfoContext diff --git a/src/Proto.Actor/IReceiverContext.cs b/src/Proto.Actor/IReceiverContext.cs index 58d65589db..d9e36976d3 100644 --- a/src/Proto.Actor/IReceiverContext.cs +++ b/src/Proto.Actor/IReceiverContext.cs @@ -2,7 +2,7 @@ namespace Proto { - public interface IReceiverContext: IInfoContext + public interface IReceiverContext : IInfoContext { Task Receive(MessageEnvelope envelope); } diff --git a/src/Proto.Actor/ISenderContext.cs b/src/Proto.Actor/ISenderContext.cs index 8245b545dd..a13e10e7fe 100644 --- a/src/Proto.Actor/ISenderContext.cs +++ b/src/Proto.Actor/ISenderContext.cs @@ -3,38 +3,47 @@ // Copyright (C) 2015-2018 Asynkron HB All rights reserved // // ----------------------------------------------------------------------- + using System; using System.Threading; using System.Threading.Tasks; namespace Proto { - public interface ISenderContext: IInfoContext + public interface ISenderContext : IInfoContext { /// - /// Send a message to a given PID target + /// MessageHeaders of the Context + /// + MessageHeader Headers { get; } + + //TODO: should the current message of the actor be exposed to sender middleware? + object Message { get; } + + /// + /// Send a message to a given PID target /// /// The target PID /// The message to send void Send(PID target, object message); /// - /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender + /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender /// /// The target PID /// The message to send void Request(PID target, object message); /// - /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender + /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender /// /// The target PID /// The message to send void Request(PID target, object message, PID sender); /// - /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. - /// This operation can be awaited. + /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. + /// This operation can be awaited. /// /// The target PID /// The message to send @@ -42,9 +51,10 @@ public interface ISenderContext: IInfoContext /// Expected return message type /// A Task that completes once the Target Responds back to the Sender Task RequestAsync(PID target, object message, TimeSpan timeout); + /// - /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. - /// This operation can be awaited. + /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. + /// This operation can be awaited. /// /// The target PID /// The message to send @@ -52,23 +62,15 @@ public interface ISenderContext: IInfoContext /// Expected return message type /// A Task that completes once the Target Responds back to the Sender Task RequestAsync(PID target, object message, CancellationToken cancellationToken); + /// - /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. - /// This operation can be awaited. + /// Sends a message together with a Sender PID, this allows the target to respond async to the Sender. + /// This operation can be awaited. /// /// The target PID /// The message to send /// Expected return message type /// A Task that completes once the Target Responds back to the Sender Task RequestAsync(PID target, object message); - - /// - /// MessageHeaders of the Context - /// - MessageHeader Headers { get; } - - //TODO: should the current message of the actor be exposed to sender middleware? - object Message { get; } - } } \ No newline at end of file diff --git a/src/Proto.Actor/ISpawnContext.cs b/src/Proto.Actor/ISpawnContext.cs index 8f544ae309..19a8c92012 100644 --- a/src/Proto.Actor/ISpawnContext.cs +++ b/src/Proto.Actor/ISpawnContext.cs @@ -8,7 +8,7 @@ public interface ISpawnContext /// The Props used to spawn the actor /// The PID of the child actor PID Spawn(Props props); - + /// /// Spawns a new child actor based on props and named using the specified name. /// @@ -16,7 +16,7 @@ public interface ISpawnContext /// The actor name /// The PID of the child actor PID SpawnNamed(Props props, string name); - + /// /// Spawns a new child actor based on props and named using a prefix followed by a unique ID. /// diff --git a/src/Proto.Actor/ISpawnerContext.cs b/src/Proto.Actor/ISpawnerContext.cs index 7bcd6b8066..08aa0f19e0 100644 --- a/src/Proto.Actor/ISpawnerContext.cs +++ b/src/Proto.Actor/ISpawnerContext.cs @@ -8,7 +8,7 @@ public interface ISpawnerContext /// The Props used to spawn the actor /// The PID of the child actor PID Spawn(Props props); - + /// /// Spawns a new child actor based on props and named using the specified name. /// @@ -16,7 +16,7 @@ public interface ISpawnerContext /// The actor name /// The PID of the child actor PID SpawnNamed(Props props, string name); - + /// /// Spawns a new child actor based on props and named using a prefix followed by a unique ID. /// diff --git a/src/Proto.Actor/MessageEnvelope.cs b/src/Proto.Actor/MessageEnvelope.cs index c67b09f7c9..e75aefaaab 100644 --- a/src/Proto.Actor/MessageEnvelope.cs +++ b/src/Proto.Actor/MessageEnvelope.cs @@ -14,24 +14,25 @@ public class MessageEnvelope { public MessageEnvelope(object message, PID? sender, MessageHeader? header) { - Sender = sender; + Sender = sender; Message = message; Header = header; } + public PID? Sender { get; } + public object Message { get; } + public MessageHeader? Header { get; } + public static MessageEnvelope Wrap(object message) { if (message is MessageEnvelope env) { return env; } + return new MessageEnvelope(message, null, null); } - public PID? Sender { get; } - public object Message { get; } - public MessageHeader? Header { get; } - public MessageEnvelope WithSender(PID sender) => new MessageEnvelope(Message, sender, Header); public MessageEnvelope WithMessage(object message) => new MessageEnvelope(message, Sender, Header); @@ -43,12 +44,11 @@ public MessageEnvelope WithHeader(string key, string value) var header = (Header ?? new MessageHeader()).With(key, value); return new MessageEnvelope(Message, Sender, header); } - + public MessageEnvelope WithHeaders(IEnumerable> items) { var header = (Header ?? new MessageHeader()).With(items); return new MessageEnvelope(Message, Sender, header); - } public static (object message, PID? sender, MessageHeader? headers) Unwrap(object message) @@ -60,7 +60,7 @@ public static (object message, PID? sender, MessageHeader? headers) Unwrap(objec return (message, null, null); } - + [MethodImpl(MethodImplOptions.AggressiveInlining)] public static MessageHeader UnwrapHeader(object message) => (message as MessageEnvelope)?.Header; diff --git a/src/Proto.Actor/MessageExtensions.cs b/src/Proto.Actor/MessageExtensions.cs index 1e2c6a9523..95415b91a0 100644 --- a/src/Proto.Actor/MessageExtensions.cs +++ b/src/Proto.Actor/MessageExtensions.cs @@ -6,7 +6,7 @@ public static Terminated From(PID who) => new Terminated { Who = who, - AddressTerminated = false, + AddressTerminated = false }; } } \ No newline at end of file diff --git a/src/Proto.Actor/MessageHeader.cs b/src/Proto.Actor/MessageHeader.cs index 6ced292a55..6375871422 100644 --- a/src/Proto.Actor/MessageHeader.cs +++ b/src/Proto.Actor/MessageHeader.cs @@ -12,27 +12,17 @@ namespace Proto { public class MessageHeader : IReadOnlyDictionary { + public static readonly MessageHeader Empty = new MessageHeader(); private readonly ImmutableDictionary _inner; - public readonly static MessageHeader Empty = new MessageHeader(); - public IDictionary ToDictionary() => _inner; - - public MessageHeader() => _inner = ImmutableDictionary.Empty; - - public MessageHeader(IDictionary headers) => _inner = headers.ToImmutableDictionary(); - - public string GetOrDefault(string key, string? @default = null) => TryGetValue(key, out var value) ? value : @default; - - public MessageHeader With(string key, string value) + public MessageHeader() { - var copy = _inner.SetItem(key, value); - return new MessageHeader(copy); + _inner = ImmutableDictionary.Empty; } - public MessageHeader With(IEnumerable> items) + public MessageHeader(IDictionary headers) { - var copy = _inner.SetItems(items); - return new MessageHeader(copy); + _inner = headers.ToImmutableDictionary(); } public IEnumerator> GetEnumerator() => _inner.GetEnumerator(); @@ -46,5 +36,22 @@ public MessageHeader With(IEnumerable> items) public IEnumerable Keys => _inner.Keys; public IEnumerable Values => _inner.Values; + + public IDictionary ToDictionary() => _inner; + + public string GetOrDefault(string key, string? @default = null) => + TryGetValue(key, out var value) ? value : @default; + + public MessageHeader With(string key, string value) + { + var copy = _inner.SetItem(key, value); + return new MessageHeader(copy); + } + + public MessageHeader With(IEnumerable> items) + { + var copy = _inner.SetItems(items); + return new MessageHeader(copy); + } } } \ No newline at end of file diff --git a/src/Proto.Actor/Messages.cs b/src/Proto.Actor/Messages.cs index eb837da36d..0b59879bde 100644 --- a/src/Proto.Actor/Messages.cs +++ b/src/Proto.Actor/Messages.cs @@ -10,15 +10,21 @@ namespace Proto { - public abstract class AutoReceiveMessage { } + public abstract class AutoReceiveMessage + { + } - public sealed partial class Terminated : SystemMessage { } + public sealed partial class Terminated : SystemMessage + { + } public sealed class Restarting { public static readonly Restarting Instance = new Restarting(); - private Restarting() { } + private Restarting() + { + } } public class Failure : SystemMessage @@ -39,17 +45,26 @@ public Failure(PID who, Exception reason, RestartStatistics crs, object message) public sealed partial class Watch : SystemMessage { - public Watch(PID watcher) => Watcher = watcher; + public Watch(PID watcher) + { + Watcher = watcher; + } } public sealed partial class Unwatch : SystemMessage { - public Unwatch(PID watcher) => Watcher = watcher; + public Unwatch(PID watcher) + { + Watcher = watcher; + } } public sealed class Restart : SystemMessage { - public Restart(Exception reason) => Reason = reason; + public Restart(Exception reason) + { + Reason = reason; + } public Exception Reason { get; } } @@ -63,31 +78,41 @@ public sealed class Stopping : AutoReceiveMessage { public static readonly Stopping Instance = new Stopping(); - private Stopping() { } + private Stopping() + { + } } public sealed class Started : SystemMessage { public static readonly Started Instance = new Started(); - private Started() { } + private Started() + { + } } public sealed class Stopped : AutoReceiveMessage { public static readonly Stopped Instance = new Stopped(); - private Stopped() { } + private Stopped() + { + } } public class ReceiveTimeout : SystemMessage { public static readonly ReceiveTimeout Instance = new ReceiveTimeout(); - private ReceiveTimeout() { } + private ReceiveTimeout() + { + } } - public interface INotInfluenceReceiveTimeout { } + public interface INotInfluenceReceiveTimeout + { + } public class Continuation : SystemMessage { diff --git a/src/Proto.Actor/NullLoggerFactory.cs b/src/Proto.Actor/NullLoggerFactory.cs index 310b6587de..759949f6cf 100644 --- a/src/Proto.Actor/NullLoggerFactory.cs +++ b/src/Proto.Actor/NullLoggerFactory.cs @@ -15,8 +15,12 @@ public class NullLoggerFactory : ILoggerFactory public ILogger CreateLogger(string name) => NullLogger.Instance; - public void AddProvider(ILoggerProvider provider) { } + public void AddProvider(ILoggerProvider provider) + { + } - public void Dispose() { } + public void Dispose() + { + } } } \ No newline at end of file diff --git a/src/Proto.Actor/PID.cs b/src/Proto.Actor/PID.cs index b978e6e5a8..a562aa1bc1 100644 --- a/src/Proto.Actor/PID.cs +++ b/src/Proto.Actor/PID.cs @@ -4,10 +4,6 @@ // // ----------------------------------------------------------------------- -using System; -using System.Threading; -using System.Threading.Tasks; - namespace Proto { // ReSharper disable once InconsistentNaming @@ -21,7 +17,10 @@ public PID(string address, string id) Id = id; } - internal PID(string address, string id, Process process) : this(address, id) => _process = process; + internal PID(string address, string id, Process process) : this(address, id) + { + _process = process; + } internal Process? Ref { @@ -34,6 +33,7 @@ internal Process? Ref { _process = null; } + return _process; } @@ -59,38 +59,6 @@ public void SendSystemMessage(object sys) reff.SendSystemMessage(this, sys); } - [Obsolete("Replaced with Context.Stop(pid)", false)] - public void Stop() - { - var reff = ProcessRegistry.Instance.Get(this); - reff.Stop(this); - } - - [Obsolete("Replaced with Context.StopAsync(pid)", false)] - public Task StopAsync() - { - var future = new FutureProcess(); - - SendSystemMessage(new Watch(future.Pid)); - Stop(); - - return future.Task; - } - - [Obsolete("Replaced with Context.Poison(pid)", false)] - public void Poison() => SendUserMessage(new PoisonPill()); - - [Obsolete("Replaced with Context.PoisonAsync(pid)", false)] - public Task PoisonAsync() - { - var future = new FutureProcess(); - - SendSystemMessage(new Watch(future.Pid)); - Poison(); - - return future.Task; - } - public string ToShortString() => Address + "/" + Id; } } \ No newline at end of file diff --git a/src/Proto.Actor/Process.cs b/src/Proto.Actor/Process.cs index f6343cb9dd..f9c7efdf88 100644 --- a/src/Proto.Actor/Process.cs +++ b/src/Proto.Actor/Process.cs @@ -14,7 +14,7 @@ public abstract class Process protected internal abstract void SendUserMessage(PID pid, object message); protected internal abstract void SendSystemMessage(PID pid, object message); - + public virtual void Stop(PID pid) => SendSystemMessage(pid, Proto.Stop.Instance); } @@ -22,7 +22,10 @@ public class ActorProcess : Process { private long _isDead; - public ActorProcess(IMailbox mailbox) => Mailbox = mailbox; + public ActorProcess(IMailbox mailbox) + { + Mailbox = mailbox; + } public IMailbox Mailbox { get; } @@ -34,7 +37,8 @@ internal bool IsDead protected internal override void SendUserMessage(PID pid, object message) => Mailbox.PostUserMessage(message); - protected internal override void SendSystemMessage(PID pid, object message) => Mailbox.PostSystemMessage(message); + protected internal override void SendSystemMessage(PID pid, object message) => + Mailbox.PostSystemMessage(message); public override void Stop(PID pid) { diff --git a/src/Proto.Actor/ProcessRegistry.cs b/src/Proto.Actor/ProcessRegistry.cs index f0ea165de9..779c6fb0dd 100644 --- a/src/Proto.Actor/ProcessRegistry.cs +++ b/src/Proto.Actor/ProcessRegistry.cs @@ -16,15 +16,15 @@ public class ProcessRegistry private const string NoHost = "nonhost"; private readonly IList> _hostResolvers = new List>(); private readonly HashedConcurrentDictionary _localActorRefs = new HashedConcurrentDictionary(); - - private int _sequenceId; private string _host; private int _port; + private int _sequenceId; + public static ProcessRegistry Instance { get; } = new ProcessRegistry(); public string Address { get; private set; } = NoHost; - + public void RegisterHostResolver(Func resolver) => _hostResolvers.Add(resolver); public Process Get(PID pid) @@ -34,7 +34,9 @@ public Process Get(PID pid) var reff = _hostResolvers.Select(x => x(pid)).FirstOrDefault(); if (reff == null) + { throw new NotSupportedException("Unknown host"); + } return reff; } diff --git a/src/Proto.Actor/Props.cs b/src/Proto.Actor/Props.cs index e439184c80..451d93d5ca 100644 --- a/src/Proto.Actor/Props.cs +++ b/src/Proto.Actor/Props.cs @@ -4,20 +4,17 @@ // // ----------------------------------------------------------------------- -using Proto.Mailbox; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using Proto.Mailbox; namespace Proto { - static class Middleware + internal static class Middleware { - internal static Task Receive(IReceiverContext context, MessageEnvelope envelope) - { - return context.Receive(envelope); - } + internal static Task Receive(IReceiverContext context, MessageEnvelope envelope) => context.Receive(envelope); internal static Task Sender(ISenderContext context, PID target, MessageEnvelope envelope) { @@ -25,6 +22,7 @@ internal static Task Sender(ISenderContext context, PID target, MessageEnvelope return Actor.Done; } } + public sealed class Props { private Spawner? _spawner; @@ -33,11 +31,17 @@ public sealed class Props public ISupervisorStrategy GuardianStrategy { get; private set; } public ISupervisorStrategy SupervisorStrategy { get; private set; } = Supervision.DefaultStrategy; public IDispatcher Dispatcher { get; private set; } = Dispatchers.DefaultDispatcher; - public IList> ReceiverMiddleware { get; private set; } = new List>(); + + public IList> ReceiverMiddleware { get; private set; } = + new List>(); + public IList> SenderMiddleware { get; private set; } = new List>(); public Receiver ReceiverMiddlewareChain { get; private set; } public Sender SenderMiddlewareChain { get; private set; } - public IList> ContextDecorator { get; private set; } = new List>(); + + public IList> ContextDecorator { get; private set; } = + new List>(); + public Func? ContextDecoratorChain { get; private set; } public Spawner Spawner @@ -60,6 +64,7 @@ private static PID DefaultSpawner(string name, Props props, PID parent) { throw new ProcessNameExistException(name, pid); } + var ctx = new ActorContext(props, parent, pid); mailbox.RegisterHandlers(ctx, dispatcher); mailbox.PostSystemMessage(Started.Instance); @@ -72,32 +77,40 @@ private static PID DefaultSpawner(string name, Props props, PID parent) public Props WithDispatcher(IDispatcher dispatcher) => Copy(props => props.Dispatcher = dispatcher); - public Props WithMailbox(Func mailboxProducer) => Copy(props => props.MailboxProducer = mailboxProducer); + public Props WithMailbox(Func mailboxProducer) => + Copy(props => props.MailboxProducer = mailboxProducer); public Props WithContextDecorator(params Func[] contextDecorator) => Copy(props => - { - props.ContextDecorator = ContextDecorator.Concat(contextDecorator).ToList(); - props.ContextDecoratorChain = props.ContextDecorator.Reverse() - .Aggregate((Func)DefaultContextDecorator, (inner, outer) => ctx => outer(inner(ctx))); - }); + { + props.ContextDecorator = ContextDecorator.Concat(contextDecorator).ToList(); + props.ContextDecoratorChain = props.ContextDecorator.Reverse() + .Aggregate((Func) DefaultContextDecorator, + (inner, outer) => ctx => outer(inner(ctx)) + ); + } + ); - public Props WithGuardianSupervisorStrategy(ISupervisorStrategy guardianStrategy) => Copy(props => props.GuardianStrategy = guardianStrategy); + public Props WithGuardianSupervisorStrategy(ISupervisorStrategy guardianStrategy) => + Copy(props => props.GuardianStrategy = guardianStrategy); - public Props WithChildSupervisorStrategy(ISupervisorStrategy supervisorStrategy) => Copy(props => props.SupervisorStrategy = supervisorStrategy); + public Props WithChildSupervisorStrategy(ISupervisorStrategy supervisorStrategy) => + Copy(props => props.SupervisorStrategy = supervisorStrategy); public Props WithReceiverMiddleware(params Func[] middleware) => Copy(props => - { - props.ReceiverMiddleware = ReceiverMiddleware.Concat(middleware).ToList(); - props.ReceiverMiddlewareChain = props.ReceiverMiddleware.Reverse() - .Aggregate((Receiver)Middleware.Receive, (inner, outer) => outer(inner)); - }); + { + props.ReceiverMiddleware = ReceiverMiddleware.Concat(middleware).ToList(); + props.ReceiverMiddlewareChain = props.ReceiverMiddleware.Reverse() + .Aggregate((Receiver) Middleware.Receive, (inner, outer) => outer(inner)); + } + ); public Props WithSenderMiddleware(params Func[] middleware) => Copy(props => - { - props.SenderMiddleware = SenderMiddleware.Concat(middleware).ToList(); - props.SenderMiddlewareChain = props.SenderMiddleware.Reverse() - .Aggregate((Sender)Middleware.Sender, (inner, outer) => outer(inner)); - }); + { + props.SenderMiddleware = SenderMiddleware.Concat(middleware).ToList(); + props.SenderMiddlewareChain = props.SenderMiddleware.Reverse() + .Aggregate((Sender) Middleware.Sender, (inner, outer) => outer(inner)); + } + ); public Props WithSpawner(Spawner spawner) => Copy(props => props.Spawner = spawner); @@ -116,7 +129,7 @@ private Props Copy(Action mutator) SupervisorStrategy = SupervisorStrategy, GuardianStrategy = GuardianStrategy, ContextDecorator = ContextDecorator, - ContextDecoratorChain = ContextDecoratorChain, + ContextDecoratorChain = ContextDecoratorChain }; mutator(props); return props; diff --git a/src/Proto.Actor/RestartStatistics.cs b/src/Proto.Actor/RestartStatistics.cs index 0e2aac77c5..56fccc7575 100644 --- a/src/Proto.Actor/RestartStatistics.cs +++ b/src/Proto.Actor/RestartStatistics.cs @@ -13,17 +13,17 @@ namespace Proto public class RestartStatistics { private readonly List _failureTimes = new List(); - - public int FailureCount => _failureTimes.Count; public RestartStatistics(int failureCount, DateTime? lastFailuretime) { - for (int i = 0; i < failureCount; i++) + for (var i = 0; i < failureCount; i++) { _failureTimes.Add(lastFailuretime ?? DateTime.Now); } } + public int FailureCount => _failureTimes.Count; + public void Fail() => _failureTimes.Add(DateTime.Now); public void Reset() => _failureTimes.Clear(); diff --git a/src/Proto.Actor/RootContext.cs b/src/Proto.Actor/RootContext.cs index 791f49fb50..da2ab11a2d 100644 --- a/src/Proto.Actor/RootContext.cs +++ b/src/Proto.Actor/RootContext.cs @@ -18,6 +18,20 @@ public interface IRootContext : ISpawnerContext, ISenderContext, IStopperContext public class RootContext : IRootContext { public static readonly RootContext Empty = new RootContext(); + + public RootContext() + { + SenderMiddleware = null; + Headers = MessageHeader.Empty; + } + + public RootContext(MessageHeader messageHeader, params Func[] middleware) + { + SenderMiddleware = middleware.Reverse() + .Aggregate((Sender) DefaultSender, (inner, outer) => outer(inner)); + Headers = messageHeader; + } + private Sender SenderMiddleware { get; set; } public MessageHeader Headers { get; private set; } @@ -44,47 +58,8 @@ public PID SpawnPrefix(Props props, string prefix) return SpawnNamed(props, name); } - public RootContext() - { - SenderMiddleware = null; - Headers = MessageHeader.Empty; - } - - public RootContext(MessageHeader messageHeader, params Func[] middleware) - { - SenderMiddleware = middleware.Reverse() - .Aggregate((Sender)DefaultSender, (inner, outer) => outer(inner)); - Headers = messageHeader; - } - - public RootContext WithHeaders(MessageHeader headers) => Copy(c => c.Headers = headers); - public RootContext WithSenderMiddleware(params Func[] middleware) => Copy(c => - { - SenderMiddleware = middleware.Reverse() - .Aggregate((Sender)DefaultSender, (inner, outer) => outer(inner)); - }); - - - private RootContext Copy(Action mutator) - { - var copy = new RootContext - { - SenderMiddleware = SenderMiddleware, - Headers = Headers - }; - mutator(copy); - return copy; - } - public object Message => null; - - private Task DefaultSender(ISenderContext context, PID target, MessageEnvelope message) - { - target.SendUserMessage(message); - return Proto.Actor.Done; - } - public void Send(PID target, object message) => SendUserMessage(target, message); @@ -106,28 +81,6 @@ public Task RequestAsync(PID target, object message, CancellationToken can public Task RequestAsync(PID target, object message) => RequestAsync(target, message, new FutureProcess()); - private Task RequestAsync(PID target, object message, FutureProcess future) - { - var messageEnvelope = new MessageEnvelope(message, future.Pid, null); - SendUserMessage(target, messageEnvelope); - - return future.Task; - } - - private void SendUserMessage(PID target, object message) - { - if (SenderMiddleware != null) - { - //slow path - SenderMiddleware(this, target, MessageEnvelope.Wrap(message)); - } - else - { - //fast path, 0 alloc - target.SendUserMessage(message); - } - } - public void Stop(PID pid) { var reff = ProcessRegistry.Instance.Get(pid); @@ -151,9 +104,59 @@ public Task PoisonAsync(PID pid) var future = new FutureProcess(); pid.SendSystemMessage(new Watch(future.Pid)); - Poison(pid); + Poison(pid); return future.Task; } + + public RootContext WithHeaders(MessageHeader headers) => Copy(c => c.Headers = headers); + + public RootContext WithSenderMiddleware(params Func[] middleware) => Copy(c => + { + SenderMiddleware = middleware.Reverse() + .Aggregate((Sender) DefaultSender, (inner, outer) => outer(inner)); + } + ); + + + private RootContext Copy(Action mutator) + { + var copy = new RootContext + { + SenderMiddleware = SenderMiddleware, + Headers = Headers + }; + mutator(copy); + return copy; + } + + + private Task DefaultSender(ISenderContext context, PID target, MessageEnvelope message) + { + target.SendUserMessage(message); + return Proto.Actor.Done; + } + + private Task RequestAsync(PID target, object message, FutureProcess future) + { + var messageEnvelope = new MessageEnvelope(message, future.Pid, null); + SendUserMessage(target, messageEnvelope); + + return future.Task; + } + + private void SendUserMessage(PID target, object message) + { + if (SenderMiddleware != null) + { + //slow path + SenderMiddleware(this, target, MessageEnvelope.Wrap(message)); + } + else + { + //fast path, 0 alloc + target.SendUserMessage(message); + } + } } -} +} \ No newline at end of file diff --git a/src/Proto.Actor/RootContextDecorator.cs b/src/Proto.Actor/RootContextDecorator.cs index b4401ccc86..bab85856fe 100644 --- a/src/Proto.Actor/RootContextDecorator.cs +++ b/src/Proto.Actor/RootContextDecorator.cs @@ -23,7 +23,8 @@ protected RootContextDecorator(IRootContext context) public virtual void Request(PID target, object message) => _context.Request(target, message); - public virtual void Request(PID target, object message, PID sender) => _context.Request(target, message, sender); + public virtual void Request(PID target, object message, PID sender) => + _context.Request(target, message, sender); public virtual Task RequestAsync(PID target, object message, TimeSpan timeout) => _context.RequestAsync(target, message, timeout); diff --git a/src/Proto.Actor/Supervision.cs b/src/Proto.Actor/Supervision.cs index 60e5904296..c83c9036a7 100644 --- a/src/Proto.Actor/Supervision.cs +++ b/src/Proto.Actor/Supervision.cs @@ -8,7 +8,6 @@ using System.Collections.Immutable; using System.Linq; using System.Threading.Tasks; -using System.Xml.Schema; using Microsoft.Extensions.Logging; namespace Proto @@ -34,6 +33,7 @@ public static class Supervision { public static ISupervisorStrategy DefaultStrategy { get; } = new OneForOneStrategy((who, reason) => SupervisorDirective.Restart, 10, TimeSpan.FromSeconds(10)); + public static ISupervisorStrategy AlwaysRestartStrategy { get; } = new AlwaysRestartStrategy(); } @@ -45,11 +45,10 @@ public interface ISupervisorStrategy public delegate SupervisorDirective Decider(PID pid, Exception reason); /// - /// AllForOneStrategy returns a new SupervisorStrategy which applies the given fault Directive from the decider to the - /// failing child and all its children. - /// - /// This strategy is appropriate when the children have a strong dependency, such that and any single one failing would - /// place them all into a potentially invalid state. + /// AllForOneStrategy returns a new SupervisorStrategy which applies the given fault Directive from the decider to the + /// failing child and all its children. + /// This strategy is appropriate when the children have a strong dependency, such that and any single one failing would + /// place them all into a potentially invalid state. /// public class AllForOneStrategy : ISupervisorStrategy { @@ -65,7 +64,8 @@ public AllForOneStrategy(Decider decider, int maxNrOfRetries, TimeSpan? withinTi _withinTimeSpan = withinTimeSpan; } - public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, object message) + public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, + object message) { var directive = _decider(child, reason); @@ -86,6 +86,7 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r LogInfo("Restarting"); supervisor.RestartChildren(reason, supervisor.Children.ToArray()); } + break; case SupervisorDirective.Stop: LogInfo("Stopping"); @@ -98,7 +99,9 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r throw new ArgumentOutOfRangeException(); } - void LogInfo(string action) => Logger.LogInformation("{Action} {Actor} because of {Reason}", action, child.ToShortString(), reason); + void LogInfo(string action) => Logger.LogInformation("{Action} {Actor} because of {Reason}", action, + child.ToShortString(), reason + ); } private bool ShouldStop(RestartStatistics rs) @@ -135,7 +138,8 @@ public OneForOneStrategy(Decider decider, int maxNrOfRetries, TimeSpan? withinTi _withinTimeSpan = withinTimeSpan; } - public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, object message) + public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, + object message) { var directive = _decider(child, reason); @@ -156,6 +160,7 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r LogInfo("Restarting"); supervisor.RestartChildren(reason, child); } + break; case SupervisorDirective.Stop: LogInfo("Stopping"); @@ -168,7 +173,9 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r throw new ArgumentOutOfRangeException(); } - void LogInfo(string action) => Logger.LogInformation("{Action} {Actor} because of {Reason}", action, child.ToShortString(), reason); + void LogInfo(string action) => Logger.LogInformation("{Action} {Actor} because of {Reason}", action, + child.ToShortString(), reason + ); } private bool ShouldStop(RestartStatistics rs) @@ -202,7 +209,8 @@ public ExponentialBackoffStrategy(TimeSpan backoffWindow, TimeSpan initialBackof _initialBackoff = initialBackoff; } - public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, object message) + public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, + object message) { if (rs.NumberOfFailures(_backoffWindow) == 0) { @@ -225,7 +233,8 @@ public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics r public class AlwaysRestartStrategy : ISupervisorStrategy { //always restart - public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, object message) + public void HandleFailure(ISupervisor supervisor, PID child, RestartStatistics rs, Exception reason, + object message) => supervisor.RestartChildren(reason, child); } } \ No newline at end of file diff --git a/src/Proto.Remote/RemoteConfig.cs b/src/Proto.Remote/RemoteConfig.cs index 1bcdbeb44c..abf9efe7a6 100644 --- a/src/Proto.Remote/RemoteConfig.cs +++ b/src/Proto.Remote/RemoteConfig.cs @@ -13,11 +13,7 @@ namespace Proto.Remote public class RemoteConfig { - [Obsolete("Use EndpointWriterOptions.EndpointWriterBatchSize instead")] - public int EndpointWriterBatchSize { - get => EndpointWriterOptions.EndpointWriterBatchSize; - set => EndpointWriterOptions.EndpointWriterBatchSize = value; - } + /// /// Gets or sets the ChannelOptions for the gRPC channel.