Skip to content

Commit

Permalink
Add workaround for Netty issue which will be fixed by Netty PR 14071
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari committed May 22, 2024
1 parent 3767f6f commit b9ad8db
Showing 1 changed file with 16 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,23 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
}
}

// .asReadOnly() is needed to prevent SslHandler from modifying the input buffers.
// readonly buffer is needed to prevent SslHandler from modifying the input buffers.
private static ByteBuf readOnlyRetainedDuplicate(ByteBuf buf) {
// If the buffer is already read-only, .asReadOnly() will return the same buffer.
// That's why the additional .retainedDuplicate() is needed to ensure that the returned buffer
// has independent readIndex and writeIndex.
return buf.asReadOnly().retainedDuplicate();
if (buf.readableBytes() == 0) {
return Unpooled.EMPTY_BUFFER;
}

// using "buf.asReadOnly().retainedDuplicate()" would be preferred here, however it's not supported
// by SslHandler until the fix https://github.com/netty/netty/pull/14071 is released.
// There's a need to use ".retainedDuplicate()" together with ".asReadOnly()".
// If the buffer is already read-only, .asReadOnly() will return the current buffer. This would be a problem
// since a duplicate is needed so that the buffer has independent readIndex and writeIndex state.
//
// The alternative workaround is to use "Unpooled.unmodifiableBuffer(buf).retain()". This is a deprecated
// method, but it will achieve the same result with the additional detail that it won't require the PR 14071
// in SslHandler to be released. The reason for this is that isWritable methods return false for an
// unwrapped ReadOnlyByteBuf instance.
return Unpooled.unmodifiableBuffer(buf).retain();
}
}
}

0 comments on commit b9ad8db

Please sign in to comment.