Refactor payouts processing (#6314)

This commit is contained in:
Nicolas Dorier
2024-10-19 21:33:34 +09:00
committed by GitHub
parent 62d765125d
commit cc0ea0b3f8
17 changed files with 596 additions and 447 deletions

View File

@@ -0,0 +1,38 @@
#nullable enable
using System;
using System.Collections.Concurrent;
namespace BTCPayServer
{
public class ResourceTracker<T> where T: notnull
{
public class ScopedResourceTracker : IDisposable
{
private ResourceTracker<T> _parent;
public ScopedResourceTracker(ResourceTracker<T> resourceTracker)
{
_parent = resourceTracker;
}
ConcurrentDictionary<T, string> _Scoped = new();
public bool TryTrack(T resource)
{
if (!_parent._TrackedResources.TryAdd(resource, string.Empty))
return false;
_Scoped.TryAdd(resource, string.Empty);
return true;
}
public bool Contains(T resource) => _Scoped.ContainsKey(resource);
public void Dispose()
{
foreach (var d in _Scoped)
_parent._TrackedResources.TryRemove(d.Key, out _);
}
}
internal ConcurrentDictionary<T, string> _TrackedResources = new();
public ScopedResourceTracker StartTracking() => new ScopedResourceTracker(this);
public bool Contains(T resource) => _TrackedResources.ContainsKey(resource);
}
}