fix(ai): meter synchronous generations
CI and Deploy / test (pull_request) Successful in 5m18s
CI and Deploy / deploy (pull_request) Has been skipped

This commit is contained in:
cesnimda
2026-08-15 20:25:10 +02:00
parent dbf28b97ce
commit 25a6da951e
9 changed files with 360 additions and 21 deletions
@@ -0,0 +1,25 @@
namespace JobTrackerApi.Services;
public sealed class AiUsageExecutionScope
{
private readonly AsyncLocal<int> _suppressionDepth = new();
public bool IsSuppressed => _suppressionDepth.Value > 0;
public IDisposable Suppress()
{
_suppressionDepth.Value++;
return new Restore(this);
}
private sealed class Restore(AiUsageExecutionScope owner) : IDisposable
{
private AiUsageExecutionScope? _owner = owner;
public void Dispose()
{
var current = Interlocked.Exchange(ref _owner, null);
if (current is not null) current._suppressionDepth.Value--;
}
}
}