From 60753fdde08f9242bb470456b6fd05cf60eddee3 Mon Sep 17 00:00:00 2001 From: rogeralsing Date: Sat, 30 Jan 2016 19:42:07 +0100 Subject: [PATCH] Added missing convenience method to `FromConfig` to comply with the Scala API. ```csharp var router1 = ActorOf(FromConfig.Props(Props.Create())) ``` Scala counterpart: ```scala val router1: ActorRef = context.actorOf(FromConfig.props(Props[Worker]), "router1") ``` Also cleaned up the RouterConfig.cs file to comply with C#6 language features. As all RouterConfig classes uses surrogates for serialization, no such language features affect the serializability of the types. api approved api --- .../CoreAPISpec.ApproveCore.approved.txt | 1 + .../Akka.Remote/RemoteActorRefProvider.cs | 1 - src/core/Akka/Routing/RouterConfig.cs | 97 ++++++++----------- 3 files changed, 43 insertions(+), 56 deletions(-) diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt index 26fc9d46ce9..33be15258b7 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt @@ -3794,6 +3794,7 @@ namespace Akka.Routing public static Akka.Routing.FromConfig Instance { get; } public override Akka.Routing.Router CreateRouter(Akka.Actor.ActorSystem system) { } public override System.Collections.Generic.IEnumerable GetRoutees(Akka.Routing.RoutedActorCell routedActorCell) { } + public static Akka.Actor.Props Props(Akka.Actor.Props props) { } public override Akka.Util.ISurrogate ToSurrogate(Akka.Actor.ActorSystem system) { } public class FromConfigSurrogate : Akka.Util.ISurrogate { diff --git a/src/core/Akka.Remote/RemoteActorRefProvider.cs b/src/core/Akka.Remote/RemoteActorRefProvider.cs index caaf6484b77..cf3b4d73602 100644 --- a/src/core/Akka.Remote/RemoteActorRefProvider.cs +++ b/src/core/Akka.Remote/RemoteActorRefProvider.cs @@ -15,7 +15,6 @@ using Akka.Dispatch.SysMsg; using Akka.Event; using Akka.Remote.Configuration; -using Akka.Serialization; using Akka.Util.Internal; namespace Akka.Remote diff --git a/src/core/Akka/Routing/RouterConfig.cs b/src/core/Akka/Routing/RouterConfig.cs index bad1e59ca66..4365f650d9d 100644 --- a/src/core/Akka/Routing/RouterConfig.cs +++ b/src/core/Akka/Routing/RouterConfig.cs @@ -250,7 +250,7 @@ public bool Equals(Group other) { if (ReferenceEquals(null, other)) return false; if (ReferenceEquals(this, other)) return true; - return _paths.SequenceEqual(other._paths); + return Paths.SequenceEqual(other.Paths); } /// @@ -276,18 +276,13 @@ public override bool Equals(object obj) /// public override int GetHashCode() { - return (_paths != null ? _paths.GetHashCode() : 0); + return Paths?.GetHashCode() ?? 0; } - private readonly string[] _paths; - /// /// Retrieves the actor paths used by this router during routee selection. /// - public string[] Paths - { - get { return _paths; } - } + public string[] Paths { get; } /// /// Initializes a new instance of the class. @@ -299,7 +294,7 @@ public string[] Paths /// An enumeration of actor paths used by the group router. protected Group(IEnumerable paths) : base(Dispatchers.DefaultDispatcherId) { - _paths = paths.ToArray(); + Paths = paths.ToArray(); } /// @@ -315,7 +310,7 @@ protected Group(IEnumerable paths) : base(Dispatchers.DefaultDispatcherI protected Group(IEnumerable paths, string routerDispatcher) : base(routerDispatcher ?? Dispatchers.DefaultDispatcherId) { - _paths = paths.ToArray(); + Paths = paths.ToArray(); } /// @@ -329,7 +324,7 @@ protected Group(IEnumerable paths, string routerDispatcher) protected Group(IEnumerable routees) : base(Dispatchers.DefaultDispatcherId) { - _paths = routees.Select(x => x.Path.ToStringWithAddress()).ToArray(); + Paths = routees.Select(x => x.Path.ToStringWithAddress()).ToArray(); } internal Routee RouteeFor(string path, IActorContext context) @@ -388,9 +383,9 @@ public override Router CreateRouter(ActorSystem system) /// public override IEnumerable GetRoutees(RoutedActorCell routedActorCell) { - if (_paths == null) return new Routee[0]; + if (Paths == null) return new Routee[0]; return - _paths.Select(((ActorSystemImpl) routedActorCell.System).ActorSelection) + Paths.Select(((ActorSystemImpl) routedActorCell.System).ActorSelection) .Select(actor => new ActorSelectionRoutee(actor)); } @@ -418,10 +413,6 @@ public override bool Equals(RouterConfig other) /// public abstract class Pool : RouterConfig, IEquatable { - private readonly int _nrOfInstances; - private readonly bool _usePoolDispatcher; - private readonly Resizer _resizer; - private readonly SupervisorStrategy _supervisorStrategy; //TODO: add supervisor strategy to the equality compare /// @@ -462,7 +453,7 @@ public override int GetHashCode() { unchecked { - int hashCode = (Resizer != null ? Resizer.GetHashCode() : 0); + int hashCode = Resizer?.GetHashCode() ?? 0; hashCode = (hashCode*397) ^ UsePoolDispatcher.GetHashCode(); hashCode = (hashCode*397) ^ NrOfInstances; return hashCode; @@ -489,11 +480,11 @@ protected Pool(int nrOfInstances, Resizer resizer, SupervisorStrategy supervisor // OMG, if every member in Java is virtual - you must never call any members in a constructor!!1! // In all seriousness, without making these members virtual RemoteRouterConfig won't work // ReSharper disable DoNotCallOverridableMethodsInConstructor - _nrOfInstances = nrOfInstances; + NrOfInstances = nrOfInstances; - _resizer = resizer; - _supervisorStrategy = supervisorStrategy ?? DefaultStrategy; - _usePoolDispatcher = usePoolDispatcher; + Resizer = resizer; + SupervisorStrategy = supervisorStrategy ?? DefaultStrategy; + UsePoolDispatcher = usePoolDispatcher; } /// @@ -506,20 +497,17 @@ protected Pool(int nrOfInstances, Resizer resizer, SupervisorStrategy supervisor /// The configuration used to configure the pool. protected Pool(Config config) : base(Dispatchers.DefaultDispatcherId) { - _nrOfInstances = config.GetInt("nr-of-instances"); - _resizer = DefaultResizer.FromConfig(config); - _usePoolDispatcher = config.HasPath("pool-dispatcher"); - _supervisorStrategy = DefaultStrategy; + NrOfInstances = config.GetInt("nr-of-instances"); + Resizer = DefaultResizer.FromConfig(config); + UsePoolDispatcher = config.HasPath("pool-dispatcher"); + SupervisorStrategy = DefaultStrategy; // ReSharper restore DoNotCallOverridableMethodsInConstructor } /// /// Retrieves the number of routees associated with this pool. /// - public virtual int NrOfInstances - { - get { return _nrOfInstances; } - } + public virtual int NrOfInstances { get; } /// /// Used by the to determine the initial number of routees. @@ -539,26 +527,17 @@ public virtual int GetNrOfInstances(ActorSystem system) /// Retrieve whether or not to use the pool dispatcher. The dispatcher is defined in the /// 'pool-dispatcher' configuration property in the deployment section of the router. /// - public virtual bool UsePoolDispatcher - { - get { return _usePoolDispatcher; } - } + public virtual bool UsePoolDispatcher { get; } /// /// Retrieve the resizer to use when dynamically allocating routees to the pool. /// - public virtual Resizer Resizer - { - get { return _resizer; } - } + public virtual Resizer Resizer { get; } /// /// Retrieve the strategy to use when supervising the pool. /// - public virtual SupervisorStrategy SupervisorStrategy - { - get { return _supervisorStrategy; } - } + public virtual SupervisorStrategy SupervisorStrategy { get; } /// /// Creates a new configured to use the provided @@ -638,9 +617,9 @@ protected RouterConfig OverrideUnsetConfig(RouterConfig other) { Pool wssConf; var p = other as Pool; - if (SupervisorStrategy == null || (SupervisorStrategy.Equals(Pool.DefaultStrategy) && - !p.SupervisorStrategy.Equals(Pool.DefaultStrategy))) - wssConf = this.WithSupervisorStrategy(p.SupervisorStrategy); + if (SupervisorStrategy == null || (SupervisorStrategy.Equals(DefaultStrategy) && + !p.SupervisorStrategy.Equals(DefaultStrategy))) + wssConf = WithSupervisorStrategy(p.SupervisorStrategy); else wssConf = this; @@ -695,10 +674,7 @@ protected RouterConfig OverrideUnsetConfig(RouterConfig other) /// The default strategy used is with an decider. /// /// - public static SupervisorStrategy DefaultStrategy - { - get { return new OneForOneStrategy(10, TimeSpan.FromSeconds(10), Decider.From(Directive.Escalate)); } - } + public static SupervisorStrategy DefaultStrategy => new OneForOneStrategy(10, TimeSpan.FromSeconds(10), Decider.From(Directive.Escalate)); #endregion @@ -728,12 +704,26 @@ public static SupervisorStrategy DefaultStrategy /// public class FromConfig : RouterConfig { - private static readonly FromConfig _instance = new FromConfig(Dispatchers.DefaultDispatcherId); - private FromConfig(string routerDispatcher) : base(routerDispatcher) { } + /// + /// Enriches a with what what's stored in the router configuration. + /// + /// This is semantically the same as: + /// + /// props.WithRouter(FromConfig.Instance) + /// + /// + /// + /// The Props to enrich + /// + public static Props Props(Props props) + { + return props.WithRouter(Instance); + } + /// /// Retrieves a based on what's stored in the configuration. /// @@ -741,10 +731,7 @@ private FromConfig(string routerDispatcher) : base(routerDispatcher) /// This router is set to use the default dispatcher . /// /// - public static FromConfig Instance - { - get { return _instance; } - } + public static FromConfig Instance { get; } = new FromConfig(Dispatchers.DefaultDispatcherId); /// /// Creates a router that is responsible for routing messages to routees within the provided .