本文记录一个开发和代码审查过程中,需要关注的细节。在 dotnet 里,在 .NET 6 和以下版本,包括 .NET Framework 版本,使用 NamedPipeClientStream 进行连接管道服务,如果此时的管道服务没有存在,或者还没有启动,调用 ConnectAsync 或 Connect 方法,将会进入一个循环,不断进行空跑,等待超时或者是连接上。默认的 ConnectAsync 或 Connect 方法,传入的超时时间都是无穷,也就是将会无限重试,不断消耗 CPU 资源
咱可以使用 NamedPipeClientStream 去连接一个管道服务,从而建立多进程之间的通讯。在连接时,最好是先有管道服务启动,然后再启动管道客户端 NamedPipeClientStream 进行连接。因为如果在 NamedPipeClientStream 开始 Connect 时,还不存在管道服务,那将有一段时间进行 CPU 的空跑
不过好在 Connect 底层实现上,采用了 SpinWait 的 SpinOnce 方法进行自旋,此自旋是一个混合自旋方式,当次数多的时候,将会自动出让 CPU 执行权。但是如果等待连接的数量足够多,依然会占用一定的 CPU 资源,占用多少,取决于 CPU 的价格(价格约等于性能)哈。如以下的代码,将会不断去连接一个不存在的管道名
using System.IO.Pipes;
using System.Security.Principal;
for (int i = 0; i < 1000; i++)
{
var namedPipeClientStream = new NamedPipeClientStream(".", "NotExists_" + i, PipeDirection.Out,
PipeOptions.None, TokenImpersonationLevel.Impersonation);
// Task.Factory.StartNew(namedPipeClientStream.Connect, TaskCreationOptions.LongRunning);
_ = namedPipeClientStream.ConnectAsync();
}
Console.Read();
尝试运行以上的代码,可以看到 CPU 将会不断上升。使用 ConnectAsync 版本,线程数量上升较慢,同时 CPU 上升速度也较慢。如使用被注释的 Task.Factory.StartNew(namedPipeClientStream.Connect, TaskCreationOptions.LongRunning)
代码,那可以看到 CPU 将会快速被占用,线程也有大量的数量
因此在开发的时候,如果需要使用 NamedPipeClientStream 进行 Connect 或 ConnectAsync 连接,除非能明确管道的服务端已创建成功,否则都推荐加上超时逻辑。不然,在尝试连接一个不存在的服务管道名,将会占用线程,不断空跑。数量少的时候,没有什么影响,数量多的时候,将会浪费 CPU 资源
如果关心 .NET 的底层实现,为什么会有此问题,请继续阅读
在 .NET 6 和以下版本,包括 .NET Framework 版本,使用 NamedPipeClientStream 的 ConnectAsync 方法,本质上相当于使用 Task.Run
包一个 Connect 方法,如以下的 .NET 6 有删减的代码。为了让本文清晰,本文以下就只讨论使用 Connect 方法的逻辑
public sealed partial class NamedPipeClientStream : PipeStream
{
// 为了让文章清晰,删减部分代码
public void Connect()
{
Connect(Timeout.Infinite);
}
public void Connect(int timeout)
{
ConnectInternal(timeout, CancellationToken.None, Environment.TickCount);
}
public Task ConnectAsync()
{
// We cannot avoid creating lambda here by using Connect method
// unless we don't care about start time to be measured before the thread is started
return ConnectAsync(Timeout.Infinite, CancellationToken.None);
}
public Task ConnectAsync(int timeout, CancellationToken cancellationToken)
{
int startTime = Environment.TickCount; // We need to measure time here, not in the lambda
return Task.Run(() => ConnectInternal(timeout, cancellationToken, startTime), cancellationToken);
}
private void ConnectInternal(int timeout, CancellationToken cancellationToken, int startTime)
{
// 连接的代码
}
}
通过如上代码可以了解到,实际的连接代码是放在 ConnectInternal 方法里面。在 .NET Framework 下的代码也是差不多的,细节可以忽略
在 ConnectInternal 方法里面,将会进入一个循环,此循环的退出条件只有超时
private void ConnectInternal(int timeout, CancellationToken cancellationToken, int startTime)
{
// This is the main connection loop. It will loop until the timeout expires.
int elapsed = 0;
SpinWait sw = default;
do
{
cancellationToken.ThrowIfCancellationRequested();
// Determine how long we should wait in this connection attempt
int waitTime = timeout - elapsed;
if (cancellationToken.CanBeCanceled && waitTime > CancellationCheckInterval)
{
waitTime = CancellationCheckInterval;
}
// Try to connect.
if (TryConnect(waitTime, cancellationToken))
{
return;
}
// Some platforms may return immediately from TryConnect if the connection could not be made,
// e.g. WaitNamedPipe on Win32 w