Process multiple IO events in one event loop

29 views
Skip to first unread message

Chāofēi Fàn

unread,
Aug 29, 2016, 4:53:17 AM8/29/16
to Netty discussions
I originally asked this question on Github:

When reading Netty's codes, I find that Netty's event loop process multiple IO events in one loop, whereas most of tutorials and other framework (e.g. rapidoid) only process one IO event in one loop. Could someone elaborate a little bit on this design? Or I've missed something and both implementation are actually the same? Cheers!

And @normanmaurer posted a helpful reply:

@FlyClover its basically handles all the events for each Channel that are present. If you not handle each of the events it will cause the select() to not block on the next call as there are more events to handle. So at the end you will handle all events in both implementations. That said it is kind of wasteful to not directly handle all events in the first place as you will do extra select() calls while have the same outcome later on.

I've got a further question. In our implementation (on Android), we use rapidoid's way of processing events. Say if a simplified event loop looks like this:

while (true) {
  selector.select();
  Iterator<SelectionKey> iterator = socketSelector.selectedKeys().iterator();
  while (iterator.hasNext()) {
    SelectionKey key = iterator.next();
    if (key.isConnectable()) {

    } else if (key.isAcceptable()) {

    } else if (key.isReadable()) {

    } else if (key.isWritable()) {

    }
    ...
  }
}

The problem we come across in this implementation is that if the channel has lots of read events and few write events, read will block write. If we use Netty's implementation, the problem will be gone. My question is, apart from the benefit @normanmaurer mentioned above, Netty's implementation seems more fair, but why still most of the implementations I could find still use the the other style?
Reply all
Reply to author
Forward
0 new messages