Skip to content

Instantly share code, notes, and snippets.

Avatar

Yoshifumi Kawai neuecc

View GitHub Profile
View SerializerBenchmark.cs
//<Project Sdk="Microsoft.NET.Sdk">
// <PropertyGroup>
// <OutputType>Exe</OutputType>
// <TargetFramework>net6.0</TargetFramework>
// <ImplicitUsings>enable</ImplicitUsings>
// <Nullable>enable</Nullable>
// </PropertyGroup>
// <ItemGroup>
// <PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
// <PackageReference Include="MessagePack" Version="2.4.35" />
View cancel_10.cs
public async Task SendAsync(CancellationToken cancellationToken = default)
{
var timeoutTokenSource = timeoutTokenSourcePool.Get();
CancellationTokenRegistration externalCancellation = default;
if (cancellationToken.CanBeCanceled)
{
externalCancellation = cancellationToken.UnsafeRegister(static state =>
{
((CancellationTokenSource)state!).Cancel();
View cancel_09.cs
class Client : IDisposable
{
readonly TimeSpan timeout;
readonly ObjectPool<CancellationTokenSource> timeoutTokenSourcePool;
readonly CancellationTokenSource clientLifetimeTokenSource;
public TimeSpan Timeout { get; }
public Client(TimeSpan timeout)
{
View cancel_08.cs
public async Task SendAsync(CancellationToken cancellationToken = default)
{
var timeoutTokenSource = timeoutTokenSourcePool.Get();
CancellationTokenRegistration externalCancellation = default;
if (cancellationToken.CanBeCanceled)
{
// When raised argument CancellationToken, raise Timeout's CancellationToken
externalCancellation = cancellationToken.UnsafeRegister(static state =>
{
View cancel_07.cs
class Client
{
// For classes that prohibit multiple calls to methods such as SqlConnection, add a single CancellationTokenSource to the field
// Things like HttpClient, which may be called multiple times, are held in ObjectPool.
readonly ObjectPool<CancellationTokenSource> timeoutTokenSourcePool;
public TimeSpan Timeout { get; }
public Client(TimeSpan timeout)
View cancel_05.cs
public async Task SendAsync(CancellationToken cancellationToken = default)
{
using var cts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken);
cts.CancelAfter(Timeout);
try
{
await SendCoreAsync(cts.Token);
}
catch (OperationCanceledException ex) when (ex.CancellationToken == cts.Token)
View client_06.cs
class Client : IDisposable
{
// called from IDisposable.Dispose
readonly CancellationTokenSource clientLifetimeTokenSource;
public TimeSpan Timeout { get; }
public Client(TimeSpan timeout)
{
this.Timeout = timeout;
View cancel_04.cs
try
{
await client.SendAsync(token);
}
catch (OperationCanceledException ex) when (ex.CancellationToken == token)
{
// detect reason of Cancel by Token
}
View cancel_03.cs
class Client
{
public TimeSpan Timeout { get; }
public Client(TimeSpan timeout)
{
this.Timeout = timeout;
}
public async Task SendAsync(CancellationToken cancellationToken = default)
View cancel_02.cs
// Dispose will stop internal timer to prevent timer leak
using var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromMinutes(1));
await client.SendAsync(cts.Token);