Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add expire milliseconds for dictionary #4

Merged
merged 1 commit into from
Apr 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions src/RpcServer/RelayActor.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Akka.Actor;
using Neo.Network.P2P.Payloads;
using System.Collections.Generic;
using System.Threading.Tasks;
using static Neo.Ledger.Blockchain;

namespace Neo.Plugins
Expand All @@ -9,10 +10,12 @@ public class RelayActor : UntypedActor
{
private readonly NeoSystem neoSystem;
private readonly Dictionary<UInt256, IActorRef> senders = new Dictionary<UInt256, IActorRef>();
private readonly int expireMs;

public RelayActor(NeoSystem neoSystem)
public RelayActor(NeoSystem neoSystem, int expireMs)
{
this.neoSystem = neoSystem;
this.expireMs = expireMs;
Context.System.EventStream.Subscribe(Self, typeof(RelayResult));
}

Expand All @@ -22,8 +25,15 @@ protected override void OnReceive(object message)
{
case IInventory inventory:
{
senders.Add(inventory.Hash, Sender);
UInt256 hash = inventory.Hash;
senders.Add(hash, Sender);
neoSystem.Blockchain.Tell(inventory);
// the sender will clean the record after expireMs
Task.Run(async () =>
{
await Task.Delay(expireMs);
senders.Remove(hash);
});
break;
}
case RelayResult reason:
Expand All @@ -37,9 +47,9 @@ protected override void OnReceive(object message)
}
}

public static Props Props(NeoSystem neoSystem)
public static Props Props(NeoSystem neoSystem, int expire = 10000)
{
return Akka.Actor.Props.Create(() => new RelayActor(neoSystem));
return Akka.Actor.Props.Create(() => new RelayActor(neoSystem, expire));
}
}
}