Skip to content

Commit 32e301e

Browse files
committed
Make UnusedChannelExceptionHandler sharable
1 parent 4b459b6 commit 32e301e

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/ChannelPipelineInitializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void channelCreated(Channel ch) {
8383
}
8484

8585
pipeline.addLast(FutureCancelHandler.getInstance());
86-
pipeline.addLast(new UnusedChannelExceptionHandler());
86+
pipeline.addLast(UnusedChannelExceptionHandler.getInstance());
8787
pipeline.addLast(new LoggingHandler(LogLevel.DEBUG));
8888
}
8989

http-clients/netty-nio-client/src/main/java/software/amazon/awssdk/http/nio/netty/internal/UnusedChannelExceptionHandler.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.awssdk.http.nio.netty.internal;
1717

18+
import io.netty.channel.ChannelHandler;
1819
import io.netty.channel.ChannelHandlerAdapter;
1920
import io.netty.channel.ChannelHandlerContext;
2021
import io.netty.util.Attribute;
@@ -33,9 +34,15 @@
3334
* This prevents spamming customer logs when errors (eg. connection resets) occur on an unused channel.
3435
*/
3536
@SdkInternalApi
36-
public class UnusedChannelExceptionHandler extends ChannelHandlerAdapter {
37+
@ChannelHandler.Sharable
38+
public final class UnusedChannelExceptionHandler extends ChannelHandlerAdapter {
39+
public static final UnusedChannelExceptionHandler INSTANCE = new UnusedChannelExceptionHandler();
40+
3741
private static final Logger log = Logger.loggerFor(UnusedChannelExceptionHandler.class);
3842

43+
private UnusedChannelExceptionHandler() {
44+
}
45+
3946
@Override
4047
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
4148
boolean channelInUse = getAttribute(ctx, ChannelAttributeKey.IN_USE).orElse(false);
@@ -65,6 +72,10 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
6572
}
6673
}
6774

75+
public static UnusedChannelExceptionHandler getInstance() {
76+
return INSTANCE;
77+
}
78+
6879
private <T> Optional<T> getAttribute(ChannelHandlerContext ctx, AttributeKey<T> key) {
6980
return Optional.ofNullable(ctx.channel().attr(key))
7081
.map(Attribute::get);

http-clients/netty-nio-client/src/test/java/software/amazon/awssdk/http/nio/netty/internal/UnusedChannelExceptionHandlerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void setUp() {
3939
public void inUseDoesNothing() {
4040
Mockito.when(inUseAttribute.get()).thenReturn(true);
4141

42-
new UnusedChannelExceptionHandler().exceptionCaught(ctx, exception);
42+
UnusedChannelExceptionHandler.getInstance().exceptionCaught(ctx, exception);
4343

4444
Mockito.verify(ctx).fireExceptionCaught(exception);
4545
Mockito.verify(ctx, new Times(0)).close();
@@ -59,7 +59,7 @@ private void notInUseCloses(Throwable exception) {
5959
Mockito.when(inUseAttribute.get()).thenReturn(false);
6060
Mockito.when(futureAttribute.get()).thenReturn(CompletableFuture.completedFuture(null));
6161

62-
new UnusedChannelExceptionHandler().exceptionCaught(ctx, exception);
62+
UnusedChannelExceptionHandler.getInstance().exceptionCaught(ctx, exception);
6363

6464
Mockito.verify(ctx).close();
6565
}
@@ -71,7 +71,7 @@ public void notInUseFutureCompletes() {
7171
Mockito.when(inUseAttribute.get()).thenReturn(false);
7272
Mockito.when(futureAttribute.get()).thenReturn(incompleteFuture);
7373

74-
new UnusedChannelExceptionHandler().exceptionCaught(ctx, exception);
74+
UnusedChannelExceptionHandler.getInstance().exceptionCaught(ctx, exception);
7575

7676
Mockito.verify(ctx).close();
7777
assertThat(incompleteFuture.isDone()).isTrue();

0 commit comments

Comments
 (0)