From 7c8bf1f6a1ca7859c9043289e8e1c5b414e5a95b Mon Sep 17 00:00:00 2001 From: Erik Zhang Date: Wed, 5 Dec 2018 15:29:01 +0800 Subject: [PATCH] Send `Idle` message to actors when they are idle. (#502) --- neo/IO/Actors/Idle.cs | 7 +++++++ neo/IO/Actors/PriorityMessageQueue.cs | 8 ++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 neo/IO/Actors/Idle.cs diff --git a/neo/IO/Actors/Idle.cs b/neo/IO/Actors/Idle.cs new file mode 100644 index 0000000000..4bb8655bd8 --- /dev/null +++ b/neo/IO/Actors/Idle.cs @@ -0,0 +1,7 @@ +namespace Neo.IO.Actors +{ + internal sealed class Idle + { + public static Idle Instance { get; } = new Idle(); + } +} diff --git a/neo/IO/Actors/PriorityMessageQueue.cs b/neo/IO/Actors/PriorityMessageQueue.cs index c166d76f08..bfdce8e67f 100644 --- a/neo/IO/Actors/PriorityMessageQueue.cs +++ b/neo/IO/Actors/PriorityMessageQueue.cs @@ -38,8 +38,12 @@ public void Enqueue(IActorRef receiver, Envelope envelope) public bool TryDequeue(out Envelope envelope) { - if (high.TryDequeue(out envelope)) return true; - if (low.TryDequeue(out envelope)) return true; + if (high.TryDequeue(out envelope) || low.TryDequeue(out envelope)) + { + if (Count == 0 && !(envelope.Message is Idle)) + low.Enqueue(new Envelope(Idle.Instance, ActorRefs.NoSender)); + return true; + } return false; } }