Skip to content

Commit dde7a37

Browse files
garyrussellartembilan
authored andcommitted
GH-1026: Fix Delay with CacheMode.CONNECTION
Fixes #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**
1 parent b4103a1 commit dde7a37

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ else if (this.cacheMode == CacheMode.CONNECTION) {
719719
private Connection connectionFromCache() {
720720
ChannelCachingConnectionProxy cachedConnection = findIdleConnection();
721721
long now = System.currentTimeMillis();
722-
if (cachedConnection == null) {
722+
if (cachedConnection == null && countOpenConnections() >= this.connectionLimit) {
723723
cachedConnection = waitForConnection(now);
724724
}
725725
if (cachedConnection == null) {

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/connection/CachingConnectionFactoryTests.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,4 +1791,23 @@ public void testOrderlyShutDown() throws Exception {
17911791
closeExec.shutdownNow();
17921792
}
17931793

1794+
@Test
1795+
public void testFirstConnectionDoesntWait() throws IOException, TimeoutException {
1796+
com.rabbitmq.client.ConnectionFactory mockConnectionFactory = mock(com.rabbitmq.client.ConnectionFactory.class);
1797+
com.rabbitmq.client.Connection mockConnection = mock(com.rabbitmq.client.Connection.class);
1798+
Channel mockChannel = mock(Channel.class);
1799+
1800+
given(mockConnectionFactory.newConnection((ExecutorService) isNull(), anyString())).willReturn(mockConnection);
1801+
given(mockConnection.createChannel()).willReturn(mockChannel);
1802+
given(mockChannel.isOpen()).willReturn(true);
1803+
given(mockConnection.isOpen()).willReturn(true);
1804+
1805+
CachingConnectionFactory ccf = new CachingConnectionFactory(mockConnectionFactory);
1806+
ccf.setCacheMode(CacheMode.CONNECTION);
1807+
ccf.setChannelCheckoutTimeout(60000);
1808+
long t1 = System.currentTimeMillis();
1809+
ccf.createConnection();
1810+
assertThat(System.currentTimeMillis() - t1).isLessThan(30_000);
1811+
}
1812+
17941813
}

0 commit comments

Comments
 (0)