Permissions, RateControl and Auditing

This commit is contained in:
2026-07-16 14:01:08 +03:00
parent 3a1b95dbc5
commit ba64a0c95a
62 changed files with 6095 additions and 2366 deletions
+39 -25
View File
@@ -1,51 +1,65 @@
using System;
using System.Collections.Generic;
using System;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace Esiur.Core;
public class AsyncBagAwaiter<T> : INotifyCompletion
{
Action callback = null;
private static readonly Action CompletedSentinel = () => { };
AsyncException exception = null;
T[] result;
private Action continuation;
private AsyncException exception;
private T[] result;
public AsyncBagAwaiter(AsyncBag<T> reply)
{
reply.Then(x =>
{
this.IsCompleted = true;
this.result = x;
this.callback?.Invoke();
}).Error(x =>
{
exception = x;
this.IsCompleted = true;
this.callback?.Invoke();
});
reply.Then(Complete).Error(Fail);
}
public T[] GetResult()
{
if (exception != null)
throw exception;
return result;
}
public bool IsCompleted { get; private set; }
public bool IsCompleted
=> ReferenceEquals(Volatile.Read(ref continuation), CompletedSentinel);
public void OnCompleted(Action continuation)
{
if (IsCompleted)
continuation?.Invoke();
else
// Continue....
callback = continuation;
if (continuation == null)
throw new ArgumentNullException(nameof(continuation));
var previous = Interlocked.CompareExchange(ref this.continuation, continuation, null);
if (ReferenceEquals(previous, CompletedSentinel))
{
continuation();
}
else if (previous != null)
{
throw new InvalidOperationException("The awaiter already has a continuation.");
}
}
private void Complete(T[] value)
{
result = value;
InvokeContinuation();
}
private void Fail(AsyncException value)
{
exception = value;
InvokeContinuation();
}
private void InvokeContinuation()
{
var registeredContinuation = Interlocked.Exchange(ref continuation, CompletedSentinel);
if (registeredContinuation != null && !ReferenceEquals(registeredContinuation, CompletedSentinel))
registeredContinuation();
}
}