SignalR 实现等待接收任务

SignalR implementation awaits receiving task

SignalR 有两个不同的分支,我想知道为什么其中一个正在等待 receiving 而另一个不是。什么原因?

else
{
    // We're waiting on the websocket to close and there are 2 things it could be doing
    // 1. Waiting for websocket data
    // 2. Waiting on a flush to complete (backpressure being applied)

    _aborted = true;

    // Abort the websocket if we're stuck in a pending receive from the client
    socket.Abort();

    // Cancel any pending flush so that we can quit
    _application.Output.CancelPendingFlush();
}

else
{
    Log.WaitingForClose(_logger);

    // We're waiting on the websocket to close and there are 2 things it could be doing
    // 1. Waiting for websocket data
    // 2. Waiting on a flush to complete (backpressure being applied)

    using (var delayCts = new CancellationTokenSource())
    {
        var resultTask = await Task.WhenAny(receiving, Task.Delay(_options.CloseTimeout, delayCts.Token));

        if (resultTask != receiving)
        {
            // Abort the websocket if we're stuck in a pending receive from the client
            _aborted = true;

            socket.Abort();

            // Cancel any pending flush so that we can quit
            _application.Output.CancelPendingFlush();
        }
        else
        {
            delayCts.Cancel();
        }
    }
}

他们在等待两件不同的事情:

  1. 有一个从 websocket 读取并写入应用程序管道的任务
  2. 有一个任务从应用程序管道读取并写入 websocket。

第一个分支正在处理 1。 第二个分支正在处理 2.

希望评论是解释性的。他们详细说明了任何一方都可能陷入困境的情况以及我们 abort/cancel 的结果。