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

2020-07-18 10:21发布

问题:

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

// ...