namespace ZB.MOM.WW.Secrets.Replicator.Grpc; /// Point-in-time outcome of the hub follower's sweeps. Never carries secret material. /// /// Registered as a singleton by AddZbSecretsGrpcHubClient alongside the background sweep /// service, so a host can surface hub-sync health (a readiness check, a status page) without /// scraping logs. Every field reflects the outcome of the most recently completed sweep only — /// there is deliberately no history here, just "what happened last time". /// public interface ISecretsHubSyncStatus { /// /// When the most recent sweep finished, whether it succeeded or failed. /// before the first sweep has completed. /// DateTimeOffset? LastSweepUtc { get; } /// /// When the most recent successful sweep finished. if no sweep /// has ever succeeded. /// DateTimeOffset? LastSuccessUtc { get; } /// /// The number of sweeps that have failed in a row, ending at the most recent sweep. Reset to /// zero the moment a sweep succeeds. /// int ConsecutiveFailures { get; } /// /// The message from the most recent sweep failure, or if the most recent /// sweep succeeded (or none has run yet). This is the exception's type and message only — never /// a stack trace, and never secret material, since the sweep never has plaintext in scope. /// string? LastError { get; } } /// The mutable implementation the sync service writes to; consumers see only the interface. /// /// Writes (/) run under , /// and every getter takes the same lock rather than reading its backing field bare — a plain field /// read races the writer with no synchronization otherwise, and a torn/half-written /// (a multi-field struct) or an interleaving that pairs one write's /// LastSweepUtc with another write's LastError is exactly the inconsistency a status /// consumer must not see. /// internal sealed class SecretsHubSyncStatus : ISecretsHubSyncStatus { private readonly object _gate = new(); private DateTimeOffset? _lastSweepUtc; private DateTimeOffset? _lastSuccessUtc; private int _consecutiveFailures; private string? _lastError; /// public DateTimeOffset? LastSweepUtc { get { lock (_gate) { return _lastSweepUtc; } } } /// public DateTimeOffset? LastSuccessUtc { get { lock (_gate) { return _lastSuccessUtc; } } } /// public int ConsecutiveFailures { get { lock (_gate) { return _consecutiveFailures; } } } /// public string? LastError { get { lock (_gate) { return _lastError; } } } /// /// Records a successful sweep, resetting the failure streak. /// /// The instant the sweep completed. /// /// The consecutive-failure count before this success — the caller uses a non-zero /// result to decide whether the sweep is a recovery worth logging. /// internal int RecordSuccess(DateTimeOffset now) { lock (_gate) { int prior = _consecutiveFailures; _lastSweepUtc = _lastSuccessUtc = now; _consecutiveFailures = 0; _lastError = null; return prior; } } /// Records a failed sweep, extending the failure streak. /// The instant the sweep failed. /// The exception the sweep failed with. internal void RecordFailure(DateTimeOffset now, Exception ex) { lock (_gate) { _lastSweepUtc = now; _consecutiveFailures++; _lastError = ex.GetType().Name + ": " + ex.Message; } } }