namespace ZB.MOM.WW.ScadaBridge.CentralUI.Services;

/// <summary>Reentrancy guard for poll timers: a fixed-period Timer whose refresh outlives
/// the period stacks overlapping fan-outs against an already-degraded site (arch-review S3).
/// Callers TryEnter at tick start and skip the tick when the previous refresh is in flight.</summary>
public sealed class PollGate
{
    private int _inFlight;

    /// <summary>Attempts to enter the gate for a new poll tick.</summary>
    /// <returns><see langword="true"/> if no refresh is currently in flight and the caller
    /// may proceed; <see langword="false"/> if a previous refresh is still running.</returns>
    public bool TryEnter() => Interlocked.CompareExchange(ref _inFlight, 1, 0) == 0;

    /// <summary>Releases the gate after a poll tick's refresh has completed.</summary>
    public void Exit() => Volatile.Write(ref _inFlight, 0);
}
