From dde7a37350e21a5bbcce26122fbc49b3dd72238b Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Mon, 17 Jun 2019 10:19:12 -0400 Subject: [PATCH] GH-1026: Fix Delay with CacheMode.CONNECTION Fixes https://github.com/spring-projects/spring-amqp/issues/1026 When using a `channelCheckoutTimeout` with `CacheModeConnection`, we incorrectly spin waiting for a connection until the timeout expires. We should only wait for a connection if the limit is exceeded. **cherry-pick to all supported** --- .../connection/CachingConnectionFactory.java | 2 +- .../CachingConnectionFactoryTests.java | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java index 864d232e..31b1bf5c 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java @@ -719,7 +719,7 @@ public class CachingConnectionFactory extends AbstractConnectionFactory private Connection connectionFromCache() { ChannelCachingConnectionProxy cachedConnection = findIdleConnection(); long now = System.currentTimeMillis(); - if (cachedConnection == null) { + if (cachedConnection == null && countOpenConnections() >= this.connectionLimit) { cachedConnection = waitForConnection(now); } if (cachedConnection == null) { diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java index 9f71d727..b4c80a4d 100644 --- a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java @@ -1791,4 +1791,23 @@ public class CachingConnectionFactoryTests extends AbstractConnectionFactoryTest closeExec.shutdownNow(); } + @Test + public void testFirstConnectionDoesntWait() throws IOException, TimeoutException { + com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class); + com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class); + Channel mockChannel = mock(Channel.class); + + given(mockConnectionFactory.newConnection((ExecutorService) isNull(), anyString())).willReturn(mockConnection); + given(mockConnection.createChannel()).willReturn(mockChannel); + given(mockChannel.isOpen()).willReturn(true); + given(mockConnection.isOpen()).willReturn(true); + + CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory); + ccf.setCacheMode(CacheMode.CONNECTION); + ccf.setChannelCheckoutTimeout(60000); + long t1 = System.currentTimeMillis(); + ccf.createConnection(); + assertThat(System.currentTimeMillis() - t1).isLessThan(30_000); + } + }