< Summary

Information
Class: Intentum.Runtime.Resilience.Degradation.MemoryDegradationPolicy
Assembly: Intentum.Runtime
File(s): /home/runner/work/Intentum/Intentum/src/Intentum.Runtime/Resilience/Degradation/MemoryDegradationPolicy.cs
Line coverage
100%
Covered lines: 47
Uncovered lines: 0
Coverable lines: 47
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
ExecuteAsync()100%22100%
Reset()100%11100%
OnSuccess()100%11100%
OnFailure()100%44100%
CheckRecovery()100%44100%

File(s)

/home/runner/work/Intentum/Intentum/src/Intentum.Runtime/Resilience/Degradation/MemoryDegradationPolicy.cs

#LineLine coverage
 1namespace Intentum.Runtime.Resilience.Degradation;
 2
 3public sealed class MemoryDegradationPolicy : IDegradationPolicy
 4{
 5    private readonly DegradationOptions _options;
 56    private readonly object _lock = new();
 7    private int _consecutiveFailures;
 8    private DateTime? _degradedSince;
 9
 510    public MemoryDegradationPolicy(DegradationOptions options)
 511    {
 512        _options = options;
 513    }
 14
 15    public bool IsDegraded
 16    {
 17        get
 1218        {
 1219            CheckRecovery();
 1220            return _consecutiveFailures >= _options.DegradationThreshold;
 1221        }
 22    }
 23
 24    public async Task<T> ExecuteAsync<T>(Func<Task<T>> operation, Func<T> degradedFallback)
 825    {
 826        if (IsDegraded)
 127            return degradedFallback();
 28
 29        try
 730        {
 731            var result = await operation();
 232            OnSuccess();
 233            return result;
 34        }
 535        catch
 536        {
 537            OnFailure();
 538            throw;
 39        }
 340    }
 41
 42    public void Reset()
 243    {
 244        lock (_lock)
 245        {
 246            _consecutiveFailures = 0;
 247            _degradedSince = null;
 248        }
 249    }
 50
 51    private void OnSuccess()
 252    {
 253        lock (_lock)
 254            _consecutiveFailures = 0;
 255    }
 56
 57    private void OnFailure()
 558    {
 559        lock (_lock)
 560        {
 561            _consecutiveFailures++;
 562            if (_consecutiveFailures >= _options.DegradationThreshold)
 463                _degradedSince ??= DateTime.UtcNow;
 564        }
 565    }
 66
 67    private void CheckRecovery()
 1268    {
 1269        if (_degradedSince.HasValue &&
 1270            DateTime.UtcNow - _degradedSince.Value >= _options.CheckInterval)
 171        {
 172            Reset();
 173        }
 1274    }
 75}