25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

93 satır
2.5 KiB

  1. // Copyright (c) .NET Core Community. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Concurrent;
  5. using System.Diagnostics;
  6. using System.Threading;
  7. using Confluent.Kafka;
  8. using Microsoft.Extensions.Logging;
  9. using Newtonsoft.Json;
  10. namespace DotNetCore.CAP.Kafka
  11. {
  12. public class ConnectionPool : IConnectionPool, IDisposable
  13. {
  14. private readonly ILogger<ConnectionPool> _logger;
  15. private readonly Func<Producer> _activator;
  16. private readonly ConcurrentQueue<Producer> _pool;
  17. private int _count;
  18. private int _maxSize;
  19. public ConnectionPool(ILogger<ConnectionPool> logger, KafkaOptions options)
  20. {
  21. _logger = logger;
  22. _pool = new ConcurrentQueue<Producer>();
  23. _maxSize = options.ConnectionPoolSize;
  24. _activator = CreateActivator(options);
  25. ServersAddress = options.Servers;
  26. _logger.LogDebug("Kafka configuration of CAP :\r\n {0}",
  27. JsonConvert.SerializeObject(options.AsKafkaConfig(), Formatting.Indented));
  28. }
  29. public string ServersAddress { get; }
  30. Producer IConnectionPool.Rent()
  31. {
  32. return Rent();
  33. }
  34. bool IConnectionPool.Return(Producer connection)
  35. {
  36. return Return(connection);
  37. }
  38. public void Dispose()
  39. {
  40. _maxSize = 0;
  41. while (_pool.TryDequeue(out var context))
  42. {
  43. context.Dispose();
  44. }
  45. }
  46. private static Func<Producer> CreateActivator(KafkaOptions options)
  47. {
  48. return () => new Producer(options.AsKafkaConfig());
  49. }
  50. public virtual Producer Rent()
  51. {
  52. if (_pool.TryDequeue(out var connection))
  53. {
  54. Interlocked.Decrement(ref _count);
  55. Debug.Assert(_count >= 0);
  56. return connection;
  57. }
  58. connection = _activator();
  59. return connection;
  60. }
  61. public virtual bool Return(Producer connection)
  62. {
  63. if (Interlocked.Increment(ref _count) <= _maxSize)
  64. {
  65. _pool.Enqueue(connection);
  66. return true;
  67. }
  68. Interlocked.Decrement(ref _count);
  69. Debug.Assert(_maxSize == 0 || _pool.Count <= _maxSize);
  70. return false;
  71. }
  72. }
  73. }