Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Netty 4 Overview (Obsolete)

Netty 4 Overview (Obsolete)

UPDATE: This presentation is out-of-date. We changed a lot of things in the public API since then.

Netty is a Java asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients. It powers many large scale services built on top of JVM such as Twitter.

This talk will provide an overview of Netty 4, the major backward-incompatible update against the version 3, from basic constructs to a working example. It's not necessarily for those who are familiar with the previous versions on Netty.

Trustin Lee

February 27, 2013
Tweet

More Decks by Trustin Lee

Other Decks in Programming

Transcript

  1. What is Netty? an asynchronous event-driven network application framework for

    rapid development of maintainable high performance protocol servers & clients
  2. Asynchronous Channel channel = ... ChannelFuture f = channel.write(message); f.addListener((f)

    -> { if (f.isSuccess()) { f.channel().write(nextMessage); // Async } else { f.cause().printStackTrace(); f.channel().close(); // Async } });
  3. Synchrony via Asynchrony ChannelFuture f = channel.write(message); try { f.sync()

    .channel().write(nextMessage).sync(); } finally { f.channel().close(); }
  4. Event-driven class MyInboundMessageListener extends ChannelInboundMessageHandlerAdapter <MyInboundMessage> { @Override void messageReceived(

    ChannelHandlerContext ctx, MyInboundMessage msg) { MyOutboundMessage res = ...; ctx.write(res); } }
  5. Available events • channelRegistered/Active • inboundBufferUpdated ◦ Most handlers have

    its own inbound buffer. ◦ Much less GC pressure than generating event objects per message ◦ CIMHAdapter.inboundBufferUpdated() invokes messageReceived() • channelInactive/Unregistered • exceptionCaught (Throwable) • userEventTriggered (Object)
  6. ChannelInboundByteHandlerAdapter abstract class ChannelInboundByteHandlerAdapter ... { ByteBuf newInboundBuffer(ChannelHandlerContext ctx) {

    return ctx.alloc().buffer(); } void freeInboundBuffer(ChannelHandlerContext ctx) { ctx.inboundByteBuffer().release(); } final void inboundBufferUpdated(ChannelHandlerContext ctx) { inboundBufferUpdated(ctx, ctx.inboundByteBuffer()); } abstract void inboundBufferUpdated(ctx, ByteBuf in); ... }
  7. ByteBufAllocator • "ctx.alloc().buffer()" • The default: PooledByteBufAllocator ◦ Pure Java

    jemalloc impl with some twist ◦ Beats JVM by not filling with NUL(0) ◦ Takes advantage of sun.misc.Unsafe to: ▪ Deallocate direct buffers timely ▪ Access direct buffers directly (+10% faster) ◦ Reference-counted ▪ "buf.release()" and "buf.retain()" ▪ Sampled leak detection enabled by '-D' flag
  8. Pipeline: List<ChannelHandler> ChannelPipeline p = ch.pipeline(); p.addLast( new HttpRequestDecoder(), //

    IB new HttpResponseEncoder(), // OM new MyHttpServiceHandler()); // IM p.addFirst(new SslHandler(...)); // IB //+OB
  9. Pipeline: dynamic manipulation class HttpAndHttpsHandler extends ChannelInboundByteHandlerAdapter { void inboundBufferUpdated(ctx,

    inBuf) { if (isHttp(inBuf)) { addHttpHandlers(ctx, inBuf); } else if (isSsl(inBuf)) { addSslHandler(ctx, inBuf); } else { ctx.close(); // Unknown protocol } }
  10. Pipeline: staged event threading EventExecutorGroup group = new DefaultEventExecutorGroup(16); ChannelPipeline

    p = channel.pipeline(); p.addLast( new RedisRequestDecoder(), // IB new RedisResponseEncoder()); // OM p.addLast( group, new RedisServiceHandler()); // IM
  11. EventExecutor and EventLoop • EventLoop ◦ performs actual I/O ◦

    triggers an event to a pipeline ◦ receives an operation from a pipeline ◦ performs arbitrary tasks submitted by user • EventExecutor ◦ EventLoop minus I/O ◦ Usually specified as an arg to ChannelPipeline. add*()
  12. Factorial example • Sends sequential integers to multiply them •

    Codec ◦ NumberEncoder ◦ BigIntegerDecoder • Server ◦ FactorialServerHandler ◦ FactorialServerInitializer ◦ FactorialServer • Client ◦ FactorialClientHandler ◦ FactorialClientInitializer ◦ FactorialClient