er,here is a question about netty nioWorker
Netty architecture – questions about NioWorker loop
but i have a different focus,i found that
processRegisterTaskQueue();
processEventQueue();
processWriteTaskQueue();
although this three queues contians Runnable type,but call run() method
private void processWriteTaskQueue() throws IOException {
for (;;) {
final Runnable task = writeTaskQueue.poll();
if (task == null) {
break;
}
task.run();
cleanUpCancelledKeys();
}
}
it means handle queues synchronized,it is possible handler queues too long,and can not do processSelectedKeys in time?
by the way,when i write data,netty push the data into writeBufferQueue,and push an write task into writeTaskQueue,then handler the task when execute processWriteTaskQueue
if (channel.writeTaskInTaskQueue.compareAndSet(false, true)) {
// "add" the channels writeTask to the writeTaskQueue.
boolean offered = writeTaskQueue.offer(channel.writeTask);
assert offered;
}
why not process data in Niowork loop direct?such as processWriteBufferQueue()?
Can somebody explain? thanks
There are two questions in this post.
The first question is: is it possible handler queues too long and cannot do
process SelectKeys()in time?Yes. However, it doesn’t seem to happen unless your handler implementation abuses intentionally.
The second question is: why is write operation always performed in the I/O loop thread?
Otherwise 1) you will see a lot of contention between writer threads if you write from different threads, 2) you will see various socket exceptions due to possible race conditions (connection reset, etc), and 3) Netty internal will become more complicated to deal with such conditions.
Please note that the thread model became more strict and event loop implementation became much simpler in Netty 4, so you might want to take a look in there, too.