Skip to content

Commit

Permalink
Configure the ChannelPool to only use TransientChannels.
Browse files Browse the repository at this point in the history
  • Loading branch information
houseofcat committed Mar 16, 2024
1 parent 016cfa5 commit ee3484c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
16 changes: 15 additions & 1 deletion src/HouseofCat.RabbitMQ/Options/PoolOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ public class PoolOptions
/// </summary>
public ushort MaxConnections { get; set; } = 2;

/// <summary>
/// Number of channels to keep in each of the channel pools. Used in round-robin to perform actions.
/// <para>Default value is 0.</para>
/// </summary>
public ushort MaxChannels { get; set; }

/// <summary>
/// Number of channels to keep in each of the channel pools. Used in round-robin to perform actions.
/// <para>Default value is 10.</para>
/// </summary>
public ushort MaxChannels { get; set; } = 10;
public ushort MaxAckableChannels { get; set; } = 10;

/// <summary>
/// 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.
Expand All @@ -31,4 +37,12 @@ public class PoolOptions
/// <para>Default value is 10000.</para>
/// </summary>
public ulong TansientChannelStartRange { get; set; } = 10000;

/// <summary>
/// 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.
/// </summary>
public bool OnlyTransientChannels { get; set; } = false;
}
19 changes: 17 additions & 2 deletions src/HouseofCat.RabbitMQ/Pools/ChannelPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ public ChannelPool(IConnectionPool connPool)
_channels = Channel.CreateBounded<IChannelHost>(Options.PoolOptions.MaxChannels);
_ackChannels = Channel.CreateBounded<IChannelHost>(Options.PoolOptions.MaxChannels);

CreateChannelsAsync().GetAwaiter().GetResult();
if (!Options.PoolOptions.OnlyTransientChannels)
{
CreateChannelsAsync().GetAwaiter().GetResult();
}
}

private async Task CreateChannelsAsync()
Expand All @@ -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);

Expand All @@ -119,6 +122,12 @@ public async ValueTask<IChannelHost> GetChannelAsync()
{
if (Shutdown) throw new InvalidOperationException(ExceptionMessages.ChannelPoolValidationMessage);

if (Options.PoolOptions.OnlyTransientChannels)
{
return await GetTransientChannelAsync(false)
.ConfigureAwait(false);
}

if (!await _channels
.Reader
.WaitToReadAsync()
Expand Down Expand Up @@ -157,6 +166,12 @@ public async ValueTask<IChannelHost> GetAckChannelAsync()
{
if (Shutdown) throw new InvalidOperationException(ExceptionMessages.ChannelPoolValidationMessage);

if (Options.PoolOptions.OnlyTransientChannels)
{
return await GetTransientChannelAsync(true)
.ConfigureAwait(false);
}

if (!await _ackChannels
.Reader
.WaitToReadAsync()
Expand Down

0 comments on commit ee3484c

Please sign in to comment.