* Changed update awaiting mechanism. Uses TaskCompletationSource, instead of ManualResetEvent

* Updated Dependencies
* Version incremented
This commit is contained in:
gutii
2026-04-23 21:29:20 +04:00
parent d4a5d3d6ee
commit 5dbf7f4073
9 changed files with 34 additions and 25 deletions
+1 -1
View File
@@ -4529,7 +4529,7 @@
Gets the update that triggered this awaiter handler. Gets the update that triggered this awaiter handler.
</summary> </summary>
</member> </member>
<member name="M:Telegrator.Handlers.Building.AwaiterHandler.Wait(System.Threading.CancellationToken)"> <member name="M:Telegrator.Handlers.Building.AwaiterHandler.Await(System.Threading.CancellationToken)">
<summary> <summary>
Waits for the specified update type to be received. Waits for the specified update type to be received.
</summary> </summary>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting.Web</Title> <Title>Telegrator.Hosting.Web</Title>
<Version>1.16.8</Version> <Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -15,7 +15,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator.Hosting</Title> <Title>Telegrator.Hosting</Title>
<Version>1.16.8</Version> <Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -31,8 +31,8 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.5" /> <PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.7" />
<PackageReference Include="Microsoft.Extensions.Http" Version="10.0.5" /> <PackageReference Include="Microsoft.Extensions.Http" Version="10.0.7" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@@ -6,7 +6,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="10.0.2" /> <PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="10.0.7" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
+1
View File
@@ -9,3 +9,4 @@ using System.Diagnostics.CodeAnalysis;
[assembly: SuppressMessage("Style", "IDE0090")] [assembly: SuppressMessage("Style", "IDE0090")]
[assembly: SuppressMessage("Style", "IDE0057")] [assembly: SuppressMessage("Style", "IDE0057")]
[assembly: SuppressMessage("Style", "IDE0270")] [assembly: SuppressMessage("Style", "IDE0270")]
[assembly: SuppressMessage("Roslynator", "RCS1037")]
@@ -15,7 +15,7 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// <summary> /// <summary>
/// Manual reset event used for synchronization. /// Manual reset event used for synchronization.
/// </summary> /// </summary>
private ManualResetEventSlim ResetEvent = new ManualResetEventSlim(false); private readonly TaskCompletionSource<Update> ResetEvent = new TaskCompletionSource<Update>();
/// <summary> /// <summary>
/// Gets the update that triggered this awaiter handler. /// Gets the update that triggered this awaiter handler.
@@ -26,10 +26,10 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// Waits for the specified update type to be received. /// Waits for the specified update type to be received.
/// </summary> /// </summary>
/// <param name="cancellationToken">The cancellation token to cancel the wait operation.</param> /// <param name="cancellationToken">The cancellation token to cancel the wait operation.</param>
public void Wait(CancellationToken cancellationToken) public async Task<Update> Await(CancellationToken cancellationToken)
{ {
ResetEvent.Reset(); cancellationToken.ThrowIfCancellationRequested();
ResetEvent.Wait(cancellationToken); return await ResetEvent.Task.ConfigureAwait(false);
} }
/// <summary> /// <summary>
@@ -51,9 +51,19 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
/// <returns>A completed task.</returns> /// <returns>A completed task.</returns>
protected override Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellation) protected override Task<Result> ExecuteInternal(IHandlerContainer container, CancellationToken cancellation)
{ {
ResetEvent.Set(); try
{
if (!ResetEvent.TrySetResult(HandlingUpdate))
ResetEvent.TrySetCanceled(cancellation);
return Task.FromResult(Result.Ok()); return Task.FromResult(Result.Ok());
} }
catch (Exception ex)
{
ResetEvent.TrySetException(ex);
return Task.FromResult(Result.Fault());
}
}
/// <inheritdoc/> /// <inheritdoc/>
protected override bool Dispose(bool disposing) protected override bool Dispose(bool disposing)
@@ -61,8 +71,7 @@ internal class AwaiterHandler(UpdateType handlingUpdateType) : UpdateHandlerBase
if (!disposing) if (!disposing)
return true; return true;
ResetEvent.Dispose(); ResetEvent.TrySetCanceled();
ResetEvent = null!;
return true; return true;
} }
} }
@@ -76,10 +76,8 @@ public class AwaiterHandlerBuilder<TUpdate> : HandlerBuilderBase, IAwaiterHandle
using (HandlerProvider.UseHandler(descriptor)) using (HandlerProvider.UseHandler(descriptor))
{ {
handlerInstance.Wait(cancellationToken); await handlerInstance.Await(cancellationToken);
}
await Task.CompletedTask;
return handlerInstance.HandlingUpdate.GetActualUpdateObject<TUpdate>(); return handlerInstance.HandlingUpdate.GetActualUpdateObject<TUpdate>();
} }
} }
}
+1 -1
View File
@@ -46,7 +46,7 @@ public class AwaitingProvider(TelegratorOptions options) : HandlersProvider([],
public readonly void Register() public readonly void Register()
{ {
if (handlerDescriptor.SingletonInstance == null) if (handlerDescriptor.SingletonInstance == null)
throw new Exception(); throw new Exception("Handler descriptor has no singleton instance.");
handlersList.Add(handlerDescriptor); handlersList.Add(handlerDescriptor);
} }
+5 -4
View File
@@ -14,7 +14,7 @@
<EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild> <EnforceCodeStyleInBuild>True</EnforceCodeStyleInBuild>
<Title>Telegrator : Telegram.Bot mediator framework</Title> <Title>Telegrator : Telegram.Bot mediator framework</Title>
<Version>1.16.8</Version> <Version>1.16.9</Version>
<Authors>Rikitav Tim4ik</Authors> <Authors>Rikitav Tim4ik</Authors>
<Company>Rikitav Tim4ik</Company> <Company>Rikitav Tim4ik</Company>
<RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl> <RepositoryUrl>https://github.com/Rikitav/Telegrator</RepositoryUrl>
@@ -23,6 +23,7 @@
<PackageIcon>telegrator_nuget.png</PackageIcon> <PackageIcon>telegrator_nuget.png</PackageIcon>
<PackageReadmeFile>README.md</PackageReadmeFile> <PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseFile>LICENSE</PackageLicenseFile> <PackageLicenseFile>LICENSE</PackageLicenseFile>
<UserSecretsId>b78bc62d-e49f-4ef0-b2de-ff4ceb3971af</UserSecretsId>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
@@ -32,9 +33,9 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.5" /> <PackageReference Include="Telegram.Bot" Version="22.9.6.1" />
<PackageReference Include="System.Threading.Channels" Version="10.0.5" /> <PackageReference Include="System.Threading.Channels" Version="10.0.7" />
<PackageReference Include="Telegram.Bot" Version="22.9.5.3" /> <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.7" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>