Skip to content

Commit

Permalink
Merge pull request #23 from shargon/improve-global-tasks
Browse files Browse the repository at this point in the history
Improve global tasks
  • Loading branch information
ShawnYun authored May 27, 2020
2 parents f93ba3a + a1b6809 commit cd41d38
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/neo/Network/P2P/TaskManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Register { public VersionPayload Version; }
public class Update { public uint LastBlockIndex; }
public class NewTasks { public InvPayload Payload; }
public class RestartTasks { public InvPayload Payload; }
private class Counter { public int Value; }
private class Timer { }

private static readonly TimeSpan TimerInterval = TimeSpan.FromSeconds(30);
Expand All @@ -33,7 +34,7 @@ private class Timer { }
/// A set of known hashes, of inventories or payloads, already received.
/// </summary>
private readonly HashSetCache<UInt256> knownHashes;
private readonly Dictionary<UInt256, int> globalTasks = new Dictionary<UInt256, int>();
private readonly Dictionary<UInt256, Counter> globalTasks = new Dictionary<UInt256, Counter>();
private readonly Dictionary<uint, TaskSession> receivedBlockIndex = new Dictionary<uint, TaskSession>();
private readonly List<uint> failedSyncTasks = new List<uint>();
private readonly Dictionary<IActorRef, TaskSession> sessions = new Dictionary<IActorRef, TaskSession>();
Expand Down Expand Up @@ -198,10 +199,10 @@ private void DecrementGlobalTask(UInt256 hash)
{
if (globalTasks.TryGetValue(hash, out var value))
{
if (value == 1)
if (value.Value == 1)
globalTasks.Remove(hash);
else
globalTasks[hash] = value - 1;
value.Value--;
}
}

Expand All @@ -210,13 +211,13 @@ private bool IncrementGlobalTask(UInt256 hash)
{
if (!globalTasks.TryGetValue(hash, out var value))
{
globalTasks[hash] = 1;
globalTasks[hash] = new Counter() { Value = 1 };
return true;
}
if (value >= MaxConncurrentTasks)
if (value.Value >= MaxConncurrentTasks)
return false;

globalTasks[hash] = value + 1;
value.Value++;
return true;
}

Expand Down

0 comments on commit cd41d38

Please sign in to comment.