diff --git a/src/core/Akka/Routing/Broadcast.cs b/src/core/Akka/Routing/Broadcast.cs index d37ad6454e6..7e3c675931c 100644 --- a/src/core/Akka/Routing/Broadcast.cs +++ b/src/core/Akka/Routing/Broadcast.cs @@ -153,38 +153,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) + BroadcastPool wssConf; + + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) { - BroadcastPool wssConf; - - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); - - return wssConf; + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } /// diff --git a/src/core/Akka/Routing/ConsistentHash.cs b/src/core/Akka/Routing/ConsistentHash.cs index 4319b91188c..3da7dad1e6b 100644 --- a/src/core/Akka/Routing/ConsistentHash.cs +++ b/src/core/Akka/Routing/ConsistentHash.cs @@ -289,35 +289,39 @@ public ISurrogated FromSurrogate(ActorSystem system) /// The object encoded into bytes - in the case of custom classes, the hashcode may be used. internal static object ToBytesOrObject(object obj) { - if (obj == null) - return new byte[] { 0 }; - if (obj is byte[]) - return (byte[])obj; - if (obj is int) - return BitConverter.GetBytes((int)obj); - if (obj is uint) - return BitConverter.GetBytes((uint)obj); - if (obj is short) - return BitConverter.GetBytes((short)obj); - if (obj is ushort) - return BitConverter.GetBytes((ushort)obj); - if (obj is bool) - return BitConverter.GetBytes((bool)obj); - if (obj is long) - return BitConverter.GetBytes((long)obj); - if (obj is ulong) - return BitConverter.GetBytes((ulong)obj); - if (obj is char) - return BitConverter.GetBytes((char)obj); - if (obj is float) - return BitConverter.GetBytes((float)obj); - if (obj is double) - return BitConverter.GetBytes((double)obj); - if (obj is decimal) - return new BitArray(decimal.GetBits((decimal)obj)).ToBytes(); - if (obj is Guid) - return ((Guid)obj).ToByteArray(); - return obj; + switch (obj) + { + case null: + return new byte[] { 0 }; + case byte[] bytes: + return bytes; + case int @int: + return BitConverter.GetBytes(@int); + case uint @uint: + return BitConverter.GetBytes(@uint); + case short @short: + return BitConverter.GetBytes(@short); + case ushort @ushort: + return BitConverter.GetBytes(@ushort); + case bool @bool: + return BitConverter.GetBytes(@bool); + case long @long: + return BitConverter.GetBytes(@long); + case ulong @ulong: + return BitConverter.GetBytes(@ulong); + case char @char: + return BitConverter.GetBytes(@char); + case float @float: + return BitConverter.GetBytes(@float); + case double @double: + return BitConverter.GetBytes(@double); + case decimal @decimal: + return new BitArray(decimal.GetBits(@decimal)).ToBytes(); + case Guid guid: + return guid.ToByteArray(); + default: + return obj; + } } /// diff --git a/src/core/Akka/Routing/ConsistentHashRouter.cs b/src/core/Akka/Routing/ConsistentHashRouter.cs index 1be24775e8e..8b9accbe86c 100644 --- a/src/core/Akka/Routing/ConsistentHashRouter.cs +++ b/src/core/Akka/Routing/ConsistentHashRouter.cs @@ -166,7 +166,7 @@ public override Routee Select(object message, Routee[] routees) if (message == null || routees == null || routees.Length == 0) return Routee.NoRoutee; - Func> updateConsistentHash = () => + ConsistentHash UpdateConsistentHash() { // update consistentHash when routees are changed // changes to routees are rare when no changes this is a quick operation @@ -185,42 +185,43 @@ public override Routee Select(object message, Routee[] routees) return consistentHash; } return oldConsistentHash; - }; + } - Func target = hashData => + Routee Target(object hashData) { try { - var currentConsistentHash = updateConsistentHash(); + var currentConsistentHash = UpdateConsistentHash(); if (currentConsistentHash.IsEmpty) return Routee.NoRoutee; else { - if (hashData is byte[]) - return currentConsistentHash.NodeFor(hashData as byte[]).Routee; - if (hashData is string) - return currentConsistentHash.NodeFor(hashData as string).Routee; - return - currentConsistentHash.NodeFor( - _system.Serialization.FindSerializerFor(hashData).ToBinary(hashData)).Routee; + switch (hashData) + { + case byte[] bytes: + return currentConsistentHash.NodeFor(bytes).Routee; + case string data: + return currentConsistentHash.NodeFor(data).Routee; + default: + return currentConsistentHash.NodeFor(_system.Serialization.FindSerializerFor(hashData).ToBinary(hashData)).Routee; + } } } catch (Exception ex) { //serialization failed - _log.Value.Warning("Couldn't route message with consistent hash key [{0}] due to [{1}]", hashData, - ex.Message); + _log.Value.Warning("Couldn't route message with consistent hash key [{0}] due to [{1}]", hashData, ex.Message); return Routee.NoRoutee; } - }; + } - if (_hashMapping(message) != null) + var hashMapping = _hashMapping(message); + if (hashMapping != null) { - return target(ConsistentHash.ToBytesOrObject(_hashMapping(message))); + return Target(ConsistentHash.ToBytesOrObject(hashMapping)); } - else if (message is IConsistentHashable) + else if (message is IConsistentHashable hashable) { - var hashable = (IConsistentHashable) message; - return target(ConsistentHash.ToBytesOrObject(hashable.ConsistentHashKey)); + return Target(ConsistentHash.ToBytesOrObject(hashable.ConsistentHashKey)); } else { @@ -283,25 +284,18 @@ public ConsistentRoutee(Routee routee, Address selfAddress) /// public Address SelfAddress { get; private set; } - /// - /// TBD - /// - /// TBD + /// public override string ToString() { - if (Routee is ActorRefRoutee) + switch (Routee) { - var actorRef = Routee as ActorRefRoutee; - return ToStringWithFullAddress(actorRef.Actor.Path); - } - else if (Routee is ActorSelectionRoutee) - { - var selection = Routee as ActorSelectionRoutee; - return ToStringWithFullAddress(selection.Selection.Anchor.Path) + selection.Selection.PathString; - } - else - { - return Routee.ToString(); + case ActorRefRoutee actorRef: + return ToStringWithFullAddress(actorRef.Actor.Path); + case ActorSelectionRoutee selection: + return ToStringWithFullAddress(selection.Selection.Anchor.Path) + + selection.Selection.PathString; + default: + return Routee.ToString(); } } @@ -511,9 +505,8 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) { return OverrideUnsetConfig(routerConfig); } - else if (routerConfig is ConsistentHashingPool) + else if (routerConfig is ConsistentHashingPool other) { - var other = routerConfig as ConsistentHashingPool; return WithHashMapping(other._hashMapping).OverrideUnsetConfig(other); } else @@ -524,38 +517,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) - { - ConsistentHashingPool wssConf; + ConsistentHashingPool wssConf; - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); - - return wssConf; + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) + { + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } /// @@ -784,9 +767,8 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) { return base.WithFallback(routerConfig); } - else if (routerConfig is ConsistentHashingGroup) + else if (routerConfig is ConsistentHashingGroup other) { - var other = routerConfig as ConsistentHashingGroup; return WithHashMapping(other._hashMapping); } else diff --git a/src/core/Akka/Routing/Listeners.cs b/src/core/Akka/Routing/Listeners.cs index cdba06c6e79..255797022dd 100644 --- a/src/core/Akka/Routing/Listeners.cs +++ b/src/core/Akka/Routing/Listeners.cs @@ -127,17 +127,21 @@ public Receive ListenerReceive { return message => { - var match=PatternMatch.Match(message) - .With(m => Add(m.Listener)) - .With(d => Remove(d.Listener)) - .With(f => - { + switch (message) + { + case Listen listen: + Add(listen.Listener); + return true; + case Deafen deafen: + Remove(deafen.Listener); + return true; + case WithListeners listeners: foreach (var listener in Listeners) - { - f.ListenerFunction(listener); - } - }); - return match.WasHandled; + listeners.ListenerFunction(listener); + return true; + default: + return false; + } }; } } diff --git a/src/core/Akka/Routing/Random.cs b/src/core/Akka/Routing/Random.cs index 8373898c303..5dd30297537 100644 --- a/src/core/Akka/Routing/Random.cs +++ b/src/core/Akka/Routing/Random.cs @@ -154,38 +154,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) - { - RandomPool wssConf; - - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); + RandomPool wssConf; - return wssConf; + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) + { + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } /// diff --git a/src/core/Akka/Routing/ResizablePoolActor.cs b/src/core/Akka/Routing/ResizablePoolActor.cs index 3f152923a4f..3ee0a1657d8 100644 --- a/src/core/Akka/Routing/ResizablePoolActor.cs +++ b/src/core/Akka/Routing/ResizablePoolActor.cs @@ -33,11 +33,8 @@ protected ResizablePoolCell ResizerCell { get { - var resizablePoolCell = Context as ResizablePoolCell; - if (resizablePoolCell != null) - return resizablePoolCell; - else - throw new ActorInitializationException($"Resizable router actor can only be used when resizer is defined, not in {Context.GetType()}"); + return Context is ResizablePoolCell resizablePoolCell + ? resizablePoolCell : throw new ActorInitializationException($"Resizable router actor can only be used when resizer is defined, not in {Context.GetType()}"); } } @@ -72,7 +69,5 @@ protected override void StopIfAllRouteesRemoved() /// public class Resize : RouterManagementMessage { - } } - diff --git a/src/core/Akka/Routing/Resizer.cs b/src/core/Akka/Routing/Resizer.cs index 551291339f7..16e345cdaf9 100644 --- a/src/core/Akka/Routing/Resizer.cs +++ b/src/core/Akka/Routing/Resizer.cs @@ -10,7 +10,6 @@ using System.Linq; using Akka.Actor; using Akka.Configuration; -using Akka.Dispatch; namespace Akka.Routing { @@ -219,31 +218,25 @@ public int Pressure(IEnumerable currentRoutees) return currentRoutees.Count( routee => { - var actorRefRoutee = routee as ActorRefRoutee; - if (actorRefRoutee != null) + if (routee is ActorRefRoutee actorRefRoutee && actorRefRoutee.Actor is ActorRefWithCell actorRef) { - var actorRef = actorRefRoutee.Actor as ActorRefWithCell; - if (actorRef != null) + var underlying = actorRef.Underlying; + if (underlying is ActorCell cell) { - var underlying = actorRef.Underlying; - var cell = underlying as ActorCell; - if (cell != null) - { - if (PressureThreshold == 1) - return cell.Mailbox.IsScheduled() && cell.Mailbox.HasMessages; - if (PressureThreshold < 1) - return cell.Mailbox.IsScheduled() && cell.CurrentMessage != null; + if (PressureThreshold == 1) + return cell.Mailbox.IsScheduled() && cell.Mailbox.HasMessages; + if (PressureThreshold < 1) + return cell.Mailbox.IsScheduled() && cell.CurrentMessage != null; - return cell.Mailbox.NumberOfMessages >= PressureThreshold; - } - else - { - if (PressureThreshold == 1) - return underlying.HasMessages; - if (PressureThreshold < 1) - return true; //unstarted cells are always busy, for instance - return underlying.NumberOfMessages >= PressureThreshold; - } + return cell.Mailbox.NumberOfMessages >= PressureThreshold; + } + else + { + if (PressureThreshold == 1) + return underlying.HasMessages; + if (PressureThreshold < 1) + return true; //unstarted cells are always busy, for instance + return underlying.NumberOfMessages >= PressureThreshold; } } return false; @@ -271,7 +264,7 @@ public int Filter(int pressure, int capacity) /// proposed increase in capacity public int Rampup(int pressure, int capacity) { - return (pressure < capacity) ? 0 : Convert.ToInt32(Math.Ceiling(RampupRate * capacity)); + return pressure < capacity ? 0 : Convert.ToInt32(Math.Ceiling(RampupRate * capacity)); } /// @@ -348,11 +341,7 @@ public int Backoff(int pressure, int capacity) /// public int MessagesPerResize { get; private set; } - /// - /// Determines whether the specified resizer, is equal to this instance. - /// - /// The resizer to compare. - /// true if the specified router is equal to this instance; otherwise, false. + /// public bool Equals(DefaultResizer other) { if (ReferenceEquals(null, other)) return false; @@ -360,13 +349,7 @@ public bool Equals(DefaultResizer other) return MessagesPerResize == other.MessagesPerResize && BackoffRate.Equals(other.BackoffRate) && RampupRate.Equals(other.RampupRate) && BackoffThreshold.Equals(other.BackoffThreshold) && UpperBound == other.UpperBound && PressureThreshold == other.PressureThreshold && LowerBound == other.LowerBound; } - /// - /// Determines whether the specified , is equal to this instance. - /// - /// The to compare with this instance. - /// - /// true if the specified is equal to this instance; otherwise, false. - /// + /// public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) return false; @@ -375,12 +358,7 @@ public override bool Equals(object obj) return Equals((DefaultResizer)obj); } - /// - /// Returns a hash code for this instance. - /// - /// - /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table. - /// + /// public override int GetHashCode() { unchecked @@ -397,4 +375,3 @@ public override int GetHashCode() } } } - diff --git a/src/core/Akka/Routing/RoundRobin.cs b/src/core/Akka/Routing/RoundRobin.cs index d4fd8794090..5f4403ae6ec 100644 --- a/src/core/Akka/Routing/RoundRobin.cs +++ b/src/core/Akka/Routing/RoundRobin.cs @@ -199,38 +199,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) + RoundRobinPool wssConf; + + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) { - RoundRobinPool wssConf; - - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); - - return wssConf; + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } /// diff --git a/src/core/Akka/Routing/RoutedActorCell.cs b/src/core/Akka/Routing/RoutedActorCell.cs index 579e61901eb..2c1215c5448 100644 --- a/src/core/Akka/Routing/RoutedActorCell.cs +++ b/src/core/Akka/Routing/RoutedActorCell.cs @@ -117,14 +117,14 @@ internal void RemoveRoutees(IList affectedRoutees, bool stopChild) private void Watch(Routee routee) { - var actorRef = routee as ActorRefRoutee; - if (actorRef != null) Watch(actorRef.Actor); + if (routee is ActorRefRoutee actorRef) + Watch(actorRef.Actor); } private void Unwatch(Routee routee) { - var actorRef = routee as ActorRefRoutee; - if (actorRef != null) Unwatch(actorRef.Actor); + if (routee is ActorRefRoutee actorRef) + Unwatch(actorRef.Actor); } /// @@ -133,12 +133,9 @@ private void Unwatch(Routee routee) /// TBD private void StopIfChild(Routee routee) { - var actorRefRoutee = routee as ActorRefRoutee; - IChildStats childActorStats; - if (actorRefRoutee != null && TryGetChildStatsByName(actorRefRoutee.Actor.Path.Name, out childActorStats)) + if (routee is ActorRefRoutee actorRefRoutee && TryGetChildStatsByName(actorRefRoutee.Actor.Path.Name, out IChildStats childActorStats)) { - var childRef = childActorStats as ChildRestartStats; - if (childRef != null && childRef.Child != null) + if (childActorStats is ChildRestartStats childRef && childRef.Child != null) { // The reason for the delay is to give concurrent // messages a chance to be placed in mailbox before sending PoisonPill, @@ -157,9 +154,8 @@ public override void Start() // create the initial routees before scheduling the Router actor Router = RouterConfig.CreateRouter(System); - if (RouterConfig is Pool) + if (RouterConfig is Pool pool) { - var pool = (Pool)RouterConfig; // must not use pool.GetNrOfInstances(system) for old (not re-compiled) custom routers // for binary backwards compatibility reasons var deprecatedNrOfInstances = pool.NrOfInstances; @@ -171,9 +167,8 @@ public override void Start() if (nrOfRoutees > 0) AddRoutees(Vector.Fill(nrOfRoutees)(() => pool.NewRoutee(RouteeProps, this))); } - else if (RouterConfig is Group) + else if (RouterConfig is Group group) { - var group = (Group)RouterConfig; // must not use group.paths(system) for old (not re-compiled) custom routers // for binary backwards compatibility reasons var deprecatedPaths = group.Paths; diff --git a/src/core/Akka/Routing/RoutedActorRef.cs b/src/core/Akka/Routing/RoutedActorRef.cs index b6b8823d9a9..1036720372b 100644 --- a/src/core/Akka/Routing/RoutedActorRef.cs +++ b/src/core/Akka/Routing/RoutedActorRef.cs @@ -5,7 +5,6 @@ // //----------------------------------------------------------------------- -using System; using Akka.Actor; using Akka.Actor.Internal; using Akka.Dispatch; @@ -49,19 +48,10 @@ public RoutedActorRef( /// TBD protected override ActorCell NewCell() { - var pool = Props.RouterConfig as Pool; - ActorCell cell = null; - if (pool != null) - { - if (pool.Resizer != null) - { - cell = new ResizablePoolCell(System, this, Props, Dispatcher, _routeeProps, Supervisor, pool); - } - } - if (cell == null) - { - cell = new RoutedActorCell(System, this, Props, Dispatcher, _routeeProps, Supervisor); - } + ActorCell cell = Props.RouterConfig is Pool pool && pool.Resizer != null + ? new ResizablePoolCell(System, this, Props, Dispatcher, _routeeProps, Supervisor, pool) + : new RoutedActorCell(System, this, Props, Dispatcher, _routeeProps, Supervisor); + cell.Init(false, MailboxType); return cell; } diff --git a/src/core/Akka/Routing/Router.cs b/src/core/Akka/Routing/Router.cs index cfc31306785..f42e965d860 100644 --- a/src/core/Akka/Routing/Router.cs +++ b/src/core/Akka/Routing/Router.cs @@ -26,9 +26,9 @@ internal class NoRoutee : Routee /// TBD public override void Send(object message, IActorRef sender) { - if (sender is LocalActorRef) + if (sender is LocalActorRef localActorRef) { - sender.AsInstanceOf().Provider.DeadLetters.Tell(message); + localActorRef.Provider.DeadLetters.Tell(message); } } } @@ -128,24 +128,11 @@ public override bool Equals(object obj) return Equals((ActorRefRoutee)obj); } - /// - /// TBD - /// - /// TBD - /// TBD - protected bool Equals(ActorRefRoutee other) - { - return Equals(Actor, other.Actor); - } + /// + protected bool Equals(ActorRefRoutee other) => Equals(Actor, other.Actor); - /// - /// TBD - /// - /// TBD - public override int GetHashCode() - { - return (Actor != null ? Actor.GetHashCode() : 0); - } + /// + public override int GetHashCode() => Actor?.GetHashCode() ?? 0; } /// @@ -203,24 +190,11 @@ public override bool Equals(object obj) return Equals((ActorSelectionRoutee)obj); } - /// - /// TBD - /// - /// TBD - /// TBD - protected bool Equals(ActorSelectionRoutee other) - { - return Equals(_actor, other._actor); - } + /// + protected bool Equals(ActorSelectionRoutee other) => Equals(_actor, other._actor); - /// - /// TBD - /// - /// TBD - public override int GetHashCode() - { - return (_actor != null ? _actor.GetHashCode() : 0); - } + /// + public override int GetHashCode() => _actor?.GetHashCode() ?? 0; } /// @@ -281,7 +255,7 @@ public SeveralRoutees(Routee[] routees) /// TBD public override void Send(object message, IActorRef sender) { - foreach (Routee routee in routees) + foreach (Routee routee in routees) { routee.Send(message, sender); } @@ -358,11 +332,7 @@ public Router(RoutingLogic logic, IActorRef routee, params IActorRef[] routees) /// TBD public Router(RoutingLogic logic, params Routee[] routees) { - if(routees == null) - { - routees = new Routee[0]; - } - _routees = routees; + _routees = routees ?? new Routee[0]; _logic = logic; } /// @@ -384,9 +354,9 @@ public RoutingLogic RoutingLogic private object UnWrap(object message) { - if (message is RouterEnvelope) + if (message is RouterEnvelope routerEnvelope) { - return message.AsInstanceOf().Message; + return routerEnvelope.Message; } return message; diff --git a/src/core/Akka/Routing/RouterActor.cs b/src/core/Akka/Routing/RouterActor.cs index 0c1eefa9efc..4fd77534b75 100644 --- a/src/core/Akka/Routing/RouterActor.cs +++ b/src/core/Akka/Routing/RouterActor.cs @@ -24,11 +24,8 @@ protected RoutedActorCell Cell { get { - var routedActorCell = Context as RoutedActorCell; - if (routedActorCell != null) - return routedActorCell; - else - throw new ActorInitializationException($"Router actor can only be used in RoutedActorRef, not in {Context.GetType()}"); + return Context is RoutedActorCell routedActorCell + ? routedActorCell : throw new ActorInitializationException($"Router actor can only be used in RoutedActorRef, not in {Context.GetType()}"); } } @@ -47,30 +44,25 @@ private IActorRef RoutingLogicController /// TBD protected override void OnReceive(object message) { - if (message is GetRoutees) + switch (message) { - Sender.Tell(new Routees(Cell.Router.Routees)); - } - else if (message is AddRoutee) - { - var addRoutee = message as AddRoutee; - Cell.AddRoutee(addRoutee.Routee); - } - else if (message is RemoveRoutee) - { - var removeRoutee = message as RemoveRoutee; - Cell.RemoveRoutee(removeRoutee.Routee, stopChild: true); - StopIfAllRouteesRemoved(); - } - else if (message is Terminated) - { - var terminated = message as Terminated; - Cell.RemoveRoutee(new ActorRefRoutee(terminated.ActorRef), stopChild: false); - StopIfAllRouteesRemoved(); - } - else - { - RoutingLogicController?.Forward(message); + case GetRoutees getRoutees: + Sender.Tell(new Routees(Cell.Router.Routees)); + break; + case AddRoutee addRoutee: + Cell.AddRoutee(addRoutee.Routee); + break; + case RemoveRoutee removeRoutee: + Cell.RemoveRoutee(removeRoutee.Routee, stopChild: true); + StopIfAllRouteesRemoved(); + break; + case Terminated terminated: + Cell.RemoveRoutee(new ActorRefRoutee(terminated.ActorRef), stopChild: false); + StopIfAllRouteesRemoved(); + break; + default: + RoutingLogicController?.Forward(message); + break; } } diff --git a/src/core/Akka/Routing/RouterConfig.cs b/src/core/Akka/Routing/RouterConfig.cs index ab4725ea2c2..c84cccff82c 100644 --- a/src/core/Akka/Routing/RouterConfig.cs +++ b/src/core/Akka/Routing/RouterConfig.cs @@ -9,7 +9,6 @@ using System.Collections.Generic; using System.Linq; using Akka.Actor; -using Akka.Actor.Internal; using Akka.Configuration; using Akka.Dispatch; using Akka.Util; @@ -128,10 +127,7 @@ public bool Equals(RouterConfig other) } /// - public override bool Equals(object obj) - { - return Equals(obj as RouterConfig); - } + public override bool Equals(object obj) => Equals(obj as RouterConfig); } /// @@ -211,10 +207,7 @@ public override bool Equals(object obj) } /// - public override int GetHashCode() - { - return Paths?.GetHashCode() ?? 0; - } + public override int GetHashCode() => Paths?.GetHashCode() ?? 0; } /// diff --git a/src/core/Akka/Routing/RouterPoolActor.cs b/src/core/Akka/Routing/RouterPoolActor.cs index aeaf8a85993..e63858634e0 100644 --- a/src/core/Akka/Routing/RouterPoolActor.cs +++ b/src/core/Akka/Routing/RouterPoolActor.cs @@ -8,7 +8,6 @@ using System.Linq; using Akka.Actor; using Akka.Util; -using Akka.Util.Internal; namespace Akka.Routing { @@ -35,8 +34,7 @@ public RouterPoolActor(SupervisorStrategy supervisorStrategy) { _supervisorStrategy = supervisorStrategy; - var pool = Cell.RouterConfig as Pool; - if (pool != null) + if (Cell.RouterConfig is Pool pool) { Pool = pool; } @@ -61,8 +59,7 @@ protected override SupervisorStrategy SupervisorStrategy() /// The message. protected override void OnReceive(object message) { - var poolSize = message as AdjustPoolSize; - if (poolSize != null) + if (message is AdjustPoolSize poolSize) { if (poolSize.Change > 0) { @@ -85,8 +82,5 @@ protected override void OnReceive(object message) base.OnReceive(message); } } - - } } - diff --git a/src/core/Akka/Routing/ScatterGatherFirstCompleted.cs b/src/core/Akka/Routing/ScatterGatherFirstCompleted.cs index a47cc572266..3a6becbf5a8 100644 --- a/src/core/Akka/Routing/ScatterGatherFirstCompleted.cs +++ b/src/core/Akka/Routing/ScatterGatherFirstCompleted.cs @@ -260,38 +260,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) - { - ScatterGatherFirstCompletedPool wssConf; - - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); + ScatterGatherFirstCompletedPool wssConf; - return wssConf; + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) + { + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } #region Surrogate diff --git a/src/core/Akka/Routing/SmallestMailbox.cs b/src/core/Akka/Routing/SmallestMailbox.cs index 6295ffa5e2b..44b30cb6a19 100644 --- a/src/core/Akka/Routing/SmallestMailbox.cs +++ b/src/core/Akka/Routing/SmallestMailbox.cs @@ -95,14 +95,9 @@ private Routee SelectNext(Routee[] routees) private ICell TryGetActorCell(Routee routee) { - var refRoutee = routee as ActorRefRoutee; - if (refRoutee != null) + if (routee is ActorRefRoutee refRoutee && refRoutee.Actor is ActorRefWithCell actorRef) { - var actorRef = refRoutee.Actor as ActorRefWithCell; - if (actorRef != null) - { - return actorRef.Underlying; - } + return actorRef.Underlying; } return null; } @@ -230,38 +225,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) - { - return this; - } - else + if (other is Pool pool) { - var pool = other as Pool; - if (pool != null) - { - SmallestMailboxPool wssConf; + SmallestMailboxPool wssConf; - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } - - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); - - return wssConf; + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) + { + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } /// diff --git a/src/core/Akka/Routing/TailChopping.cs b/src/core/Akka/Routing/TailChopping.cs index a1d769e906c..fb2ca5f6ba6 100644 --- a/src/core/Akka/Routing/TailChopping.cs +++ b/src/core/Akka/Routing/TailChopping.cs @@ -7,7 +7,6 @@ using System; using System.Collections.Generic; -using System.Linq; using System.Threading.Tasks; using Akka.Actor; using Akka.Configuration; @@ -123,7 +122,7 @@ public override void Send(object message, IActorRef sender) try { - completion.TrySetResult(await ((Task)_routees[currentIndex].Ask(message, _within)).ConfigureAwait(false)); + completion.TrySetResult(await (_routees[currentIndex].Ask(message, _within)).ConfigureAwait(false)); } catch (TaskCanceledException) { @@ -277,38 +276,28 @@ public override RouterConfig WithFallback(RouterConfig routerConfig) private RouterConfig OverrideUnsetConfig(RouterConfig other) { - if (other is NoRouter) + if (other is Pool pool) { - return this; - } - else - { - var pool = other as Pool; - if (pool != null) - { - TailChoppingPool wssConf; - - if (SupervisorStrategy != null - && SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy) - && !(pool.SupervisorStrategy.Equals(Pool.DefaultSupervisorStrategy))) - { - wssConf = this.WithSupervisorStrategy(pool.SupervisorStrategy); - } - else - { - wssConf = this; - } + TailChoppingPool wssConf; - if (wssConf.Resizer == null && pool.Resizer != null) - return wssConf.WithResizer(pool.Resizer); - - return wssConf; + if (SupervisorStrategy != null + && SupervisorStrategy.Equals(DefaultSupervisorStrategy) + && !pool.SupervisorStrategy.Equals(DefaultSupervisorStrategy)) + { + wssConf = WithSupervisorStrategy(pool.SupervisorStrategy); } else { - return this; + wssConf = this; } + + if (wssConf.Resizer == null && pool.Resizer != null) + return wssConf.WithResizer(pool.Resizer); + + return wssConf; } + + return this; } ///