fix(ai): meter synchronous generations
This commit is contained in:
@@ -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--;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user