Question about epoll and splicing

My application is going to send a huge amount of data over the network, so I decided (because I use Linux) to use epoll and splice. This is how I see it (pseudo-code):

epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event while(1) { epoll_wait (tmp_structure); if (tmp_structure->fd == file_descriptor) { epoll_ctl (file_fd, EPOLL_CTL_DEL); epoll_ctl (tcp_socket_fd, EPOLL_CTL_ADD); // wait for EPOLLOUT event } if (tmp_structure->fd == tcp_socket_descriptor) { splice (file_fd, tcp_socket_fd); epoll_ctl (tcp_socket_fd, EPOLL_CTL_DEL); epoll_ctl (file_fd, EPOLL_CTL_ADD); // waiting for EPOLLIN event } } 

I assume that my application will open up to 2000 TCP sockets. I want to ask you about two things:

  • There will be many calls to epoll_ctl, wouldn't it be slow if I have so many sockets?
  • First, the file descriptor must be readable, and before the socket is writable, there will be some interval. Can I be sure that the moment the socket becomes writable, the file descriptor is still readable (to avoid blocking the call)?
+3
source share
2 answers

1st question

  • You can use the generated front, not even the triggered polling, so you do not need to delete sockets every time.
  • You can use EPOLLONESHOT to prevent socket removal

First, the file descriptor must be readable, and there will be some interval before the socket is writable.

What is the file descriptor? If this file is in the file system you cannot use select / poll or other tools for this purpose, the file will always be available for reading or writing, regardless of the state, if the disk and cache. If you need asynchronous staff, you can use the aio_* API, but as a rule, just read from the file, write to the file, and assume that it does not block.

If it is a TCP socket, it will be written most of the time. It's better to use non-blocking calls and put sockets in epoll when you get EWOULDBLOCK.

+2
source

Consider using the EPOLLET flag. It is definitely for this occasion. When using this flag, you can use the event loop correctly, without unregistering (or changing the mode) of the file descriptors from the moment you first registered with epoll. :) enjoy!

+1
source

Source: https://habr.com/ru/post/949211/


All Articles