Skip to content

Commit

Permalink
Release Netty ByteBuf after it is consumed by Tyrus. Issue #7002. (#7042
Browse files Browse the repository at this point in the history
)

Signed-off-by: Santiago Pericasgeertsen <[email protected]>
  • Loading branch information
spericas authored Jun 22, 2023
1 parent b2f265b commit 15a560f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 4 additions & 0 deletions webserver/websocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
<artifactId>helidon-webserver-websocket</artifactId>
<name>Helidon WebServer WebSocket</name>

<properties>
<surefire.argLine>-Dio.netty.leakDetectionLevel=paranoid</surefire.argLine>
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.webserver</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Oracle and/or its affiliates.
* Copyright (c) 2022, 2023 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -69,7 +69,7 @@ class WebSocketHandler extends SimpleChannelInboundHandler<Object> {
private final TyrusServerContainer tyrusServerContainer;
private volatile Connection connection;
private final WebSocketEngine.UpgradeInfo upgradeInfo;
private final BufferedEmittingPublisher<ByteBuffer> emitter;
private final BufferedEmittingPublisher<ByteBuf> emitter;

WebSocketHandler(ChannelHandlerContext ctx, String path,
FullHttpRequest upgradeRequest,
Expand Down Expand Up @@ -154,16 +154,18 @@ public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf byteBuf) {
emitter.emit(byteBuf.copy().nioBuffer());
emitter.emit(byteBuf.copy());
}
}

private void sendBytesToTyrus(ChannelHandlerContext ctx, ByteBuffer nioBuffer) {
private void sendBytesToTyrus(ChannelHandlerContext ctx, ByteBuf byteBuf) {
// Pass all data to Tyrus spi
ByteBuffer nioBuffer = byteBuf.nioBuffer();
int retries = MAX_RETRIES;
while (nioBuffer.remaining() > 0 && retries-- > 0) {
connection.getReadHandler().handle(nioBuffer);
}
byteBuf.release();

// If we can't push all data to Tyrus, cancel and report problem
if (retries == 0) {
Expand Down Expand Up @@ -223,13 +225,13 @@ public void write(ByteBuffer byteBuffer, CompletionHandler<ByteBuffer> completio
return ctx;
}, webSocketRouting.getExecutorService()).thenAccept(c -> Multi.create(emitter)
.observeOn(webSocketRouting.getExecutorService())
.forEach(byteBuffer -> sendBytesToTyrus(c, byteBuffer))
.forEach(byteBuf -> sendBytesToTyrus(c, byteBuf))
.onError(this::logError)
);
} else {
this.connection = upgradeInfo.createConnection(writer, WebSocketHandler::close);
Multi.create(emitter)
.forEach(byteBuffer -> sendBytesToTyrus(ctx, byteBuffer))
.forEach(byteBuf -> sendBytesToTyrus(ctx, byteBuf))
.onError(this::logError);
}

Expand Down

0 comments on commit 15a560f

Please sign in to comment.