From 7d1ce6ea22cb764020dc44dcb42620e32968e549 Mon Sep 17 00:00:00 2001 From: Rikitav Date: Sat, 2 Aug 2025 03:33:35 +0400 Subject: [PATCH] * Added mising XML summaries * Obsolete code comments cleanup --- Telegrator/Handlers/Result.cs | 29 ++++ Telegrator/Polling/LimitedDictionary.cs | 26 ++++ Telegrator/Polling/LimitedQueue.cs | 2 + Telegrator/Polling/UpdateHandlersPool.cs | 167 +---------------------- 4 files changed, 58 insertions(+), 166 deletions(-) diff --git a/Telegrator/Handlers/Result.cs b/Telegrator/Handlers/Result.cs index 589e7e0..a72660a 100644 --- a/Telegrator/Handlers/Result.cs +++ b/Telegrator/Handlers/Result.cs @@ -1,11 +1,23 @@ namespace Telegrator.Handlers { + /// + /// Represents handler results, allowing to communicate with router and control aspect execution + /// public sealed class Result { + /// + /// Is result positive + /// public bool Positive { get; } + /// + /// Should router search for next matching handler + /// public bool RouteNext { get; } + /// + /// Exact type that router should search + /// public Type? NextType { get; } internal Result(bool positive, bool routeNext, Type? nextType) @@ -15,15 +27,32 @@ NextType = nextType; } + /// + /// "OK" result + /// + /// public static Result Ok() => new Result(true, false, null); + /// + /// "Somethong went wrong" result + /// + /// public static Result Fault() => new Result(false, false, null); + /// + /// "Search next handler" result + /// + /// public static Result Next() => new Result(true, true, null); + /// + /// "Search next handler of type" result + /// + /// + /// public static Result Next() => new Result(true, true, typeof(T)); } diff --git a/Telegrator/Polling/LimitedDictionary.cs b/Telegrator/Polling/LimitedDictionary.cs index 0d8bd47..b14c3e2 100644 --- a/Telegrator/Polling/LimitedDictionary.cs +++ b/Telegrator/Polling/LimitedDictionary.cs @@ -3,12 +3,21 @@ using System.Collections.Concurrent; namespace Telegrator.Polling { + /// + /// Represents a dictionayr with limited number of slots, if trying to overflow, will block calling thread until one of slots will be free + /// + /// + /// public class LimitedDictionary : IEnumerable>, IDisposable { private readonly int? _maximum; private readonly SemaphoreSlim _semaphore = null!; private readonly ConcurrentDictionary _dict = []; + /// + /// Initializes new instance of + /// + /// public LimitedDictionary(int? maximum) { _maximum = maximum; @@ -19,6 +28,14 @@ namespace Telegrator.Polling } } + /// + /// Tries to add new element to dictioanry. + /// If all slots are occupied, blocks calling thread. + /// + /// + /// + /// + /// public async Task Add(TKey key, TValue value, CancellationToken cancellationToken) { if (_semaphore != null) @@ -27,6 +44,13 @@ namespace Telegrator.Polling return _dict.TryAdd(key, value); } + /// + /// Tries to remove element from dictionay. + /// Frees slot on success. + /// + /// + /// + /// public bool Remove(TKey key, out TValue result) { if (_dict.TryRemove(key, out result)) @@ -38,8 +62,10 @@ namespace Telegrator.Polling return false; } + /// public IEnumerator> GetEnumerator() => _dict.GetEnumerator(); + /// IEnumerator IEnumerable.GetEnumerator() => _dict.GetEnumerator(); /// diff --git a/Telegrator/Polling/LimitedQueue.cs b/Telegrator/Polling/LimitedQueue.cs index 3d0978c..ba940be 100644 --- a/Telegrator/Polling/LimitedQueue.cs +++ b/Telegrator/Polling/LimitedQueue.cs @@ -2,6 +2,7 @@ namespace Telegrator.Polling { + /* public class LimitedQueue { private readonly int? _maximum; @@ -39,4 +40,5 @@ namespace Telegrator.Polling return false; } } + */ } diff --git a/Telegrator/Polling/UpdateHandlersPool.cs b/Telegrator/Polling/UpdateHandlersPool.cs index 2252fe2..8eb3a20 100644 --- a/Telegrator/Polling/UpdateHandlersPool.cs +++ b/Telegrator/Polling/UpdateHandlersPool.cs @@ -1,5 +1,4 @@ -using System.Collections.Concurrent; -using Telegrator.Handlers; +using Telegrator.Handlers; using Telegrator.MadiatorCore; using Telegrator.MadiatorCore.Descriptors; @@ -16,34 +15,11 @@ namespace Telegrator.Polling /// protected object SyncObj = new object(); - /* - /// - /// Event that signals when awaiting handlers are queued. - /// - protected ManualResetEventSlim AwaitingHandlersQueuedEvent = null!; - /// /// Semaphore for controlling the number of concurrently executing handlers. /// protected SemaphoreSlim ExecutingHandlersSemaphore = null!; - /// - /// Queue for storing awaiting handlers. - /// - protected readonly ConcurrentQueue AwaitingHandlersQueue = []; - - /// - /// Dictionary for tracking currently executing handlers. - /// - protected readonly ConcurrentDictionary ExecutingHandlersPool = []; - */ - - //protected readonly ConcurrentDictionary> AwaitingHandlersQueue; - - //protected readonly LimitedDictionary ExecutingHandlersPool; - - protected SemaphoreSlim ExecutingHandlersSemaphore = null!; - /// /// The bot configuration options. /// @@ -74,26 +50,16 @@ namespace Telegrator.Polling { Options = options; GlobalCancellationToken = globalCancellationToken; - //AwaitingHandlersQueue = new ConcurrentDictionary>(); - //ExecutingHandlersPool = new LimitedDictionary(options.MaximumParallelWorkingHandlers); if (options.MaximumParallelWorkingHandlers != null) { ExecutingHandlersSemaphore = new SemaphoreSlim(options.MaximumParallelWorkingHandlers.Value); - //AwaitingHandlersQueuedEvent = new ManualResetEventSlim(false); } - - /* - if (Options.MaximumParallelWorkingHandlers != null) - HandlersCheckpoint(); - */ } /// public async Task Enqueue(IEnumerable handlers) { - //handlers.ForEach(Enqueue); - Result? lastResult = null; foreach (DescribedHandlerInfo handlerInfo in handlers) { @@ -123,137 +89,6 @@ namespace Telegrator.Polling break; } } - - /* - /// - public void Enqueue(DescribedHandlerInfo handlerInfo) - { - throw new NotImplementedException(); - - if (Options.MaximumParallelWorkingHandlers == null) - { - Task.Run(async () => await ExecuteHandlerWrapper(handlerInfo)); - return; - } - - lock (SyncObj) - { - AwaitingHandlersQueue.Enqueue(handlerInfo); - HandlerEnqueued?.Invoke(handlerInfo); - AwaitingHandlersQueuedEvent.Set(); - } - } - - /// - public void Dequeue(HandlerLifetimeToken token) - { - if (Options.MaximumParallelWorkingHandlers == null) - return; - - lock (SyncObj) - { - ExecutingHandlersPool.TryRemove(token, out _); - ExecutingHandlersSemaphore.Release(1); - } - } - */ - - /* - /// - /// Main checkpoint method that manages handler execution in a loop. - /// Continuously processes queued handlers while respecting concurrency limits. - /// - protected virtual async void HandlersCheckpoint() - { - await Task.Yield(); - while (!GlobalCancellationToken.IsCancellationRequested) - { - if (!CanEnqueueHandler()) - { - await ExecutingHandlersSemaphore.WaitAsync(GlobalCancellationToken); - if (!CanEnqueueHandler()) - continue; - } - - if (!TryDequeueHandler(out DescribedHandlerInfo? enqueuedHandler)) - { - AwaitingHandlersQueuedEvent.Reset(); - AwaitingHandlersQueuedEvent.Wait(GlobalCancellationToken); - - if (!TryDequeueHandler(out enqueuedHandler)) - continue; - } - - if (enqueuedHandler == null) - continue; - - ExecuteHandler(enqueuedHandler); - } - } - - /// - /// Executes a handler by creating a lifetime token and tracking the execution. - /// - /// The handler to execute. - protected virtual void ExecuteHandler(DescribedHandlerInfo enqueuedHandler) - { - HandlerLifetimeToken lifetimeToken = enqueuedHandler.HandlerLifetime; - lifetimeToken.OnLifetimeEnded += Dequeue; - - Task executingHandler = ExecuteHandlerWrapper(enqueuedHandler); - lock (SyncObj) - ExecutingHandlersPool.TryAdd(lifetimeToken, executingHandler); - - HandlerExecuting?.Invoke(enqueuedHandler); - } - - /// - /// Wrapper method that executes a handler and handles exceptions. - /// - /// The handler to execute. - /// A task representing the asynchronous execution. - /// Thrown when the handler execution fails. - protected virtual async Task ExecuteHandlerWrapper(DescribedHandlerInfo enqueuedHandler) - { - try - { - await enqueuedHandler.Execute(GlobalCancellationToken); - } - catch (OperationCanceledException) - { - return; - } - catch (Exception ex) - { - throw new HandlerFaultedException(enqueuedHandler, ex); - } - } - - /// - /// Checks if a new handler can be enqueued based on the current execution count. - /// - /// True if a new handler can be enqueued; otherwise, false. - protected virtual bool CanEnqueueHandler() - { - lock (SyncObj) - { - return ExecutingHandlersPool.Count < Options.MaximumParallelWorkingHandlers; - } - } - - /// - /// Attempts to dequeue a handler from the awaiting queue. - /// - /// The dequeued handler, if successful. - /// True if a handler was successfully dequeued; otherwise, false. - protected virtual bool TryDequeueHandler(out DescribedHandlerInfo? enqueuedHandler) - { - lock (SyncObj) - { - return AwaitingHandlersQueue.TryDequeue(out enqueuedHandler); - } - } - */ /// /// Disposes of the handlers pool and releases all resources.