-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathKafkaConnectionPool.cs
79 lines (71 loc) · 2.22 KB
/
KafkaConnectionPool.cs
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
using Confluent.Kafka;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Concurrent;
using System.Threading;
namespace Blog.Core.EventBus
{
/// <summary>
/// Kafka producer 连接池管理
/// 可以使用微软官方的对象池进行构造ObjectPool
/// </summary>
public class KafkaConnectionPool : IKafkaConnectionPool
{
private readonly KafkaOptions _options;
private ConcurrentQueue<IProducer<string, byte[]>> _producerPool = new();
private int _currentCount;
private int _maxSize;
public KafkaConnectionPool(IOptions<KafkaOptions> options)
{
_options = options.Value;
_maxSize = _options.ConnectionPoolSize;
}
/// <summary>
/// 取对象
/// </summary>
/// <returns></returns>
public IProducer<string,byte[]> Producer()
{
if (_producerPool.TryDequeue(out var producer))
{
Interlocked.Decrement(ref _currentCount);
return producer;
}
var config = new ProducerConfig()
{
BootstrapServers = _options.Servers,
QueueBufferingMaxMessages = 10,
MessageTimeoutMs = 5000,
RequestTimeoutMs = 3000
};
producer = new ProducerBuilder<string, byte[]>(config)
.Build();
return producer;
}
/// <summary>
/// 将对象放入连接池
/// </summary>
/// <param name="producer"></param>
/// <returns></returns>
public bool Return(IProducer<string, byte[]> producer)
{
if (Interlocked.Increment(ref _currentCount) <= _maxSize)
{
_producerPool.Enqueue(producer);
return true;
}
producer.Dispose();
Interlocked.Decrement(ref _currentCount);
return false;
}
public void Dispose()
{
_maxSize = 0;
_currentCount = 0;
while (_producerPool.TryDequeue(out var context))
{
context?.Dispose();
}
}
}
}