SignalR 服务器内存消耗(归咎于 Windsor IHubActivator?)

SignalR server memory consumption (Windsor IHubActivator to blame?)

我正在测试 SignalR 服务器(在控制台应用程序中自行托管),它最终将构成数据记录系统的基础。一个测试客户端正在向集线器发送大约 180 calls/sec,而消息本身非常小(只有 name/value 对)。

我将 Castle Windsor 用于 DI,使用自定义 IHubActivator 来解析 Hub 实例:

internal class WindsorHubActivator : IHubActivator
{
    private readonly IWindsorContainer _container;

    public WindsorHubActivator(IWindsorContainer container)
    {
        _container = container;
    }

    public IHub Create(HubDescriptor descriptor)
    {
        var hubType = descriptor.HubType;
        return _container.Resolve(hubType) as IHub;
    }
}

这里是枢纽 class:

public class TelemetryHub : Hub
{
    private readonly TelemetryDataService _telemetryDataService;

    public TelemetryHub(TelemetryDataService telemetryDataService)
    {
        _telemetryDataService = telemetryDataService;
    }

    public void LogTelemetryData(string name, double value)
    {
        _telemetryDataService.LogTelemetryData(name, value);
    }
}

当集线器 class 在 Windsor 注册为 "Transient" 时,内存消耗稳步攀升,直到达到 2Gb,然后因 OOM 异常而倒下。如果我改为将集线器注册为 "Singleton",那么应用程序内存消耗将保持非常低且一致。

TelemetryDataService class 不是问题。 hub的构造函数和方法代码我都注释掉了,问题依旧。

出于好奇,我将事情更进一步并更改了 WindsorHubActivator class 以将温莎排除在外:

internal class WindsorHubActivator : IHubActivator
{
    ...

    public IHub Create(HubDescriptor descriptor)
    {
        return new TelemetryHub(new TelemetryDataService());
    }
}

这次内存问题消失了,所以我假设 Windsor 正在保留创建的集线器实例并防止它们被垃圾回收。解决办法是什么?我知道不建议使用单例集线器,我不想让 IHubActivator 处于上述 "hardcoded" 状态。

对于显式 Resolved 来自容器的临时组件,you need to explicitly Release them to let Windsor know it can release its reference to them:

Tran­sient com­po­nents are sim­i­lar to pooled, because there’s no well known end to tran­sient component’s life­time, and Wind­sor will not know if you still want to use a com­po­nent or not, unless you explic­itly tell it (by call­ing Release). Since tran­sient com­po­nents are by def­i­n­i­tion non-shared Wind­sor will imme­di­ately destroy the com­po­nent when you Release it.

所以,我相信你有两个选择:

  1. 如果你知道 IHub.Dispose() 会在哪里被调用,你可以在那里调用 container.Release。这可能不是最好的方法,因为您会将 IHub 的使用与它的创建方式结合起来,这是一个大问题(如果不是 问题) 首先使用 IOC 容器解决。

  2. 如果您不知道 IHub.Dispose() 将在哪里被调用,您可以将集线器和容器包装在一个对象中,以便为您管理。像这样:

    public class HubContainerWrapper : IHub, IDisposable
    {
        IWindsorContainer container;
        IHub hub;
    
        public HubContainerWrapper (IWindsorContainer container, IHub hub)
        {
            this.container = container;
            this.hub = hub;
        }
    
        ~HubContainerWrapper()
        {
            Dispose(false);
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (hub != null)
                {
                    try
                    {
                        hub.Dispose();
                    }
                    finally
                    {
                        container.Release(hub);
                        container = null;
                        hub = null;
                    }
                }                   
            }
        }
    
        // forward all IHub calls to hub member
    }
    

    然后在你的 IHubActivator:

    public IHub Create(HubDescriptor descriptor)
    {
        var hubType = descriptor.HubType;
        var hub = _container.Resolve(hubType) as IHub;
        return new HubContainerWrapper(_container, hub);
    }
    

    这样,当 SignalR 处理您的集线器包装器时,它将释放您从容器中解析的实际集线器。

public class Startup
{
    public void Configuration(IAppBuilder app)
    {
        //this value default is 1000
        GlobalHost.Configuration.DefaultMessageBufferSize = 32;
        app.MapSignalR();
    }
}