-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathChannelProvider.cs
More file actions
123 lines (106 loc) · 3.81 KB
/
ChannelProvider.cs
File metadata and controls
123 lines (106 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using System;
using System.Collections.Concurrent;
using System.Threading;
using Unity.Modules.Shared.MessageBrokers.RabbitMQ.Interfaces;
namespace Unity.Modules.Shared.MessageBrokers.RabbitMQ
{
public sealed class ChannelProvider : IChannelProvider, IDisposable
{
private readonly IConnectionProvider _connectionProvider;
private readonly ILogger<ChannelProvider> _logger;
private readonly int _maxChannels;
private readonly ConcurrentQueue<IModel> _channelPool = new();
private int _currentChannelCount;
private bool _disposed;
public ChannelProvider(IConnectionProvider connectionProvider, ILogger<ChannelProvider> logger, int maxChannels = 10000)
{
_connectionProvider = connectionProvider ?? throw new ArgumentNullException(nameof(connectionProvider));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_maxChannels = maxChannels;
}
public IModel? GetChannel()
{
ThrowIfDisposed();
// Try to reuse a channel from the pool
while (_channelPool.TryDequeue(out var channel))
{
if (channel.IsOpen)
return channel;
DisposeChannel(channel);
}
// Try to create a new channel if we haven't reached max
if (Interlocked.Increment(ref _currentChannelCount) <= _maxChannels)
{
try
{
var connection = _connectionProvider.GetConnection();
if (connection != null && connection.IsOpen)
return connection.CreateModel();
_logger.LogWarning("RabbitMQ connection is not open.");
Interlocked.Decrement(ref _currentChannelCount); // failed to create
}
catch (Exception ex)
{
_logger.LogError(ex, "Error creating RabbitMQ channel.");
Interlocked.Decrement(ref _currentChannelCount); // failed to create
}
}
else
{
Interlocked.Decrement(ref _currentChannelCount); // revert increment since max reached
_logger.LogWarning("Max channel count reached ({MaxChannels}). Cannot create new channel.", _maxChannels);
}
return null;
}
public void ReturnChannel(IModel channel)
{
if (_disposed)
{
DisposeChannel(channel);
return;
}
if (channel.IsOpen)
_channelPool.Enqueue(channel);
else
DisposeChannel(channel);
}
private void DisposeChannel(IModel channel)
{
try
{
if (channel.IsOpen)
channel.Close();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Exception while closing RabbitMQ channel.");
}
try
{
channel.Dispose();
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Exception while disposing RabbitMQ channel.");
}
Interlocked.Decrement(ref _currentChannelCount);
}
private void ThrowIfDisposed()
{
if (_disposed)
throw new ObjectDisposedException(nameof(ChannelProvider));
}
public void Dispose()
{
if (_disposed)
return;
_disposed = true;
while (_channelPool.TryDequeue(out var channel))
{
DisposeChannel(channel);
}
}
}
}