diff --git a/src/HouseofCat.RabbitMQ/Options/PoolOptions.cs b/src/HouseofCat.RabbitMQ/Options/PoolOptions.cs index 893e9640..85de785c 100644 --- a/src/HouseofCat.RabbitMQ/Options/PoolOptions.cs +++ b/src/HouseofCat.RabbitMQ/Options/PoolOptions.cs @@ -13,11 +13,17 @@ public class PoolOptions /// public ushort MaxConnections { get; set; } = 2; + /// + /// Number of channels to keep in each of the channel pools. Used in round-robin to perform actions. + /// Default value is 0. + /// + public ushort MaxChannels { get; set; } + /// /// Number of channels to keep in each of the channel pools. Used in round-robin to perform actions. /// Default value is 10. /// - public ushort MaxChannels { get; set; } = 10; + public ushort MaxAckableChannels { get; set; } = 10; /// /// The time to sleep (in ms) when an error occurs on Channel or Connection creation. It's best not to be hyper aggressive with this value. @@ -31,4 +37,12 @@ public class PoolOptions /// Default value is 10000. /// public ulong TansientChannelStartRange { get; set; } = 10000; + + /// + /// Stops the ConnectionPool from creating tracked internal channels. This can be useful + /// if you wish to only use the ConnectionPool for building channels on demand. Transient + /// channels are created on demand and that means this can slow down internally or at the RabbitMQ + /// server if you aren't re-using the transient channel you just created. + /// + public bool OnlyTransientChannels { get; set; } = false; } diff --git a/src/HouseofCat.RabbitMQ/Pools/ChannelPool.cs b/src/HouseofCat.RabbitMQ/Pools/ChannelPool.cs index b4731bd1..5626b6ab 100644 --- a/src/HouseofCat.RabbitMQ/Pools/ChannelPool.cs +++ b/src/HouseofCat.RabbitMQ/Pools/ChannelPool.cs @@ -82,7 +82,10 @@ public ChannelPool(IConnectionPool connPool) _channels = Channel.CreateBounded(Options.PoolOptions.MaxChannels); _ackChannels = Channel.CreateBounded(Options.PoolOptions.MaxChannels); - CreateChannelsAsync().GetAwaiter().GetResult(); + if (!Options.PoolOptions.OnlyTransientChannels) + { + CreateChannelsAsync().GetAwaiter().GetResult(); + } } private async Task CreateChannelsAsync() @@ -96,7 +99,7 @@ await _channels .WriteAsync(chanHost); } - for (int i = 0; i < Options.PoolOptions.MaxChannels; i++) + for (int i = 0; i < Options.PoolOptions.MaxAckableChannels; i++) { var chanHost = await CreateChannelAsync(CurrentChannelId++, true).ConfigureAwait(false); @@ -119,6 +122,12 @@ public async ValueTask GetChannelAsync() { if (Shutdown) throw new InvalidOperationException(ExceptionMessages.ChannelPoolValidationMessage); + if (Options.PoolOptions.OnlyTransientChannels) + { + return await GetTransientChannelAsync(false) + .ConfigureAwait(false); + } + if (!await _channels .Reader .WaitToReadAsync() @@ -157,6 +166,12 @@ public async ValueTask GetAckChannelAsync() { if (Shutdown) throw new InvalidOperationException(ExceptionMessages.ChannelPoolValidationMessage); + if (Options.PoolOptions.OnlyTransientChannels) + { + return await GetTransientChannelAsync(true) + .ConfigureAwait(false); + } + if (!await _ackChannels .Reader .WaitToReadAsync()