Skip to content
Permalink
Browse files

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**
  • Loading branch information
garyrussell authored and artembilan committed Jun 17, 2019
1 parent b4103a1 commit dde7a37350e21a5bbcce26122fbc49b3dd72238b
@@ -719,7 +719,7 @@ else if (this.cacheMode == CacheMode.CONNECTION) {
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 void testOrderlyShutDown() throws Exception {
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);
}

}

0 comments on commit dde7a37

Please sign in to comment.
You can’t perform that action at this time.