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**
This commit is contained in:
committed by
Artem Bilan
parent
b4103a1354
commit
dde7a37350
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user