Mio

When working with non-blocking sockets, we need a way to check whether the socket is ready for the desired operation. The situation is worse when we have thousands, or more, sockets to manage. We can use the very inefficient way of running a loop, checking for the socket state, and performing the operation once it's ready. But there are better ways to do this. In Unix, we had the poll system call, to which you give the list of file descriptors you want to be monitored for events. It was then replaced by the select system call, which improved things a bit. However, both select and poll were not scalable as they were basically for loops under the hood and the iteration time went up linearly as more and more sockets were added to its monitor list.

Under Linux, then came epoll, which is the current and most efficient file descriptor multiplexing API. This is used by most network and I/O applications that want to do asynchronous I/O. Other platforms have similar abstractions, such as kqueue in macOS and BSD. On Windows, we have IO Completion Ports (IOCP).

It is these low-level abstractions that mio abstracts over, providing a cross-platform, highly efficient interface to all of these I/O multiplexing APIs. Mio is quite a low-level library, but it provides a convenient way to set up a reactor for socket events. It provides the same kind of networking primitives such as the TcpStream type as the standard library does, but these are non-blocking by default.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset