Skip to content

Commit

Permalink
Merge pull request #48 from fassadlr/memleaks-node-part2
Browse files Browse the repository at this point in the history
Memory leak fixes for node [Part 2]
  • Loading branch information
dangershony committed Nov 2, 2017
2 parents 55ac6dd + 3acd928 commit 2b0fbc3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 69 deletions.
29 changes: 15 additions & 14 deletions NBitcoin.Tests/ProtocolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -403,26 +403,27 @@ public void CanGetTransactionsFromMemPool()
[Fact]
public void CanConnectToRandomNode()
{
Stopwatch watch = new Stopwatch();
NodeConnectionParameters parameters = new NodeConnectionParameters();
var watch = new Stopwatch();
var parameters = new NodeConnectionParameters();

var addrman = GetCachedAddrMan("addrmancache.dat");
parameters.TemplateBehaviors.Add(new AddressManagerBehavior(addrman)
{
PeersToDiscover = 50
});

watch.Start();
IPEndPoint[] connectedEndpoints = null;
using (var node = Node.Connect(Network.Main, parameters, connectedEndpoints))
{
var timeToFind = watch.Elapsed;
node.VersionHandshake();
node.Dispose();
watch.Restart();
using (var node2 = Node.Connect(Network.Main, parameters, connectedEndpoints))
{
var timeToFind2 = watch.Elapsed;
}
}

Node node = Node.Connect(Network.Main, parameters);
TimeSpan timeToFind = watch.Elapsed;
node.VersionHandshake();
node.Disconnect();

watch.Restart();
Node node2 = Node.Connect(Network.Main, parameters);
TimeSpan timeToFind2 = watch.Elapsed;
node.Disconnect();

addrman.SavePeerFile("addrmancache.dat", Network.Main);
}

Expand Down
11 changes: 7 additions & 4 deletions NBitcoin/Protocol/MessageListener.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if !NOSOCKET

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
Expand All @@ -9,7 +10,6 @@

namespace NBitcoin.Protocol
{

public interface MessageListener<in T>
{
void PushMessage(T message);
Expand All @@ -35,6 +35,7 @@ public NewThreadMessageListener(Action<T> process)
throw new ArgumentNullException("process");
_Process = process;
}

#region MessageListener<T> Members

public void PushMessage(T message)
Expand Down Expand Up @@ -85,6 +86,7 @@ public EventLoopMessageListener(Action<T> processMessage)
}
})).Start();
}

BlockingCollection<T> _MessageQueue = new BlockingCollection<T>(new ConcurrentQueue<T>());
public BlockingCollection<T> MessageQueue
{
Expand All @@ -94,7 +96,6 @@ public BlockingCollection<T> MessageQueue
}
}


#region MessageListener Members

public void PushMessage(T message)
Expand All @@ -107,20 +108,21 @@ public void PushMessage(T message)
#region IDisposable Members

CancellationTokenSource cancellationSource = new CancellationTokenSource();

public void Dispose()
{
if(cancellationSource.IsCancellationRequested)
return;

cancellationSource.Cancel();
cancellationSource.Dispose();
}

#endregion

}

public class PollMessageListener<T> : MessageListener<T>
{

BlockingCollection<T> _MessageQueue = new BlockingCollection<T>(new ConcurrentQueue<T>());
public BlockingCollection<T> MessageQueue
{
Expand All @@ -145,4 +147,5 @@ public virtual void PushMessage(T message)
#endregion
}
}

#endif
Loading

0 comments on commit 2b0fbc3

Please sign in to comment.