Is select () Ok to implement read / write timeout on one socket?

I have a network connection with call blocking application processing. Each thread controls one connection. I added a timeout to the read and write operations using the selection before reading or writing to the socket.

The choice is known as inefficient when dealing with a large number of sockets. But how good is it, in terms of performance, to use it with a single socket, or are there more efficient methods for adding timeout support to calls with single sockets? The advantage of choice is being portable.

+4
source share
3 answers

Yes, this is not a problem, and you need some timeout mechanisms so that resources do not leak from clients with poor work, etc.

Note that having a large number of threads is even more inefficient than choosing to deal with a large number of sockets.

+4
source

If yo uthink select is inefficient with lots of sockets, try handling a lot of sockets with a single thread on sockt. You are in a world of pain. Like you, you will have problems scaling up to 1000 threads.

What I have done in the past is that:

  • Group sockets in groups on X (512, 1024).
  • Skip one or two streams in these groups and select () - then release the sockets with the new data into the queue.
  • There are several workflows that work with these sockets with new data. How much depends how much I need to maximize the processor;)

Thus, I do not do super ΓΌber select () with TONS elements, and I also do not spend an ridiculous amount of memory on threads (hint: each thread needs its own stack). ONLY 2 MB, i.e. 2 GB for 1000 sockets - talk about inefficiency) and drop a huge amount of processor when using useless context switches.

+2
source

The issue with / select threads is whether you want to avoid blocking each other. If this is not a problem, then work single-threaded. If so, select the appropriate threading scheme (1 thread per connection, worker threads per connection, worker thread per request, ...).

When working with 1 thread per connection, the read / write choice is a worthy decision, but as a rule, it is better to work with non-blocking sockets in combination with a choice to avoid blocking in situations when only a part of the expected message is expected, and then make a choice after recording .

0
source

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


All Articles