Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the read process of netty server?

Shulou Source: shulou.com Published: 2022-05-31 18:53:09 09月22日 Update

This article mainly explains "what is the read process of netty server". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what the read process of netty server is like"!

When the client sends data, exactly how netty performs the read task.

Still start with NioEventLoop, or the NioEventLoop.processSelectedKey method.

Private static void processSelectedKey (SelectionKey k, AbstractNioChannel ch) {

Final NioUnsafe unsafe = ch.unsafe ()

If (! k.isValid ()) {

/ / close the channel if the key is not valid anymore

Unsafe.close (unsafe.voidPromise ())

Return

}

Try {

Int readyOps = k.readyOps ()

/ / Also check for readOps of 0 to workaround possible JDK bug which may otherwise lead

/ / to a spin loop

If ((readyOps & (SelectionKey.OP_READ | SelectionKey.OP_ACCEPT))! = 0 | | readyOps = = 0) {

Unsafe.read ()

If (! ch.isOpen ()) {

/ / Connection already closed-no need to handle write.

Return

}

}

If ((readyOps & SelectionKey.OP_WRITE)! = 0) {

/ / Call forceFlush which will also take care of clear the OP_WRITE once there is nothing left to write

Ch.unsafe () .forceFlush ()

}

If ((readyOps & SelectionKey.OP_CONNECT)! = 0) {

/ / remove OP_CONNECT as otherwise Selector.select (..) Will always return without blocking

/ / See https://github.com/netty/netty/issues/924

Int ops = k.interestOps ()

Ops & = ~ SelectionKey.OP_CONNECT

K.interestOps (ops)

Unsafe.finishConnect ()

}

} catch (CancelledKeyException e) {

Unsafe.close (unsafe.voidPromise ())

}

}

The red font part, unsafe.read (), goes into AbstractNioByteChannel$NioByteUnsafe. To read data from io, you first need to allocate a ByteBuf. Here, two questions are involved: 1, where to allocate memory, 2, how much memory is allocated each time. First of all, the first question, first of all, we need to understand (the difference between allocate and allocateDirect2). The specific implementation in allocator.ioBuffer will determine whether the system memory can be allocated directly according to the platform. Second, you can take a look at the allocHandle.record method, which determines the amount of memory to be allocated later based on the amount of data currently read from the io. Then it reads the data from socket, writes it to bytebuf, and triggers a series of handler.

Public void read () {

Final ChannelConfig config = config ()

Final ChannelPipeline pipeline = pipeline ()

Final ByteBufAllocator allocator = config.getAllocator ()

Final int maxMessagesPerRead = config.getMaxMessagesPerRead ()

RecvByteBufAllocator.Handle allocHandle = this.allocHandle

If (allocHandle = = null) {

This.allocHandle = allocHandle = config.getRecvByteBufAllocator (). NewHandle ()

}

If (! config.isAutoRead ()) {

RemoveReadOp ()

}

ByteBuf byteBuf = null

Int messages = 0

Boolean close = false

Try {

Int byteBufCapacity = allocHandle.guess ()

Int totalReadAmount = 0

Do {

ByteBuf = allocator.ioBuffer (byteBufCapacity)

Int writable = byteBuf.writableBytes ()

Int localReadAmount = doReadBytes (byteBuf)

If (localReadAmount = Integer.MAX_VALUE-localReadAmount) {

/ / Avoid overflow.

TotalReadAmount = Integer.MAX_VALUE

Break

}

TotalReadAmount + = localReadAmount

If (localReadAmount < writable) {

/ / Read less than what the buffer can hold

/ / which might mean we drained the recv buffer completely.

Break

}

} while (+ + messages < maxMessagesPerRead)

Pipeline.fireChannelReadComplete ()

AllocHandle.record (totalReadAmount)

If (close) {

CloseOnRead (pipeline)

Close = false

}

} catch (Throwable t) {

HandleReadException (pipeline, byteBuf, t, close)

}

}

At this point, I believe you have a deeper understanding of "what is the read process of netty server". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Allocation memory data processes methods problems differences content learning practicality deeper tasks interests size fonts practicality practical customers clients platforms Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Technology Linux vpn Shulou Information