Can I reduce the Circular Buffer to “1”? Is that a good idea?

大兔子大兔子 提交于 2019-12-04 10:07:44

Even if you set the DefaultMessageBufferSize to 1, SignalR ensures each buffer will hold at least 32 messages.

The primary purpose of this minimum buffer size is to ensure that SignalR's long-polling transport works somewhat reliably. If the buffer size was actually 1, a client connected via long-polling would be very likely to miss messages between polls.

I understand that there are some applications where only the last message matters. Unfortantely, as of now, SignalR does not have a "volitile" messaging configuration. Setting the buffer size to 32 is about as good as it gets. At least the client shouldn't lag too far behind with that small of a buffer size.

You are correct in assuming that there are multiple buffers, but buffer sizes cannot be configured individually. SignalR creates one ring buffer per "signal". A "signal" can be a connection id, group name, user name, PersistentConnection name (for when you call Connection.Broadcast), and Hub name (for when you call Clients.All). If you use Clients.All inside of multiple methods in a single hub, all those calls will end up in a single buffer.

EDIT: If you want to configure another SignalR endpoint with different settings inside of the same application you can do the following:

using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Configuration;
using Owin;

// ...

public void Configuration(IAppBuilder app)
{
    // The following will setup a SignalR endpoint at "/signalr"
    // using settings from GlobalHost
    app.MapSignalR();

    var resolver = new DefaultDependencyResolver();
    var configuration = resolver.Resolve<IConfigurationManager>();

    configuration.DefaultMessageBufferSize = 32;

    // By specifying or own dependency resolver, we tell the
    // "/volatile" endpoint not to use settings from GlobalHost
    app.MapSignalR("/volatile", new HubConfiguration
    {
        Resolver = resolver
    });
}

// ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!