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 pool metrics for tainted connections #489

Merged
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 @@ -10,6 +10,7 @@
import io.vertx.redis.client.Response;

import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

/**
* A pooled Redis connection
Expand All @@ -20,6 +21,7 @@ public class PooledRedisConnection implements RedisConnection {
private final RedisConnectionInternal connection;
private final PoolMetrics metrics;
private final Object metric;
private final AtomicBoolean ended = new AtomicBoolean();

public PooledRedisConnection(Lease<RedisConnectionInternal> lease, PoolMetrics<?, ?> poolMetrics, Object metric) {
this.lease = lease;
Expand Down Expand Up @@ -88,6 +90,9 @@ public RedisConnection endHandler(@Nullable Handler<Void> endHandler) {
public Future<Void> close() {
if (connection.reset()) {
lease.recycle();
}

if (ended.compareAndSet(false, true)) {
if (metrics != null) {
metrics.end(metric);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,33 @@ public void simpleTest(TestContext should) {
});
}

@Test
public void taintedConnection(TestContext test) {
Async async = test.async();

Redis client = Redis.createClient(rule.vertx(), new RedisOptions().setConnectionString(redis.getRedisUri()));
client.connect()
.compose(conn -> {
test.assertEquals(0, getMetrics().pending());
test.assertEquals(1, getMetrics().inUse());

return conn.send(Request.cmd(Command.SELECT).arg(7)) // taints the connection
.compose(response -> {
test.assertEquals(0, getMetrics().pending());
test.assertEquals(1, getMetrics().inUse());

return conn.close();
}).onComplete(test.asyncAssertSuccess(ignored -> {
test.assertEquals(0, getMetrics().pending());
test.assertEquals(0, getMetrics().inUse());
}));
})
.compose(ignored -> client.close())
.onComplete(test.asyncAssertSuccess(ignored -> {
async.complete();
}));
}

@Test
public void testLifecycle(TestContext should) {
final Async test = should.async();
Expand Down
Loading