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

Fix connection pool error. #437

Merged
merged 2 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,26 +76,17 @@ public Publisher<Connection> borrowConnection() {
return Mono.<Connection>create(sink -> {
Connection connection = dequeue();
if (connection != null) {
if (borrowedCount.getAndIncrement() < poolSettings.maxConnectionsPerHost()) {
sink.success(connection);
} else {
borrowedCount.decrementAndGet();
queueNewConnection(connection);
}
borrowedCount.incrementAndGet();
sink.success(connection);
} else {
if (waitingSubscribers.size() >= poolSettings.maxPendingConnectionsPerHost()) {
if (waitingSubscribers.size() < poolSettings.maxPendingConnectionsPerHost()) {
this.waitingSubscribers.add(sink.onDispose(() -> waitingSubscribers.remove(sink)));
dvlato marked this conversation as resolved.
Show resolved Hide resolved
newConnection();
} else {
sink.error(new MaxPendingConnectionsExceededException(
origin,
poolSettings.maxPendingConnectionsPerHost(),
poolSettings.maxPendingConnectionsPerHost()));
} else {
this.waitingSubscribers.add(sink.onCancel(() -> waitingSubscribers.remove(sink)));
int borrowed = borrowedCount.get();
int inEstablishment = connectionsInEstablishment.getAndIncrement();

if ((borrowed + inEstablishment) < poolSettings.maxConnectionsPerHost()) {
newConnection();
}
}
}
}).timeout(
Expand All @@ -104,6 +95,14 @@ public Publisher<Connection> borrowConnection() {
}

private void newConnection() {
int borrowed = borrowedCount.get();
int inEstablishment = connectionsInEstablishment.getAndIncrement();
dvlato marked this conversation as resolved.
Show resolved Hide resolved

if ((borrowed + inEstablishment) >= poolSettings.maxConnectionsPerHost()) {
connectionsInEstablishment.decrementAndGet();
return;
}

connectionAttempts.incrementAndGet();
newConnection(MAX_ATTEMPTS)
.doOnNext(it -> it.addConnectionListener(SimpleConnectionPool.this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private static void registerMetrics(ConnectionPool hostConnectionPool, MetricReg
scopedRegistry.register("connection-failures", (Gauge<Integer>) () -> (int) stats.connectionFailures());
scopedRegistry.register("connections-closed", (Gauge<Integer>) () -> (int) stats.closedConnections());
scopedRegistry.register("connections-terminated", (Gauge<Integer>) () -> (int) stats.terminatedConnections());
scopedRegistry.register("in-establishment", (Gauge<Integer>) () -> (int) stats.connectionsInEstablishment());
scopedRegistry.register("connections-in-establishment", (Gauge<Integer>) () -> (int) stats.connectionsInEstablishment());
}

private void registerMetrics() {
Expand All @@ -107,7 +107,7 @@ private void removeMetrics() {
MetricRegistry scopedRegistry = getMetricScope(connectionPool);
asList("busy-connections", "pending-connections", "available-connections", "ttfb",
"connection-attempts", "connection-failures", "connections-closed", "connections-terminated",
"in-establishment")
"connections-in-establishment")
.forEach(scopedRegistry::deregister);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void registersMetricsUnderOriginsScope() {
"origins.test-app.origin-X.connectionspool.pending-connections",
"origins.test-app.origin-X.connectionspool.available-connections",
"origins.test-app.origin-X.connectionspool.busy-connections",
"origins.test-app.origin-X.connectionspool.in-establishment"
"origins.test-app.origin-X.connectionspool.connections-in-establishment"
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void removesRegisteredMetricsOnClose() {
"origins.generic-app.backend-01.connectionspool.busy-connections",
"origins.generic-app.backend-01.connectionspool.pending-connections",
"origins.generic-app.backend-01.connectionspool.connection-attempts",
"origins.generic-app.backend-01.connectionspool.in-establishment",
"origins.generic-app.backend-01.connectionspool.connections-in-establishment",
"origins.generic-app.backend-01.connectionspool.connection-failures",
"origins.generic-app.backend-01.connectionspool.connections-closed",
"origins.generic-app.backend-01.connectionspool.connections-terminated"
Expand Down