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

Add channel idle strategy. #61

Merged
merged 1 commit into from
May 25, 2021
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
14 changes: 14 additions & 0 deletions src/main/java/com/github/tonivade/resp/RespConnectionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
*/
package com.github.tonivade.resp;

import io.netty.channel.Channel;
import io.netty.handler.timeout.IdleStateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -46,6 +48,18 @@ public void channelInactive(ChannelHandlerContext ctx) throws Exception {
ctx.close();
}

@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
if (evt instanceof IdleStateEvent) {
Channel channel = ctx.channel();
LOGGER.info("IdleStateEvent triggered, close channel " + channel);
impl.disconnected(ctx);
ctx.close();
} else {
super.userEventTriggered(ctx, evt);
}
}

@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
LOGGER.debug("uncaught exception", cause);
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/github/tonivade/resp/RespServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static com.github.tonivade.resp.protocol.SafeString.safeString;
import static java.util.stream.Collectors.toList;

import io.netty.handler.timeout.IdleStateHandler;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.stream.Stream;
Expand Down Expand Up @@ -114,6 +115,7 @@ public void channel(SocketChannel channel) {

channel.pipeline().addLast("redisEncoder", new RedisEncoder());
channel.pipeline().addLast("linDelimiter", new RedisDecoder(MAX_FRAME_SIZE));
channel.pipeline().addLast(new IdleStateHandler(0, 0, 5 * 60));
channel.pipeline().addLast(new RespConnectionHandler(this));
}

Expand Down