View SerializerBenchmark.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try | |
{ | |
await client.SendAsync(token); | |
} | |
catch (OperationCanceledException ex) when (ex.CancellationToken == token) | |
{ | |
// detect reason of Cancel by Token | |
} |
View cancel_03.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Client | |
{ | |
public TimeSpan Timeout { get; } | |
public Client(TimeSpan timeout) | |
{ | |
this.Timeout = timeout; | |
} | |
public async Task SendAsync(CancellationToken cancellationToken = default) |
View cancel_02.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dispose will stop internal timer to prevent timer leak | |
using var cts = new CancellationTokenSource(); | |
cts.CancelAfter(TimeSpan.FromMinutes(1)); | |
await client.SendAsync(cts.Token); |
NewerOlder