34 lines
1.1 KiB
C#
34 lines
1.1 KiB
C#
using ZB.MOM.WW.ScadaBridge.Commons.Entities.Kpi;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.KpiHistory;
|
|
|
|
/// <summary>
|
|
/// Root-provider-owned admission for source collections. Actor replacement must not
|
|
/// start another call while an earlier cancellation-ignoring collection still owns its scope.
|
|
/// Registration indices are stable because the root provider's service collection is immutable.
|
|
/// </summary>
|
|
internal sealed class KpiSourceAdmission
|
|
{
|
|
private readonly object _gate = new();
|
|
private readonly Dictionary<int, Task<IReadOnlyList<KpiSample>>> _tasks = new();
|
|
|
|
internal bool TryStart(
|
|
int registrationIndex,
|
|
Func<Task<IReadOnlyList<KpiSample>>> collect,
|
|
out Task<IReadOnlyList<KpiSample>> collection)
|
|
{
|
|
lock (_gate)
|
|
{
|
|
if (_tasks.TryGetValue(registrationIndex, out var existing) && !existing.IsCompleted)
|
|
{
|
|
collection = existing;
|
|
return false;
|
|
}
|
|
|
|
collection = Task.Run(collect);
|
|
_tasks[registrationIndex] = collection;
|
|
return true;
|
|
}
|
|
}
|
|
}
|