Netty 4:如何在不获取 IllegalReferenceCountException 的情况下发送相同的 DefaultFullHttpResponse 实例
Netty 4: How to send the same DefaultFullHttpResponse instance without getting a IllegalReferenceCountException
我正在使用 netty 4 编写一个模拟服务器。您可以为模拟服务器提供一个 DefaultFullHttpResponse
,每次指定的请求匹配时都应该返回它。
如果响应最多发送 2 次,则此方法工作正常,当发送第 3 次时抛出 IllegalReferenceCountException
。据我了解,DefaultFullHttpResponse
有自己的 ByteBuf(http 内容)。因此,当发送响应时,引用计数器会递减。这个逻辑不在我手中,因为它是由 HttpCodec 完成的。
我的问题是:如何多次使用同一个 DefaultFullHttpResponse
?发送时我需要retain()
内容,对吗?这不是问题,因为所有响应都是 DefaultFullHttpResponse
,但模拟服务器是通用的,允许使用其他协议和编解码器。
如果响应消息像 DefaultFullHttpResponse
那样实现 ByteBufHolder
,则可以复制其 ByteBuf。
if (resp is ByteBufHolder) {
resp = resp.duplicate() //duplicates the byte buffer of the original message
resp.retain()
resp.touch()
}
我正在使用 netty 4 编写一个模拟服务器。您可以为模拟服务器提供一个 DefaultFullHttpResponse
,每次指定的请求匹配时都应该返回它。
如果响应最多发送 2 次,则此方法工作正常,当发送第 3 次时抛出 IllegalReferenceCountException
。据我了解,DefaultFullHttpResponse
有自己的 ByteBuf(http 内容)。因此,当发送响应时,引用计数器会递减。这个逻辑不在我手中,因为它是由 HttpCodec 完成的。
我的问题是:如何多次使用同一个 DefaultFullHttpResponse
?发送时我需要retain()
内容,对吗?这不是问题,因为所有响应都是 DefaultFullHttpResponse
,但模拟服务器是通用的,允许使用其他协议和编解码器。
如果响应消息像 DefaultFullHttpResponse
那样实现 ByteBufHolder
,则可以复制其 ByteBuf。
if (resp is ByteBufHolder) {
resp = resp.duplicate() //duplicates the byte buffer of the original message
resp.retain()
resp.touch()
}