Skip to content

Commit

Permalink
Re-ordering Swarm<T>.StartAsync()
Browse files Browse the repository at this point in the history
- Listen → (NAT check & set Endpoint & run TURN tasks) → preload → run & wait other tasks (+1 squashed commits)
  • Loading branch information
longfin committed Aug 11, 2019
1 parent 699e3ac commit 8962f5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 33 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ To be released.
disconnected while receiving. [[#416]]
- Fixed a bug that `Swarm<T>.PreloadAsync()` had been processed even if there
is no appropriate peer. [[#418]]
- Fixed a bug that TURN relay connection had disconnected when preloading
took a long time. [[#424]]

[#319]: https://github.com/planetarium/libplanet/issues/319
[#343]: https://github.com/planetarium/libplanet/pull/343
Expand Down Expand Up @@ -122,6 +124,7 @@ To be released.
[#416]: https://github.com/planetarium/libplanet/pull/416
[#417]: https://github.com/planetarium/libplanet/pull/417
[#418]: https://github.com/planetarium/libplanet/pull/418
[#424]: https://github.com/planetarium/libplanet/pull/424
[LiteDB #1268]: https://github.com/mbdavid/LiteDB/issues/1268


Expand Down
56 changes: 23 additions & 33 deletions Libplanet/Net/Swarm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -435,12 +435,18 @@ public async Task StartAsync(

_logger.Information($"Listen on {_listenPort}");

_workerCancellationTokenSource = new CancellationTokenSource();
CancellationToken workerCancellationToken =
CancellationTokenSource.CreateLinkedTokenSource(
_workerCancellationTokenSource.Token, cancellationToken
).Token;
_cancellationToken = workerCancellationToken;
var tasks = new List<Task>();
var behindNAT = false;

if (!(_turnClient is null))
{
_publicIPAddress = (await _turnClient.GetMappedAddressAsync())
.Address;
_publicIPAddress = (await _turnClient.GetMappedAddressAsync()).Address;

if (await _turnClient.IsBehindNAT())
{
Expand All @@ -453,42 +459,28 @@ public async Task StartAsync(
IPEndPoint turnEp = await _turnClient.AllocateRequestAsync(
TurnAllocationLifetime
);
EndPoint = new DnsEndPoint(
turnEp.Address.ToString(), turnEp.Port);
EndPoint = new DnsEndPoint(turnEp.Address.ToString(), turnEp.Port);

tasks.Add(BindingProxies(_cancellationToken));
tasks.Add(RefreshAllocate(_cancellationToken));
tasks.Add(RefreshPermissions(_cancellationToken));
}
else
{
EndPoint = new DnsEndPoint(_host, _listenPort.Value);
}

try
using (await _runningMutex.LockAsync())
{
_workerCancellationTokenSource = new CancellationTokenSource();
CancellationToken workerCancellationToken =
CancellationTokenSource.CreateLinkedTokenSource(
_workerCancellationTokenSource.Token, cancellationToken
).Token;
_cancellationToken = workerCancellationToken;

using (await _runningMutex.LockAsync())
{
Running = true;
await PreloadAsync(render: true, cancellationToken: _cancellationToken);
}

var tasks = new List<Task>
{
RepeatDeltaDistributionAsync(distributeInterval, _cancellationToken),
BroadcastTxAsync(broadcastTxInterval, _cancellationToken),
Task.Run(() => _poller.Run(), _cancellationToken),
};
await PreloadAsync(render: true, cancellationToken: _cancellationToken);
Running = true;
}

if (behindNAT)
{
tasks.Add(BindingProxies(_cancellationToken));
tasks.Add(RefreshAllocate(_cancellationToken));
tasks.Add(RefreshPermissions(_cancellationToken));
}
try
{
tasks.Add(RepeatDeltaDistributionAsync(distributeInterval, _cancellationToken));
tasks.Add(BroadcastTxAsync(broadcastTxInterval, _cancellationToken));
tasks.Add(Task.Run(() => _poller.Run(), _cancellationToken));

await await Task.WhenAny(tasks);
}
Expand All @@ -498,9 +490,7 @@ public async Task StartAsync(
}
catch (Exception e)
{
_logger.Error(
e,
"An unexpected exception occured during StartAsync()");
_logger.Error(e, "An unexpected exception occured during StartAsync()");
throw;
}
}
Expand Down

0 comments on commit 8962f5e

Please sign in to comment.