2021-08-27 15:03:47 +00:00
|
|
|
using System;
|
2021-04-29 09:10:19 +00:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
namespace Myriad.Gateway.State
|
|
|
|
{
|
|
|
|
public class HeartbeatWorker: IAsyncDisposable
|
|
|
|
{
|
|
|
|
private Task? _worker;
|
|
|
|
private CancellationTokenSource? _workerCts;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-04-29 09:10:19 +00:00
|
|
|
public TimeSpan? CurrentHeartbeatInterval { get; private set; }
|
|
|
|
|
|
|
|
public async ValueTask Start(TimeSpan heartbeatInterval, Func<Task> callback)
|
|
|
|
{
|
|
|
|
if (_worker != null)
|
|
|
|
await Stop();
|
|
|
|
|
|
|
|
CurrentHeartbeatInterval = heartbeatInterval;
|
|
|
|
_workerCts = new CancellationTokenSource();
|
|
|
|
_worker = Worker(heartbeatInterval, callback, _workerCts.Token);
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ValueTask Stop()
|
|
|
|
{
|
2021-08-27 15:03:47 +00:00
|
|
|
if (_worker == null)
|
2021-04-29 09:10:19 +00:00
|
|
|
return;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-04-29 09:10:19 +00:00
|
|
|
_workerCts?.Cancel();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
await _worker;
|
|
|
|
}
|
|
|
|
catch (TaskCanceledException) { }
|
|
|
|
|
|
|
|
_worker?.Dispose();
|
|
|
|
_workerCts?.Dispose();
|
|
|
|
_worker = null;
|
|
|
|
CurrentHeartbeatInterval = null;
|
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-04-29 09:10:19 +00:00
|
|
|
private async Task Worker(TimeSpan heartbeatInterval, Func<Task> callback, CancellationToken ct)
|
|
|
|
{
|
|
|
|
var initialDelay = GetInitialHeartbeatDelay(heartbeatInterval);
|
|
|
|
await Task.Delay(initialDelay, ct);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-04-29 09:10:19 +00:00
|
|
|
while (!ct.IsCancellationRequested)
|
|
|
|
{
|
|
|
|
await callback();
|
|
|
|
await Task.Delay(heartbeatInterval, ct);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static TimeSpan GetInitialHeartbeatDelay(TimeSpan heartbeatInterval) =>
|
|
|
|
// Docs specify `heartbeat_interval * random.random()` but we'll add a lil buffer :)
|
|
|
|
heartbeatInterval * (new Random().NextDouble() * 0.9 + 0.05);
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
{
|
|
|
|
await Stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|