Skip to content

Commit

Permalink
Adjusted resource cleanup to prevent memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
mp911de committed Jun 21, 2014
1 parent b07a2f0 commit 807f391
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ protected void initChannel(Channel ch) throws Exception {

redisBootstrap.connect(redisAddress).get();

connection.registerCloseables(closeableResources, connection);
connection.registerCloseables(closeableResources, connection, handler);

return connection;
} catch (Exception e) {
connection.close();
throw new RedisException("Unable to connect", e);
}
}
Expand All @@ -119,14 +120,14 @@ protected void initChannel(Channel ch) throws Exception {
*/
public void shutdown() {

ImmutableList<Closeable> autoCloseables = ImmutableList.copyOf(closeableResources);
for (Closeable closeableResource : autoCloseables) {
while (!closeableResources.isEmpty()) {
Closeable closeableResource = closeableResources.iterator().next();
try {
closeableResource.close();
} catch (Exception e) {
logger.debug("Exception on Close: " + e.getMessage(), e);

}
closeableResources.remove(closeableResource);
}

for (Channel c : channels) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.lambdaworks.redis;

import java.io.Closeable;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
Expand All @@ -20,7 +21,7 @@
* @author <a href="mailto:[email protected]">Mark Paluch</a>
* @since 15.05.14 16:09
*/
public abstract class RedisChannelHandler<K, V> extends ChannelInboundHandlerAdapter {
public abstract class RedisChannelHandler<K, V> extends ChannelInboundHandlerAdapter implements Closeable {

private static final InternalLogger logger = InternalLoggerFactory.getInstance(RedisChannelHandler.class);

Expand Down Expand Up @@ -65,7 +66,6 @@ public synchronized void close() {
if (!closed) {
active = false;
closed = true;
channelWriter.close();
closeEvents.fireEventClosed(this);
closeEvents = null;
}
Expand All @@ -92,8 +92,19 @@ public void registerCloseables(final Collection<Closeable> registry, final Close
addListener(new CloseEvents.CloseListener() {
@Override
public void resourceClosed(Object resource) {
registry.removeAll(Arrays.asList(closeables));
for (Closeable closeable : closeables) {
if (closeable == RedisChannelHandler.this) {
continue;
}

try {
closeable.close();
} catch (IOException e) {
logger.debug(e.toString(), e);
}
}

registry.removeAll(Arrays.asList(closeables));
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ public void close() {
logger.debug("close()");

if (closed) {
logger.warn("Client is already closed");
return;
}

Expand All @@ -276,22 +275,22 @@ public void close() {
buffer = null;
}

if (!closed && channel != null) {
closed = true;

if (channel != null) {
ConnectionWatchdog watchdog = channel.pipeline().get(ConnectionWatchdog.class);
if (watchdog != null) {
watchdog.setReconnect(false);
}
closed = true;

try {
channel.close().sync();
} catch (InterruptedException e) {
throw new RedisException(e);
}

channel = null;

}

}

public boolean isClosed() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,18 @@ public void testResourceCleaning() throws Exception {
pool1.allocateConnection();

assertEquals(1, redisClient.getChannelCount());
assertEquals(2, redisClient.getResourceCount());
assertEquals(3, redisClient.getResourceCount());

RedisConnectionPool<RedisConnection<String, String>> pool2 = redisClient.pool();

assertEquals(3, redisClient.getResourceCount());
assertEquals(4, redisClient.getResourceCount());

pool2.allocateConnection();

assertEquals(4, redisClient.getResourceCount());
assertEquals(6, redisClient.getResourceCount());

redisClient.pool().close();
assertEquals(4, redisClient.getResourceCount());
assertEquals(6, redisClient.getResourceCount());

redisClient.shutdown();

Expand Down

0 comments on commit 807f391

Please sign in to comment.