82
82
public class CommandHandler extends ChannelDuplexHandler implements HasQueuedCommands {
83
83
84
84
/**
85
- * When we encounter an unexpected IOException we look for these {@link Throwable#getMessage() messages} (because we have no
86
- * better way to distinguish) and log them at DEBUG rather than WARN, since they are generally caused by unclean client
87
- * disconnects rather than an actual problem.
85
+ * When we encounter an unexpected {@link IOException} we look for these {@link Throwable#getMessage() messages} (because we
86
+ * have no better way to distinguish) and log them at DEBUG rather than WARN, since they are generally caused by unclean
87
+ * client disconnects rather than an actual problem.
88
88
*/
89
89
static final Set <String > SUPPRESS_IO_EXCEPTION_MESSAGES = LettuceSets .unmodifiableSet ("Connection reset by peer" ,
90
90
"Broken pipe" , "Connection timed out" );
@@ -123,7 +123,7 @@ public class CommandHandler extends ChannelDuplexHandler implements HasQueuedCom
123
123
124
124
private Channel channel ;
125
125
126
- private ByteBuf buffer ;
126
+ private ByteBuf readBuffer ;
127
127
128
128
private boolean hasDecodeProgress ;
129
129
@@ -182,8 +182,8 @@ protected void setState(LifecycleState lifecycleState) {
182
182
}
183
183
}
184
184
185
- void setBuffer (ByteBuf buffer ) {
186
- this .buffer = buffer ;
185
+ void setReadBuffer (ByteBuf readBuffer ) {
186
+ this .readBuffer = readBuffer ;
187
187
}
188
188
189
189
@ Override
@@ -222,7 +222,7 @@ public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
222
222
223
223
setState (LifecycleState .REGISTERED );
224
224
225
- buffer = ctx .alloc ().buffer (8192 * 8 );
225
+ readBuffer = ctx .alloc ().buffer (8192 * 8 );
226
226
rsm = new RedisStateMachine ();
227
227
ctx .fireChannelRegistered ();
228
228
}
@@ -242,10 +242,15 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
242
242
ctx .fireChannelUnregistered ();
243
243
return ;
244
244
}
245
-
246
245
channel = null ;
247
- buffer .release ();
248
- rsm .close ();
246
+
247
+ if (readBuffer != null ) {
248
+ readBuffer .release ();
249
+ }
250
+
251
+ if (rsm != null ) {
252
+ rsm .close ();
253
+ }
249
254
rsm = null ;
250
255
251
256
reset ();
@@ -596,7 +601,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
596
601
}
597
602
598
603
try {
599
- if (buffer .refCnt () < 1 ) {
604
+ if (readBuffer == null || readBuffer .refCnt () < 1 ) {
600
605
logger .warn ("{} Ignoring received data for closed or abandoned connection" , logPrefix ());
601
606
return ;
602
607
}
@@ -610,10 +615,10 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
610
615
logger .trace ("{} Buffer: {}" , logPrefix (), input .toString (Charset .defaultCharset ()).trim ());
611
616
}
612
617
613
- buffer .touch ("CommandHandler.read(…)" );
614
- buffer .writeBytes (input );
618
+ readBuffer .touch ("CommandHandler.read(…)" );
619
+ readBuffer .writeBytes (input );
615
620
616
- decode (ctx , buffer );
621
+ decode (ctx , readBuffer );
617
622
} finally {
618
623
input .release ();
619
624
}
@@ -829,7 +834,7 @@ private boolean decode0(ChannelHandlerContext ctx, ByteBuf buffer, RedisCommand<
829
834
830
835
private boolean decode0 (ChannelHandlerContext ctx , ByteBuf buffer , CommandOutput <?, ?, ?> pushOutput ) {
831
836
832
- if (!rsm .decode (buffer , pushOutput , ctx ::fireExceptionCaught )) {
837
+ if (rsm != null && !rsm .decode (buffer , pushOutput , ctx ::fireExceptionCaught )) {
833
838
return false ;
834
839
}
835
840
@@ -852,11 +857,11 @@ private boolean decode0(ChannelHandlerContext ctx, ByteBuf buffer, CommandOutput
852
857
}
853
858
854
859
protected boolean decode (ByteBuf buffer , CommandOutput <?, ?, ?> output ) {
855
- return rsm .decode (buffer , output );
860
+ return rsm != null && rsm .decode (buffer , output );
856
861
}
857
862
858
863
protected boolean decode (ByteBuf buffer , RedisCommand <?, ?, ?> command , CommandOutput <?, ?, ?> output ) {
859
- return rsm .decode (buffer , output , command ::completeExceptionally );
864
+ return rsm != null && rsm .decode (buffer , output , command ::completeExceptionally );
860
865
}
861
866
862
867
/**
@@ -918,7 +923,7 @@ private void onProtectedMode(String message) {
918
923
* @param command
919
924
*/
920
925
protected void afterDecode (ChannelHandlerContext ctx , RedisCommand <?, ?, ?> command ) {
921
- decodeBufferPolicy .afterCommandDecoded (buffer );
926
+ decodeBufferPolicy .afterCommandDecoded (readBuffer );
922
927
}
923
928
924
929
private void recordLatency (WithLatency withLatency , RedisCommand <?, ?, ?> command ) {
@@ -960,8 +965,8 @@ private void resetInternals() {
960
965
rsm .reset ();
961
966
}
962
967
963
- if (buffer .refCnt () > 0 ) {
964
- buffer .clear ();
968
+ if (readBuffer != null && readBuffer .refCnt () > 0 ) {
969
+ readBuffer .clear ();
965
970
}
966
971
}
967
972
0 commit comments