My server is implemented using edge-triggered epoll().
I basically need a way to do the following:
process_request(client);
client_timestamp = curr_time;
// do some other stuff, possibly call process_request(client) again and updating the timestamp
when (curr_time - client_timestamp > TIMEOUT) close(client);
The problems are:
I cannot use
sleep()for obvious performance reasons.If I use a thread to monitor each such connection, I may end up running out of threads if the number of clients grows too big.
How should I implement this?
For an
epollserver, you use a timeout with epoll_wait to make sure you can run code at least every few seconds, and then every timeepoll_waitreturns, after performing real work, you incrementally process connection maintenance tasks like handling various timeouts, including keep-alive.To spread out the load, for example, keep connections in a deque. When it's time to do maintenance, calculate the time since the last maintenance check, and then check, say
n = ceil(deque.size() * time/30s)to make sure that each connection gets checked at least every 30s. Move checked connections from the front to the back of the deque.