Skip to content

Commit

Permalink
raise priority of Ping
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashuaidehao committed Jul 31, 2020
1 parent 75df7cd commit c448ac8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Neo.Network.P2P.Payloads;
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -217,8 +218,16 @@ public static Props Props(NeoSystem system)
return Akka.Actor.Props.Create(() => new TaskManager(system)).WithMailbox("task-manager-mailbox");
}

private readonly ConcurrentDictionary<ActorPath, DateTime> _expiredTimes = new ConcurrentDictionary<ActorPath, DateTime>();

private void RequestTasks(TaskSession session)
{
if (!_expiredTimes.ContainsKey(session.RemoteNode.Path) ||
_expiredTimes.TryGetValue(session.RemoteNode.Path, out var expireTime) && expireTime < DateTime.Now)
{
session.RemoteNode.Tell(Message.Create("ping", PingPayload.Create(Blockchain.Singleton.Height)));
_expiredTimes[session.RemoteNode.Path] = DateTime.Now.AddSeconds(PingCoolingOffPeriod);
}
if (session.HasTask) return;
if (session.AvailableTasks.Count > 0)
{
Expand Down Expand Up @@ -260,11 +269,6 @@ private void RequestTasks(TaskSession session)
}
session.RemoteNode.Tell(Message.Create("getblocks", GetBlocksPayload.Create(hash)));
}
else if (Blockchain.Singleton.HeaderHeight >= session.LastBlockIndex
&& TimeProvider.Current.UtcNow.ToTimestamp() - PingCoolingOffPeriod >= Blockchain.Singleton.GetBlock(Blockchain.Singleton.CurrentHeaderHash)?.Timestamp)
{
session.RemoteNode.Tell(Message.Create("ping", PingPayload.Create(Blockchain.Singleton.Height)));
}
}
}

Expand Down

0 comments on commit c448ac8

Please sign in to comment.