How to properly implement keep-alive connection for a C HTTP server?

248 Views Asked by At

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:

  1. I cannot use sleep() for obvious performance reasons.

  2. 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?

1

There are 1 best solutions below

17
Matt Timmermans On

For an epoll server, you use a timeout with epoll_wait to make sure you can run code at least every few seconds, and then every time epoll_wait returns, 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.