SignalR hub scaling issue

We have a SignalR hub called StatusUpdateHub. This hub is updated by a .NET client called HubClient. This client will be (in production), called several thousand times per second by several users for different transactions. Here is the client code:

public static class HubClient { private static readonly string statusUpdateUrl = ConfigurationManager.AppSettings["StatusUpdateUrl"]; private static readonly HubConnection connection = new HubConnection(statusUpdateUrl); private static readonly IHubProxy hub = connection.CreateProxy("StatusUpdateHub"); internal static void UpdateBrowser(long transactionId) { connection.Start().ContinueWith(task => hub.Invoke("UpdateTransactionStatus", transactionId)).ContinueWith(task => { if (task.IsFaulted && task.Exception != null) { // log error } }); } } 

When this code is called with 100 concurrent users, it works fine, but when we increase the number of concurrent users to 250, we see the following error:

Unexpected error in UpdateBrowser: System.InvalidOperationException: Connection failed. in SignalR.Client.Connection.SignalR.Client.IConnection.Send [T] (Data String) in SignalR.Client.Hubs.HubProxy.Invoke [T] (String method, Object [] args) in SignalR.Client.Hubs. HubProxy.Invoke (String method, Object [] args) with Application.Services.HubClient <. > C_DisplayClass2.b_0 (Task task1) in c: \ Build \ Work \ Application \ Services \ HubClient.cs: line 20
in System.Threading.Tasks.Task`1.InvokeFuture (Object futureAsObj)
in System.Threading.Tasks.Task.Execute ()

Let us know how to make the code more scalable?

+7
source share
2 answers

If this method is called 1000 times per second, you should not call connection.Start() every time.

Open the connection only once, and then just call the methods on it.

Edit , I mean that at least make your code something like this:

 internal static void UpdateBrowser(long transactionId) { lock (connection) { if (connection.State == ConnectionState.Disconnected){ connection.Start().Wait(); } } hub.Invoke("UpdateTransactionStatus", transactionId).ContinueWith(task => { if (task.IsFaulted && task.Exception != null) { // log error } }); } 
+9
source

By default, .NET allows you to use 2 simultaneous connections coming from client applications and 10 from ASP.NET applications. If you want to increase this number, set http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.defaultconnectionlimit.aspx to a larger number. See if that helps.

+5
source

All Articles