为什么 Netty HTTP 处理程序不可共享?

每当打开新连接时, Netty 都会实例化一组请求处理程序类。对于像 Websocket 这样的东西来说,这似乎很好,因为连接将在 Websocket 的生命周期内保持打开状态。

当使用 Netty 作为每秒可以接收数千个请求的 HTTP 服务器时,这似乎会对垃圾收集造成相当大的负担。每个请求都会实例化几个类(在我的例子中是 10 个处理程序类),然后垃圾收集器会在几毫秒后收集它们。

在中等负载 ~1000 req/sec 的 HTTP 服务器中,每秒需要实例化和垃圾收集一万个类。

看来我们可以简单地 看到下面的答案,创建可共享的处理程序,这将使用 消除这种巨大的 GC 开销ChannelHandler.Sharable。它们只需是线程安全的。

但是,我发现库中打包的所有非常基本的 HTTP 处理程序都是不可共享的,例如HttpServerCodecHttpObjectAggregator。此外,所有HTTP 处理程序示例都是不可共享的。99% 的示例代码和教程似乎都不关心它。Norman Maurer 的书(Netty 作者)中只有一则简介给出了使用共享处理程序的原因:

为什么要共享 ChannelHandler?

在多个 ChannelPipelines 中安装单个 ChannelHandler 的常见原因是跨多个 Channel 收集统计信息。

任何地方都没有提到 GC 负载问题。



Netty 已在常规生产环境中使用了近十年。它可以说是目前最常用的高度并发非阻塞 IO 的 Java 库。


换句话说,它的设计目的是远远超过我每秒 1000 个适度的请求。

我是否错过了一些使 GC 负载不再成为问题的东西?

或者,我是否应该尝试实现自己的Sharable处理程序,具有类似的功能来解码、编码和写入 HTTP 请求和响应?


慕盖茨4494581
浏览 139回答 1
1回答

呼唤远方

虽然我们始终致力于在 Netty 中产生尽可能少的 GC,但在某些情况下这是不可能的。例如,http 编解码器等保留每个连接的状态,因此这些无法共享(即使它们是线程安全的)。解决这个问题的唯一方法是对它们进行池化,但我认为还有其他对象更有可能导致 GC 问题,对于这些对象,我们会在可能的情况下尝试进行池化。

叮当猫咪

