Skip to content

Commit 335a0f3

Browse files
authored
Remove code duplication to handle the pool (#356)
Signed-off-by: Gabriele Santomaggio <[email protected]>
1 parent 66cbd13 commit 335a0f3

File tree

4 files changed

+13
-14
lines changed

4 files changed

+13
-14
lines changed

RabbitMQ.Stream.Client/ConnectionsPool.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,13 @@ internal async Task<IClient> GetOrCreateClient(string brokerInfo, Func<Task<ICli
145145
// do we have a connection for this brokerInfo and with free slots for producer or consumer?
146146
// it does not matter which connection is available
147147
// the important is to have a connection available for the brokerInfo
148-
var count = Connections.Values.Count(x => x.BrokerInfo == brokerInfo && x.Available);
148+
var connectionItems = Connections.Values.Where(x => x.BrokerInfo == brokerInfo && x.Available).ToList();
149149

150-
if (count > 0)
150+
if (connectionItems.Any())
151151
{
152152
// ok we have a connection available for this brokerInfo
153153
// let's get the first one
154-
// TODO: we can improve this by getting the connection with the less active items
155-
var connectionItem = Connections.Values.First(x => x.BrokerInfo == brokerInfo && x.Available);
154+
var connectionItem = connectionItems.OrderBy(x => x.EntitiesCount).First();
156155
connectionItem.LastUsed = DateTime.UtcNow;
157156

158157
if (connectionItem.Client is not { IsClosed: true })
@@ -162,11 +161,11 @@ internal async Task<IClient> GetOrCreateClient(string brokerInfo, Func<Task<ICli
162161
// let's remove it from the pool
163162
Connections.TryRemove(connectionItem.Client.ClientId, out _);
164163
// let's create a new one
165-
connectionItem = new ConnectionItem(brokerInfo, _idsPerConnection,
164+
var newConnectionItem = new ConnectionItem(brokerInfo, _idsPerConnection,
166165
await createClient().ConfigureAwait(false));
167166
Connections.TryAdd(connectionItem.Client.ClientId, connectionItem);
168167

169-
return connectionItem.Client;
168+
return newConnectionItem.Client;
170169
}
171170

172171
if (_maxConnections > 0 && Connections.Count >= _maxConnections)

RabbitMQ.Stream.Client/RawSuperStreamProducer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,16 +311,16 @@ public Task<ResponseCode> Close()
311311
//<summary>
312312
/// Returns lower from the LastPublishingId for all the producers
313313
// </summary>
314-
public Task<ulong> GetLastPublishingId()
314+
public async Task<ulong> GetLastPublishingId()
315315
{
316316
foreach (var stream in _streamInfos.Keys.ToList())
317317
{
318-
MaybeAddAndGetProducer(stream).Wait();
318+
await MaybeAddAndGetProducer(stream).ConfigureAwait(false);
319319
}
320320

321321
var v = _producers.Values.Min(p => p.GetLastPublishingId().Result);
322322

323-
return Task.FromResult(v);
323+
return v;
324324
}
325325

326326
public bool IsOpen()

docs/ReliableClient/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
Host = "node0",
1313
Port = 5562,
1414
LoadBalancer = false,
15-
SuperStream = false,
15+
SuperStream = true,
1616
Streams = 3,
17-
Producers = 10,
17+
Producers = 2,
1818
MessagesPerProducer = 10_000_000,
19-
Consumers = 10,
19+
Consumers = 5,
2020
// Username = "test",
2121
// Password = "test"
2222
});

docs/ReliableClient/RClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static async Task Start(Config config)
4343
options.TimestampFormat = "[HH:mm:ss] ";
4444
options.ColorBehavior = LoggerColorBehavior.Default;
4545
})
46-
.AddFilter(level => level >= LogLevel.Information)
46+
.AddFilter(level => level >= LogLevel.Debug)
4747
);
4848
var loggerFactory = serviceCollection.BuildServiceProvider()
4949
.GetService<ILoggerFactory>();
@@ -261,7 +261,7 @@ async Task MaybeSend(Producer producer, Message message, ManualResetEvent publis
261261
Properties = new Properties() {MessageId = $"hello{i}"}
262262
};
263263
await MaybeSend(producer, message, publishEvent).ConfigureAwait(false);
264-
await Task.Delay(20).ConfigureAwait(false);
264+
await Task.Delay(320).ConfigureAwait(false);
265265
Interlocked.Increment(ref totalSent);
266266
}
267267
});

0 commit comments

Comments
 (0)