user292167
user292167

Reputation: 187

Socket response timeout using poll

I have an existing multi-threaded C++ program which uses a pool of sockets for read and write. The sender thread sends HTTP requests using the next available socket. The receiver therad polls the pool of sockets (using the poll() method) and read the HTTP responses. How the receiver thread can detect a possible HTTP response timeout from a socket of the pool?

Upvotes: 0

Views: 743

Answers (1)

Useless
Useless

Reputation: 67713

Quick example off the top of my head:

  1. when you send a request, create an object containing the fd, any logical connection info you need, and the absolute time at which you'll consider it timed out
  2. keep a priority queue of these objects ordered by time, so the soonest-to-expire is always at the front (this degenerates to a FIFO queue if all requests get the same timeout)
  3. when you poll, calculate the timeout from now to the first absolute time on the queue
  4. if poll returns zero (or just every time it wakes up), get the current time and walk the queue timing out any requests whose timeout is now in the past
  5. when you get a successful response, you'll need to remove the associated object from the queue as well

Upvotes: 2

Related Questions