Allow processing of incoming invocations in parallel (#143)
* Allow processing of other incoming invocations in parallel - Don't wait on the response of an invocation to pick up the next message from the channel. - Unhandled exceptions should continue bubbling up correctly
This commit is contained in:
parent
354daa2ded
commit
1e957a9e5a
|
|
@ -7,6 +7,7 @@ using System.IO;
|
||||||
using System.IO.Pipelines;
|
using System.IO.Pipelines;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Microsoft.AspNetCore.Sockets;
|
using Microsoft.AspNetCore.Sockets;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
|
@ -143,7 +144,16 @@ namespace Microsoft.AspNetCore.SignalR
|
||||||
{
|
{
|
||||||
var invocationAdapter = _registry.GetInvocationAdapter(connection.Metadata.Get<string>("formatType"));
|
var invocationAdapter = _registry.GetInvocationAdapter(connection.Metadata.Get<string>("formatType"));
|
||||||
|
|
||||||
while (await connection.Transport.Input.WaitToReadAsync())
|
// We use these for error handling. Since we dispatch multiple hub invocations
|
||||||
|
// in parallel, we need a way to communicate failure back to the main processing loop. The
|
||||||
|
// cancellation token is used to stop reading from the channel, the tcs
|
||||||
|
// is used to get the exception so we can bubble it up the stack
|
||||||
|
var cts = new CancellationTokenSource();
|
||||||
|
var tcs = new TaskCompletionSource<object>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
while (await connection.Transport.Input.WaitToReadAsync(cts.Token))
|
||||||
{
|
{
|
||||||
Message incomingMessage;
|
Message incomingMessage;
|
||||||
while (connection.Transport.Input.TryRead(out incomingMessage))
|
while (connection.Transport.Input.TryRead(out incomingMessage))
|
||||||
|
|
@ -168,6 +178,43 @@ namespace Microsoft.AspNetCore.SignalR
|
||||||
_logger.LogDebug("Received hub invocation: {invocation}", invocationDescriptor);
|
_logger.LogDebug("Received hub invocation: {invocation}", invocationDescriptor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Don't wait on the result of execution, continue processing other
|
||||||
|
// incoming messages on this connection.
|
||||||
|
var ignore = ProcessInvocation(connection, invocationAdapter, invocationDescriptor, cts, tcs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (OperationCanceledException)
|
||||||
|
{
|
||||||
|
// Await the task so the exception bubbles up to the caller
|
||||||
|
await tcs.Task;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ProcessInvocation(Connection connection,
|
||||||
|
IInvocationAdapter invocationAdapter,
|
||||||
|
InvocationDescriptor invocationDescriptor,
|
||||||
|
CancellationTokenSource cts,
|
||||||
|
TaskCompletionSource<object> tcs)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
// If an unexpected exception occurs then we want to kill the entire connection
|
||||||
|
// by ending the processing loop
|
||||||
|
await Execute(connection, invocationAdapter, invocationDescriptor);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// Set the exception on the task completion source
|
||||||
|
tcs.TrySetException(ex);
|
||||||
|
|
||||||
|
// Cancel reading operation
|
||||||
|
cts.Cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task Execute(Connection connection, IInvocationAdapter invocationAdapter, InvocationDescriptor invocationDescriptor)
|
||||||
|
{
|
||||||
InvocationResultDescriptor result;
|
InvocationResultDescriptor result;
|
||||||
Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>> callback;
|
Func<Connection, InvocationDescriptor, Task<InvocationResultDescriptor>> callback;
|
||||||
if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback))
|
if (_callbacks.TryGetValue(invocationDescriptor.Method, out callback))
|
||||||
|
|
@ -201,8 +248,6 @@ namespace Microsoft.AspNetCore.SignalR
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void InitializeHub(THub hub, Connection connection)
|
private void InitializeHub(THub hub, Connection connection)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue