Slide 1

Slide 1 text

Netty 4 Overview by Trustin Lee on 27-Feb-2013

Slide 2

Slide 2 text

What is Netty? an asynchronous event-driven network application framework for rapid development of maintainable high performance protocol servers & clients

Slide 3

Slide 3 text

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 } });

Slide 4

Slide 4 text

Synchrony via Asynchrony ChannelFuture f = channel.write(message); try { f.sync() .channel().write(nextMessage).sync(); } finally { f.channel().close(); }

Slide 5

Slide 5 text

Event-driven class MyInboundMessageListener extends ChannelInboundMessageHandlerAdapter { @Override void messageReceived( ChannelHandlerContext ctx, MyInboundMessage msg) { MyOutboundMessage res = ...; ctx.write(res); } }

Slide 6

Slide 6 text

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)

Slide 7

Slide 7 text

ChannelHandler type hierarchy ChannelHandler ChannelStateHandler ChannelOperationHandler ChannelInboundByteHandler ChannelInboundMessageHandler ChannelOutboundByteHandler ChannelOutboundMessageHandler *Adapter

Slide 8

Slide 8 text

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); ... }

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

Pipeline: List ChannelPipeline p = ch.pipeline(); p.addLast( new HttpRequestDecoder(), // IB new HttpResponseEncoder(), // OM new MyHttpServiceHandler()); // IM p.addFirst(new SslHandler(...)); // IB //+OB

Slide 11

Slide 11 text

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 } }

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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*()

Slide 14

Slide 14 text

EventLoop type hierarchy ScheduledExecutorService EventExecutorGroup EventLoop EventExecutor EventLoopGroup NioEventLoopGroup AioEventLoopGroup DefaultEvtExecGrp

Slide 15

Slide 15 text

Putting all together NioEventLoopGroup NioSocketChannel ChannelPipeline HttpRequestDecoder EL2 ELn HttpRequestEncoder MyHttpServiceHandler DefaultEvtExecGrp EE1 EEm EL1 EE2

Slide 16

Slide 16 text

Code walk-through via Factorial example

Slide 17

Slide 17 text

Factorial example ● Sends sequential integers to multiply them ● Codec ○ NumberEncoder ○ BigIntegerDecoder ● Server ○ FactorialServerHandler ○ FactorialServerInitializer ○ FactorialServer ● Client ○ FactorialClientHandler ○ FactorialClientInitializer ○ FactorialClient

Slide 18

Slide 18 text

@netty_project netty.io Q & A