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

匆匆过客 提交于 2019-12-06 05:39:44

问题


By default, there are a default of 1,000 messages stored in the ring buffer on the server.

It doesn't make sense for me to send a lagging client 1,000 updates, but rather just the most recent update. In WCF I can do this by using volatile data.

I suppose I can emulate a volatile approach by reducing the buffer to "1", but not sure if this can be configured on a per hub basis, or ideally, on a per method basis.

Does it matter if I use hubs or persistent connections with this?


回答1:


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
    });
}

// ...


来源:https://stackoverflow.com/questions/25263177/can-i-reduce-the-circular-buffer-to-1-is-that-a-good-idea

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