Skip to content

Commit

Permalink
Add pausing/resuming events to IRetryTaskController
Browse files Browse the repository at this point in the history
For issue #50.
  • Loading branch information
sonvister committed Feb 25, 2018
1 parent 8decc4a commit 72f9e41
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/Binance/Utility/IRetryTaskController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
namespace Binance.Utility
using System;

namespace Binance.Utility
{
public interface IRetryTaskController : ITaskController
{
/// <summary>
/// The pausing event.
/// </summary>
event EventHandler<PausingEventArgs> Pausing;

/// <summary>
/// The resuming event.
/// </summary>
event EventHandler<EventArgs> Resuming;

/// <summary>
/// Get or set the retry delay (milliseconds).
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions src/Binance/Utility/PausingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Binance.Utility
{
/// <summary>
/// The <see cref="IRetryTaskController"/> pausing event arguments.
/// </summary>
public class PausingEventArgs : EventArgs
{
/// <summary>
/// Get the delay.
/// </summary>
public TimeSpan Delay { get; }

/// <summary>
/// Constructor.
/// </summary>
/// <param name="delay"></param>
public PausingEventArgs(TimeSpan delay)
{
Throw.IfNull(delay, nameof(delay));

Delay = delay;
}
}
}
67 changes: 65 additions & 2 deletions src/Binance/Utility/RetryTaskController.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,42 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Binance.Utility
{
public class RetryTaskController : TaskController, IRetryTaskController
{
#region Public Events

public event EventHandler<PausingEventArgs> Pausing
{
add
{
if (_pausing == null || !_pausing.GetInvocationList().Contains(value))
{
_pausing += value;
}
}
remove => _pausing -= value;
}
private EventHandler<PausingEventArgs> _pausing;

public event EventHandler<EventArgs> Resuming
{
add
{
if (_resuming == null || !_resuming.GetInvocationList().Contains(value))
{
_resuming += value;
}
}
remove => _resuming -= value;
}
private EventHandler<EventArgs> _resuming;

#endregion Public Events

#region Public Properties

public int RetryDelayMilliseconds { get; set; } = 5000;
Expand Down Expand Up @@ -45,7 +76,11 @@ public override void Begin(Func<CancellationToken, Task> action = null)
{
while (!Cts.IsCancellationRequested)
{
try { await Action(Cts.Token).ConfigureAwait(false); }
try
{
await Action(Cts.Token)
.ConfigureAwait(false);
}
catch (OperationCanceledException) { /* ignored */ }
catch (Exception e)
{
Expand All @@ -59,10 +94,16 @@ public override void Begin(Func<CancellationToken, Task> action = null)
{
if (!Cts.IsCancellationRequested)
{
await DelayAsync(Cts.Token).ConfigureAwait(false);
await DelayAsync(Cts.Token)
.ConfigureAwait(false);
}
}
catch { /* ignored */ }

if (!Cts.IsCancellationRequested)
{
OnResuming();
}
}
});
}
Expand All @@ -73,9 +114,31 @@ public override void Begin(Func<CancellationToken, Task> action = null)

protected virtual Task DelayAsync(CancellationToken token)
{
// Notify listeners.
OnPausing(TimeSpan.FromMilliseconds(RetryDelayMilliseconds));

return Task.Delay(RetryDelayMilliseconds, token);
}

/// <summary>
/// Raise a pausing event.
/// </summary>
/// <param name="timeSpan"></param>
protected void OnPausing(TimeSpan timeSpan)
{
try { _pausing?.Invoke(this, new PausingEventArgs(timeSpan)); }
catch { /* ignored */ }
}

/// <summary>
/// Raise a resuming event.
/// </summary>
protected void OnResuming()
{
try { _resuming?.Invoke(this, EventArgs.Empty); }
catch { /* ignored */ }
}

#endregion Protected Methods
}
}
3 changes: 3 additions & 0 deletions src/Binance/WebSocket/BinanceWebSocketStreamController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ await base.DelayAsync(token)

while (status == BinanceStatus.Maintenance)
{
// Notify listeners.
OnPausing(TimeSpan.FromMilliseconds(SystemMaintenanceCheckDelayMilliseconds));

await Task.Delay(SystemMaintenanceCheckDelayMilliseconds, token)
.ConfigureAwait(false);

Expand Down

0 comments on commit 72f9e41

Please sign in to comment.