长话短说:如果使用默认 HTTP 处理程序达到了使 GC 成为问题所需的量,那么无论如何都应该使用代理服务器进行扩展。在 Norman 的回答之后,我最终尝试了一个非常简单的可共享 HTTP 编解码器/聚合器 POC,看看这是否值得追求。我的可共享解码器距离RFC 7230还很远,但它满足了我当前项目的足够要求。然后我使用httperf和VisualVM来了解 GC 负载差异的概念。经过我的努力,GC 率仅降低了 10%。换句话说,这确实没有多大区别。唯一真正值得赞赏的效果是,与使用打包的非共享 HTTP 编解码器 + 聚合器和我的可共享编解码器相比,在运行 1000 个请求/秒时,我的错误减少了 5%。只有当我每秒执行 1000 个请求并持续超过 10 秒时才会出现这种情况。最后我不会去追求它。为了获得通过使用代理服务器可以解决的微小好处而将其变成完全兼容 HTTP 的解码器所需的时间根本不值得。出于参考目的,这里是我尝试过的组合可共享解码器/聚合器:import java.util.concurrent.ConcurrentHashMap;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelHandler.Sharable;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelId;import io.netty.channel.ChannelInboundHandlerAdapter;@Sharablepublic class SharableHttpDecoder extends ChannelInboundHandlerAdapter {&nbsp; &nbsp; private static final ConcurrentHashMap<ChannelId, SharableHttpRequest> MAP =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new ConcurrentHashMap<ChannelId, SharableHttpRequest>();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void channelRead(ChannelHandlerContext ctx, Object msg)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; throws Exception&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (msg instanceof ByteBuf)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteBuf buf = (ByteBuf) msg;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChannelId channelId = ctx.channel().id();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SharableHttpRequest request = MAP.get(channelId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (request == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request = new SharableHttpRequest(buf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.release();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (request.isComplete())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fireChannelRead(request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MAP.put(channelId, request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; request.append(buf);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.release();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (request.isComplete())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fireChannelRead(request);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO send 501&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("WTF is this? " + msg.getClass().getName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.fireChannelRead(msg);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; throws Exception&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Unable to handle request on channel: " +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ctx.channel().id().asLongText());&nbsp; &nbsp; &nbsp; &nbsp; cause.printStackTrace(System.err);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // TODO send 500&nbsp; &nbsp; &nbsp; &nbsp; ctx.fireExceptionCaught(cause);&nbsp; &nbsp; &nbsp; &nbsp; ctx.close();&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}解码器创建的用于在管道上处理的结果对象:import java.util.Arrays;import java.util.HashMap;import io.netty.buffer.ByteBuf;public class SharableHttpRequest{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private static final byte SPACE = 32;&nbsp; &nbsp; private static final byte COLON = 58;&nbsp; &nbsp; private static final byte CARRAIGE_RETURN = 13;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private HashMap<Header,String> myHeaders;&nbsp; &nbsp; private Method myMethod;&nbsp; &nbsp; private String myPath;&nbsp; &nbsp; private byte[] myBody;&nbsp; &nbsp; private int myIndex = 0;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public SharableHttpRequest(ByteBuf buf)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myHeaders = new HashMap<Header,String>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final StringBuilder builder = new StringBuilder(8);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseRequestLine(buf, builder);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while (parseNextHeader(buf, builder));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parseBody(buf);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; catch (Exception e)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace(System.err);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public String getHeader(Header name)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myHeaders.get(name);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public Method getMethod()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myMethod;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public String getPath()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myPath;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public byte[] getBody()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myBody;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public boolean isComplete()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return myIndex >= myBody.length;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public void append(ByteBuf buf)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int length = buf.readableBytes();&nbsp; &nbsp; &nbsp; &nbsp; buf.getBytes(buf.readerIndex(), myBody, myIndex, length);&nbsp; &nbsp; &nbsp; &nbsp; myIndex += length;&nbsp; &nbsp; }&nbsp; &nbsp; private void parseRequestLine(ByteBuf buf, StringBuilder builder)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int idx = buf.readerIndex();&nbsp; &nbsp; &nbsp; &nbsp; int end = buf.writerIndex();&nbsp; &nbsp; &nbsp; &nbsp; for (; idx < end; ++idx)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte next = buf.getByte(idx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // break on CR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next == CARRAIGE_RETURN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we need the method&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (myMethod == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next == SPACE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myMethod = Method.fromBuilder(builder);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.delete(0, builder.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.ensureCapacity(100);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append((char) next);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we need the path&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (myPath == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next == SPACE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myPath = builder.toString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.delete(0, builder.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append((char) next);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // don't need the version right now&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; idx += 2; // skip line endings&nbsp; &nbsp; &nbsp; &nbsp; buf.readerIndex(idx);&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private boolean parseNextHeader(ByteBuf buf, StringBuilder builder)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Header header = null;&nbsp; &nbsp; &nbsp; &nbsp; int idx = buf.readerIndex();&nbsp; &nbsp; &nbsp; &nbsp; int end = buf.writerIndex();&nbsp; &nbsp; &nbsp; &nbsp; for (; idx < end; ++idx)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte next = buf.getByte(idx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // break on CR&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next == CARRAIGE_RETURN)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (header != Header.UNHANDLED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myHeaders.put(header,builder.toString());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.delete(0, builder.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (header == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we have the full header name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (next == COLON)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header = Header.fromBuilder(builder);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.delete(0, builder.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get header name as lower case for mapping purposes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append(next > 64 && next < 91 ?&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (char) ( next | 32 ) : (char) next);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // we don't care about some headers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (header == Header.UNHANDLED)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // skip initial spaces&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (builder.length() == 0 && next == SPACE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // get the header value&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; builder.append((char) next);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; idx += 2; // skip line endings&nbsp; &nbsp; &nbsp; &nbsp; buf.readerIndex(idx);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (buf.getByte(idx) == CARRAIGE_RETURN)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; idx += 2; // skip line endings&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.readerIndex(idx);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; private void parseBody(ByteBuf buf)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int length = buf.readableBytes();&nbsp; &nbsp; &nbsp; &nbsp; if (length == 0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myBody = new byte[0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myIndex = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("Content-Length: " + myHeaders.get(Header.CONTENT_LENGTH));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (myHeaders.get(Header.CONTENT_LENGTH) != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int totalLength = Integer.valueOf(myHeaders.get(Header.CONTENT_LENGTH));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myBody = new byte[totalLength];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buf.getBytes(buf.readerIndex(), myBody, myIndex, length);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myIndex += length;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // TODO handle chunked&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public enum Method&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; GET(new char[]{71, 69, 84}),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; POST(new char[]{80, 79, 83, 84}),&nbsp; &nbsp; &nbsp; &nbsp; UNHANDLED(new char[]{}); // could be expanded if needed&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; private char[] chars;&nbsp; &nbsp; &nbsp; &nbsp; Method(char[] chars)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.chars = chars;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public static Method fromBuilder(StringBuilder builder)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Method method : Method.values())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (method.chars.length == builder.length())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean match = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < builder.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (method.chars[i] != builder.charAt(i))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (match)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return method;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; public enum Header&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; HOST(new char[]{104, 111, 115, 116}),&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; CONNECTION(new char[]{99, 111, 110, 110, 101, 99, 116, 105, 111, 110}),&nbsp; &nbsp; &nbsp; &nbsp; IF_MODIFIED_SINCE(new char[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 105, 102, 45, 109, 111, 100, 105, 102, 105, 101, 100, 45, 115,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 105, 110, 99, 101}),&nbsp; &nbsp; &nbsp; &nbsp; COOKIE(new char[]{99, 111, 111, 107, 105, 101}),&nbsp; &nbsp; &nbsp; &nbsp; CONTENT_LENGTH(new char[]{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 99, 111, 110, 116, 101, 110, 116, 45, 108, 101, 110, 103, 116, 104}),&nbsp; &nbsp; &nbsp; &nbsp; UNHANDLED(new char[]{}); // could be expanded if needed&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; private char[] chars;&nbsp; &nbsp; &nbsp; &nbsp; Header(char[] chars)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.chars = chars;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; public static Header fromBuilder(StringBuilder builder)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Header header : Header.values())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (header.chars.length == builder.length())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; boolean match = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < builder.length(); i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (header.chars[i] != builder.charAt(i))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; match = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (match)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return header;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return UNHANDLED;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}用于测试的简单处理程序:import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandler.Sharable;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.util.CharsetUtil;@Sharablepublic class SharableHttpHandler extends SimpleChannelInboundHandler<SharableHttpRequest>{&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void channelRead0(ChannelHandlerContext ctx, SharableHttpRequest msg)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; throws Exception&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; String message = "HTTP/1.1 200 OK\r\n" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content-type: text/html\r\n" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content-length: 42\r\n\r\n" +&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "<html><body>Hello sharedworld</body><html>";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ByteBuf buffer = ctx.alloc().buffer(message.length());&nbsp; &nbsp; &nbsp; &nbsp; buffer.writeCharSequence(message, CharsetUtil.UTF_8);&nbsp; &nbsp; &nbsp; &nbsp; ChannelFuture flushPromise = ctx.channel().writeAndFlush(buffer);&nbsp; &nbsp; &nbsp; &nbsp; flushPromise.addListener(ChannelFutureListener.CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; if (!flushPromise.isSuccess())&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flushPromise.cause().printStackTrace(System.err);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;}使用这些可共享处理程序的完整管道:import tests.SharableHttpDecoder;import tests.SharableHttpHandler;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.socket.SocketChannel;public class ServerPipeline extends ChannelInitializer<SocketChannel>{&nbsp; &nbsp; private final SharableHttpDecoder decoder = new SharableHttpDecoder();&nbsp; &nbsp; private final SharableHttpHandler handler = new SharableHttpHandler();&nbsp; &nbsp; @Override&nbsp; &nbsp; public void initChannel(SocketChannel channel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ChannelPipeline pipeline = channel.pipeline();&nbsp; &nbsp; &nbsp; &nbsp; pipeline.addLast(decoder);&nbsp; &nbsp; &nbsp; &nbsp; pipeline.addLast(handler);&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }}上面的内容针对这个(更常见的)非共享管道进行了测试:import static io.netty.handler.codec.http.HttpResponseStatus.OK;import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerContext;import io.netty.channel.ChannelInitializer;import io.netty.channel.ChannelPipeline;import io.netty.channel.SimpleChannelInboundHandler;import io.netty.channel.socket.SocketChannel;import io.netty.handler.codec.http.DefaultFullHttpResponse;import io.netty.handler.codec.http.FullHttpRequest;import io.netty.handler.codec.http.FullHttpResponse;import io.netty.handler.codec.http.HttpHeaderNames;import io.netty.handler.codec.http.HttpHeaderValues;import io.netty.handler.codec.http.HttpObjectAggregator;import io.netty.handler.codec.http.HttpServerCodec;import io.netty.handler.codec.http.HttpUtil;import io.netty.util.CharsetUtil;public class ServerPipeline extends ChannelInitializer<SocketChannel>{&nbsp; &nbsp; @Override&nbsp; &nbsp; public void initChannel(SocketChannel channel)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; ChannelPipeline pipeline = channel.pipeline();&nbsp; &nbsp; &nbsp; &nbsp; pipeline.addLast(new HttpServerCodec());&nbsp; &nbsp; &nbsp; &nbsp; pipeline.addLast(new HttpObjectAggregator(65536));&nbsp; &nbsp; &nbsp; &nbsp; pipeline.addLast(new UnsharedHttpHandler());&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; class UnsharedHttpHandler extends SimpleChannelInboundHandler<FullHttpRequest>&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws Exception&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String message = "<html><body>Hello sharedworld</body><html>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ByteBuf buffer = ctx.alloc().buffer(message.length());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer.writeCharSequence(message.toString(), CharsetUtil.UTF_8);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, OK, buffer);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpUtil.setContentLength(response, response.content().readableBytes());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ChannelFuture flushPromise = ctx.writeAndFlush(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flushPromise.addListener(ChannelFutureListener.CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java