From 4b7fa159846e95de68170594a39f03b790a943cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=B9=E6=9D=A1?= Date: Wed, 4 Aug 2021 12:24:40 +0800 Subject: [PATCH] Move the release of the object inside the Return method (#969) --- src/DotNetCore.CAP.Kafka/IConnectionPool.Default.cs | 2 ++ src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs | 6 +----- src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs | 6 ++++-- src/DotNetCore.CAP.NATS/ITransport.NATS.cs | 10 +++------- .../IConnectionChannelPool.Default.cs | 4 +++- src/DotNetCore.CAP.RabbitMQ/ITransport.RabbitMQ.cs | 6 +----- 6 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/DotNetCore.CAP.Kafka/IConnectionPool.Default.cs b/src/DotNetCore.CAP.Kafka/IConnectionPool.Default.cs index 9f1c023..f4ba628 100644 --- a/src/DotNetCore.CAP.Kafka/IConnectionPool.Default.cs +++ b/src/DotNetCore.CAP.Kafka/IConnectionPool.Default.cs @@ -60,6 +60,8 @@ namespace DotNetCore.CAP.Kafka return true; } + producer.Dispose(); + Interlocked.Decrement(ref _pCount); return false; diff --git a/src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs b/src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs index 28e59f9..5484193 100644 --- a/src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs +++ b/src/DotNetCore.CAP.Kafka/ITransport.Kafka.cs @@ -64,11 +64,7 @@ namespace DotNetCore.CAP.Kafka } finally { - var returned = _connectionPool.Return(producer); - if (!returned) - { - producer.Dispose(); - } + _connectionPool.Return(producer); } } } diff --git a/src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs b/src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs index ad37e61..cc88e8a 100644 --- a/src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs +++ b/src/DotNetCore.CAP.NATS/IConnectionPool.Default.cs @@ -17,7 +17,7 @@ namespace DotNetCore.CAP.NATS private readonly ConnectionFactory _connectionFactory; private int _pCount; private int _maxSize; - + public ConnectionPool(ILogger logger, IOptions options) { _options = options.Value; @@ -51,7 +51,7 @@ namespace DotNetCore.CAP.NATS { connection = _connectionFactory.CreateConnection(_options.Servers); } - + return connection; } @@ -64,6 +64,8 @@ namespace DotNetCore.CAP.NATS return true; } + connection.Dispose(); + Interlocked.Decrement(ref _pCount); return false; diff --git a/src/DotNetCore.CAP.NATS/ITransport.NATS.cs b/src/DotNetCore.CAP.NATS/ITransport.NATS.cs index 0fe2057..35a63fc 100644 --- a/src/DotNetCore.CAP.NATS/ITransport.NATS.cs +++ b/src/DotNetCore.CAP.NATS/ITransport.NATS.cs @@ -35,8 +35,8 @@ namespace DotNetCore.CAP.NATS { msg.Header[header.Key] = header.Value; } - - var reply= connection.Request(msg); + + var reply = connection.Request(msg); if (reply.Data != null && reply.Data[0] == 1) { @@ -54,11 +54,7 @@ namespace DotNetCore.CAP.NATS } finally { - var returned = _connectionPool.Return(connection); - if (!returned) - { - connection.Dispose(); - } + _connectionPool.Return(connection); } } } diff --git a/src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs b/src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs index f3f9474..1889553 100644 --- a/src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs +++ b/src/DotNetCore.CAP.RabbitMQ/IConnectionChannelPool.Default.cs @@ -105,7 +105,7 @@ namespace DotNetCore.CAP.RabbitMQ if (options.HostName.Contains(",")) { options.ConnectionFactoryOptions?.Invoke(factory); - + return () => factory.CreateConnection( options.HostName.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)); } @@ -149,6 +149,8 @@ namespace DotNetCore.CAP.RabbitMQ return true; } + connection.Dispose(); + Interlocked.Decrement(ref _count); Debug.Assert(_maxSize == 0 || _pool.Count <= _maxSize); diff --git a/src/DotNetCore.CAP.RabbitMQ/ITransport.RabbitMQ.cs b/src/DotNetCore.CAP.RabbitMQ/ITransport.RabbitMQ.cs index 1c8be1b..61306ec 100644 --- a/src/DotNetCore.CAP.RabbitMQ/ITransport.RabbitMQ.cs +++ b/src/DotNetCore.CAP.RabbitMQ/ITransport.RabbitMQ.cs @@ -67,11 +67,7 @@ namespace DotNetCore.CAP.RabbitMQ { if (channel != null) { - var returned = _connectionChannelPool.Return(channel); - if (!returned) - { - channel.Dispose(); - } + _connectionChannelPool.Return(channel); } } }