Skip to content

Commit 21f65a8

Browse files
committed
Add setting to configure RequestedChannelMax on ConnectionFactory
1 parent 7886436 commit 21f65a8

4 files changed

Lines changed: 18 additions & 25 deletions

File tree

Socolin.RabbitMQ.Client.Tests.Integration/CancelActiveConsumerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ public async Task CanCancelAndInterruptCurrentProcessing()
105105
messageCount.Should().Be(1);
106106
completed.Should().Be(false);
107107
}
108-
}
108+
}

Socolin.RabbitMQ.Client.Tests.Integration/DelayedRetryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,4 @@ await _serviceClient.StartListeningQueueAsync(_queueName, consumerOptions, (mess
9494
actualMessages.Should().HaveCount(3);
9595
(await _serviceClient.GetMessageCountInQueueAsync(_queueName)).Should().Be(0);
9696
}
97-
}
97+
}

Socolin.RabbitMQ.Client/RabbitMqChannelManager.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,16 @@ public class RabbitMqChannelManager(
1919
string connectionName,
2020
TimeSpan connectionTimeout,
2121
ChannelType channelType,
22-
TimeSpan requestedHeartbeat
22+
TimeSpan requestedHeartbeat,
23+
ushort maxChannelPoolSize
2324
) : IRabbitMqChannelManager
2425
{
25-
private const int MaxChannelPool = 90;
26-
2726
private readonly SemaphoreSlim _connectionLock = new(1);
2827
private IConnection? _connection;
2928

3029
private readonly ConcurrentBag<IChannel> _availableChannelPool = new();
3130
private int _usedChannelCount;
3231

33-
public RabbitMqChannelManager(
34-
Uri uri,
35-
string connectionName,
36-
TimeSpan connectionTimeout,
37-
ChannelType channelType
38-
)
39-
: this(uri, connectionName, connectionTimeout, channelType, ConnectionFactory.DefaultHeartbeat)
40-
{
41-
}
42-
4332
public async Task<ChannelContainer> AcquireChannelAsync()
4433
{
4534
if (_connection != null)
@@ -59,6 +48,7 @@ public async Task<ChannelContainer> AcquireChannelAsync()
5948
Uri = uri,
6049
AutomaticRecoveryEnabled = true,
6150
RequestedHeartbeat = requestedHeartbeat,
51+
RequestedChannelMax = maxChannelPoolSize,
6252
};
6353

6454
_connection = await connectionFactory.CreateConnectionAsync(connectionName + ":" + channelType);
@@ -84,9 +74,6 @@ private async Task<ChannelContainer> GetOrCreateChannelAsync()
8474
}
8575
}
8676

87-
if (_usedChannelCount > MaxChannelPool)
88-
throw new Exception("Too many channel allocated");
89-
9077
var channel = await _connection!.CreateChannelAsync();
9178

9279
Interlocked.Increment(ref _usedChannelCount);
@@ -113,4 +100,4 @@ public void Dispose()
113100
{
114101
ClearConnection();
115102
}
116-
}
103+
}

Socolin.RabbitMQ.Client/RabbitMqConnectionManager.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Socolin.RabbitMQ.Client;
88
public enum ChannelType
99
{
1010
Publish,
11-
Consumer
11+
Consumer,
1212
}
1313

1414
[PublicAPI]
@@ -18,14 +18,20 @@ public interface IRabbitMqConnectionManager : IDisposable
1818
void ReleaseChannel(ChannelType channelType, IChannel channel);
1919
}
2020

21-
public class RabbitMqConnectionManager(Uri uri, string connectionName, TimeSpan connectionTimeout, TimeSpan requestedHeart)
21+
public class RabbitMqConnectionManager(
22+
Uri uri,
23+
string connectionName,
24+
TimeSpan connectionTimeout,
25+
TimeSpan requestedHeart,
26+
ushort maxChannelPerConnection = 90
27+
)
2228
: IRabbitMqConnectionManager
2329
{
24-
private readonly IRabbitMqChannelManager _publishChannelManager = new RabbitMqChannelManager(uri, connectionName, connectionTimeout, ChannelType.Publish, requestedHeart);
25-
private readonly IRabbitMqChannelManager _consumerChannelManager = new RabbitMqChannelManager(uri, connectionName, connectionTimeout, ChannelType.Consumer, requestedHeart);
30+
private readonly IRabbitMqChannelManager _publishChannelManager = new RabbitMqChannelManager(uri, connectionName, connectionTimeout, ChannelType.Publish, requestedHeart, maxChannelPerConnection);
31+
private readonly IRabbitMqChannelManager _consumerChannelManager = new RabbitMqChannelManager(uri, connectionName, connectionTimeout, ChannelType.Consumer, requestedHeart, maxChannelPerConnection);
2632

2733
public RabbitMqConnectionManager(Uri uri, string connectionName, TimeSpan connectionTimeout)
28-
: this(uri, connectionName, connectionTimeout, System.Threading.Timeout.InfiniteTimeSpan)
34+
: this(uri, connectionName, connectionTimeout, System.Threading.Timeout.InfiniteTimeSpan, 90)
2935
{
3036
}
3137

@@ -62,4 +68,4 @@ public void ReleaseChannel(ChannelType channelType, IChannel channel)
6268
throw new ArgumentOutOfRangeException(nameof(channelType), channelType, null);
6369
}
6470
}
65-
}
71+
}

0 commit comments

Comments
 (0)