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

Separate _poller in NetMQTransport #859

Merged
merged 1 commit into from
May 4, 2020
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ To be released.

### Behavioral changes

- Improved performance of `Swarm<T>` by multiplexing response and
broadcast. [[#858], [#859]]

### Bug fixes

### CLI tools

[#858]: https://github.com/planetarium/libplanet/issues/858
[#859]: https://github.com/planetarium/libplanet/pull/859

Version 0.9.0
-------------
Expand Down
61 changes: 39 additions & 22 deletions Libplanet/Net/NetMQTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ internal class NetMQTransport : ITransport
private NetMQQueue<(Address?, Message)> _broadcastQueue;

private RouterSocket _router;
private NetMQPoller _poller;
private NetMQPoller _routerPoller;
private NetMQPoller _broadcastPoller;

private int? _listenPort;
private TurnClient _turnClient;
Expand Down Expand Up @@ -263,7 +264,8 @@ public async Task StartAsync(CancellationToken cancellationToken)

_replyQueue = new NetMQQueue<NetMQMessage>();
_broadcastQueue = new NetMQQueue<(Address?, Message)>();
_poller = new NetMQPoller { _router, _replyQueue, _broadcastQueue };
_routerPoller = new NetMQPoller { _router, _replyQueue };
_broadcastPoller = new NetMQPoller { _broadcastQueue };

_router.ReceiveReady += ReceiveMessage;
_replyQueue.ReceiveReady += DoReply;
Expand All @@ -287,24 +289,8 @@ public async Task RunAsync(CancellationToken cancellationToken)
TimeSpan.FromSeconds(10),
_cancellationToken));
tasks.Add(RebuildConnectionAsync(TimeSpan.FromMinutes(30), _cancellationToken));
tasks.Add(
Task.Run(() =>
{
// Ignore NetMQ related exceptions during NetMQPoller.Run() to stabilize
// tests.
try
{
_poller.Run();
}
catch (TerminatingException)
{
_logger.Error($"TerminatingException occurred in {nameof(_poller)}");
}
catch (ObjectDisposedException)
{
_logger.Error($"ObjectDisposedException occurred in {nameof(_poller)}");
}
}));
tasks.Add(RunPoller(_routerPoller));
tasks.Add(RunPoller(_broadcastPoller));

await await Task.WhenAny(tasks);
}
Expand All @@ -323,9 +309,14 @@ public async Task StopAsync(
_replyQueue.ReceiveReady -= DoReply;
_router.ReceiveReady -= ReceiveMessage;

if (_poller.IsRunning)
if (_routerPoller.IsRunning)
{
_poller.Dispose();
_routerPoller.Dispose();
}

if (_broadcastPoller.IsRunning)
{
_broadcastPoller.Dispose();
}

_broadcastQueue.Dispose();
Expand Down Expand Up @@ -992,6 +983,32 @@ private async Task RebuildConnectionAsync(
}
}

private Task RunPoller(NetMQPoller poller) =>
Task.Factory.StartNew(
() =>
{
// Ignore NetMQ related exceptions during NetMQPoller.Run() to stabilize
// tests.
try
{
poller.Run();
}
catch (TerminatingException)
{
_logger.Error($"TerminatingException occurred during poller.Run()");
}
catch (ObjectDisposedException)
{
_logger.Error(
$"ObjectDisposedException occurred during poller.Run()"
);
}
},
CancellationToken.None,
TaskCreationOptions.DenyChildAttach | TaskCreationOptions.LongRunning,
TaskScheduler.Default
);

private readonly struct MessageRequest
{
private readonly int _retried;
Expand Down