RawRabbit icon indicating copy to clipboard operation
RawRabbit copied to clipboard

Memeory leak when using ChannelFactory to create channels

Open Ankurkh1 opened this issue 7 years ago • 2 comments

Hi @pardahlman

Once again thanks for this awesome library. If we try to create channels using Channel factory it keeps leaking memory. Even if we explicitly dispose it(or in using scope). My memory profiling tells me protected readonly ConcurrentBag<IModel> Channels; is holding on to the the reference and so GC can never release it.

I think this factory class should not worry about the lifetime of the channel and let the creater manage it.

Ankurkh1 avatar Apr 15 '18 21:04 Ankurkh1

Hello @Ankurkh1 - thanks for reporting this. I think what you are proposing makes a lot of sense. Feel free to submit a PR with the change proposed 👍

pardahlman avatar Apr 16 '18 07:04 pardahlman

Resolved this by introducing a RawRabbit connection pool as IDictionary<string, IBusClient>, where string is RabbitMQ connection string. However, it works only if you need to send a lot of messages to a little amount of queues. If you need to send a lot of messages to a lot of queues, then this won't work — still searching for a solution that covers such cases as well.

public class QueueManager {
    private readonly IDictionary<string, IBusClient> _clients = 
        new ConcurrentDictionary<string, IBusClient>();

    public async Task SendMessage(string connectionString) {
        var connection = ConnectionStringParser.Parse(connectionString);
        if (!_clients.TryGetValue(connectionString, out var bus))
            bus = _clients[connectionString] = BusClientFactory.CreateDefault(connection);
        await bus.PublishAsync(...);
    }
}

worldbeater avatar Apr 16 '19 19:04 worldbeater