Skip to content

Commit

Permalink
Add ConnectionWatchdog as last handler #335
Browse files Browse the repository at this point in the history
The reuse of handlers causes multiple calls to channelActive/channelInactive/and so on. In some cases, the reconnection is faster than channel deregistration (calls to channelUnregistered) which leads to the call sequence channelRegistered (new channel), channelUnregistered (old channel). Similar for channelActive/channelInactive. This is handled inside the CommandHandler but can still lead to issues. Moving the ConnectionWatchdog to the end reduces the probability of the calls coming in the wrong order. It fixes also the channelActive/channelInactive order in fast reconnect scenarios as the channelInactive call is done before notifying the ConnectionWatchdog.
  • Loading branch information
mp911de committed Aug 17, 2016
1 parent 5900bc0 commit ae70102
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/lambdaworks/redis/ConnectionBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ protected List<ChannelHandler> buildHandlers() {
LettuceAssert.assertState(clientResources != null, "clientResources must be set");

List<ChannelHandler> handlers = new ArrayList<>();
if (clientOptions.isAutoReconnect()) {
handlers.add(createConnectionWatchdog());
}

connection.setOptions(clientOptions);

Expand All @@ -65,6 +62,10 @@ protected List<ChannelHandler> buildHandlers() {
handlers.add(connection);
handlers.add(new ConnectionEventTrigger(connectionEvents, connection, clientResources.eventBus()));

if (clientOptions.isAutoReconnect()) {
handlers.add(createConnectionWatchdog());
}

return handlers;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ public SocketAddress get() {
this.reconnectionHandler = new ReconnectionHandler(clientOptions, bootstrap, wrappedSocketAddressSupplier);
}

@Override
public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

if(commandHandler == null) {
this.commandHandler = ctx.pipeline().get(CommandHandler.class);
}
super.channelRegistered(ctx);
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

Expand Down Expand Up @@ -139,6 +130,10 @@ void prepareClose(ConnectionEvents.PrepareClose prepareClose) {
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {

if(commandHandler == null) {
this.commandHandler = ctx.pipeline().get(CommandHandler.class);
}

channel = ctx.channel();
logger.debug("{} channelActive({})", commandHandler.logPrefix(), ctx);
remoteAddress = channel.remoteAddress();
Expand Down

0 comments on commit ae70102

Please sign in to comment.