Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub#486 - Fix connection pool leaks #504

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public Publisher<Connection> borrowConnection() {
return Mono.<Connection>create(sink -> {
Connection connection = dequeue();
if (connection != null) {
borrowedCount.incrementAndGet();
sink.success(connection);
attemptBorrowConnection(sink, connection);
} else {
if (waitingSubscribers.size() < poolSettings.maxPendingConnectionsPerHost()) {
this.waitingSubscribers.add(sink.onDispose(() -> waitingSubscribers.remove(sink)));
this.waitingSubscribers.add(sink);
sink.onDispose(() -> waitingSubscribers.remove(sink));
newConnection();
} else {
sink.error(new MaxPendingConnectionsExceededException(
Expand Down Expand Up @@ -127,7 +127,8 @@ private Mono<Connection> newConnection(int attempts) {
}
}

private Connection dequeue() {
@VisibleForTesting
Connection dequeue() {
Connection connection = availableConnections.poll();

while (nonNull(connection) && !connection.isConnected()) {
Expand All @@ -142,11 +143,18 @@ private void queueNewConnection(Connection connection) {
if (subscriber == null) {
availableConnections.add(connection);
} else {
borrowedCount.incrementAndGet();
subscriber.success(connection);
attemptBorrowConnection(subscriber, connection);
}
}

private void attemptBorrowConnection(MonoSink<Connection> sink, Connection connection) {
borrowedCount.incrementAndGet();
sink.onCancel(() -> {
returnConnection(connection);
});
sink.success(connection);
}

public boolean returnConnection(Connection connection) {
borrowedCount.decrementAndGet();
if (connection.isConnected()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.hotels.styx.client.Connection;
import com.hotels.styx.client.ConnectionSettings;
import org.mockito.Mockito;
import org.mockito.internal.stubbing.answers.AnswersWithDelay;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.reactivestreams.Publisher;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -39,6 +42,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
Expand Down Expand Up @@ -718,6 +722,88 @@ public void emitsExceptionWhenPendingConnectionTimesOut() {
assertEquals(pool.stats().connectionAttempts(), 1);
}

@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenCreatingNewConnection() throws Exception {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class)))
.thenReturn(Mono.just(connection1));

ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder()
.pendingConnectionTimeout(100, MILLISECONDS)
.build();

SimpleConnectionPool simpleConnectionPool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
SimpleConnectionPool pool = spy(simpleConnectionPool);
when(pool.dequeue())
.thenAnswer(new AnswersWithDelay(200, new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.callRealMethod();
}
}));
StepVerifier.create(pool.borrowConnection())
.expectError(MaxPendingConnectionTimeoutException.class)
.verify();

assertEquals(pool.stats().availableConnectionCount(), 1);
assertEquals(pool.stats().pendingConnectionCount(), 0); // Waiting subscribers
assertEquals(pool.stats().busyConnectionCount(), 0); // Borrowed count
}

@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionIsReturned() throws Exception {
EmitterProcessor<Connection> processor = EmitterProcessor.create();
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class)))
.thenReturn(Mono.from(processor));
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder()
.pendingConnectionTimeout(100, MILLISECONDS)
.build();

SimpleConnectionPool simpleConnectionPool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
SimpleConnectionPool pool = spy(simpleConnectionPool);
when(pool.dequeue())
.thenAnswer(new AnswersWithDelay(200, new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return invocation.callRealMethod();
}
}));

StepVerifier.create(pool.borrowConnection())
.expectError(MaxPendingConnectionTimeoutException.class)
.verify();

pool.returnConnection(connection1);

assertEquals(pool.stats().availableConnectionCount(), 1);
assertEquals(pool.stats().pendingConnectionCount(), 0); // Waiting subscribers
assertEquals(pool.stats().busyConnectionCount(), -1); // Borrowed count
}

@Test
public void shouldNotHandoutConnectionToCancelledSubscriberWhenConnectionAlreadyInPool() throws Exception {
ConnectionPoolSettings poolSettings = new ConnectionPoolSettings.Builder()
.pendingConnectionTimeout(100, MILLISECONDS)
.build();

SimpleConnectionPool simpleConnectionPool = new SimpleConnectionPool(origin, poolSettings, connectionFactory);
SimpleConnectionPool pool = spy(simpleConnectionPool);
when(pool.dequeue())
.thenAnswer(new AnswersWithDelay(200, new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
return connection1;
}
}));

StepVerifier.create(pool.borrowConnection())
.expectError(MaxPendingConnectionTimeoutException.class)
.verify();

assertEquals(pool.stats().availableConnectionCount(), 1);
assertEquals(pool.stats().pendingConnectionCount(), 0); // Waiting subscribers
assertEquals(pool.stats().busyConnectionCount(), 0); // Borrowed count
}

@Test
public void registersAsConnectionListener() {
when(connectionFactory.createConnection(any(Origin.class), any(ConnectionSettings.class)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@
<netty-tcnative.version>2.0.26.Final</netty-tcnative.version>
<rxjava.version>1.1.6</rxjava.version>
<reactive-streams.version>1.0.2</reactive-streams.version>
<reactor.version>3.2.0.RELEASE</reactor.version>
<reactor.version>3.3.0.RELEASE</reactor.version>
<pcollections.version>3.0.3</pcollections.version>

<!--Kotlin -->
Expand Down