I'm writing a tcp server in c that uses epoll() i/o multiplexing to manage concurrent connections. I want to timeout connections that have been idle for more than an allowed time.
So far I keep a last_active time_t variable associated with each connection, which I update to the current time in the event handler. Before doing that, I check if more than the allowed time has ellapsed since last event and if so I terminate the connection.
So far so good but it's not really what I want because the timeout is only triggered on the first out-of-time event, but if the connection remains inactive, my code doesn't detect it until it becomes "active" again.
The way I've seen this done in select() based servers is by linearly traversing the interest set during each iteration of the event loop and purge the inactive connections there. This is not a problem in select because you already have to do this traversal anyways, but I use epoll() precisely to avoid having to do this. If I do this, epoll is no better than select.
I've also looked into socket options, the closest thing I found was SO_RCVTIMEO, which makes read()/recv() return an error if it's been waiting more than the specified time. But since I'm working with i/o multiplexing and the sockets are in nonblock mode this makes no sese because the sockets don't block.
I would appreciate any insight on how to solve this. Thank you very much.