http://qs321.pair.com?node_id=649498


in reply to TCP Socket, Forking, Memory exhaustion

First I would recommend having a look at some generic solutions on CPAN, so that you can prototype quickly different approaches: some good ones are Net::Server, IO::Multiplex, IO::SessionSet and IO::SessionData in the lib directory of the perl network programming site (for non-blocking sockets in case you cannot trust your clients which is almost always the case ;))

Secondly if you maintain lots of open connections a select loop might be the way to go. Multiplexing on a few "select-loop " servers could be used to make the approach more scalable ( a request is sent to one of the children in the pool, and that child does the accept and adds the descriptor to the "select" set).

Finally studying part of the code of aproxy (a small TCP port forwarder) could be useful too

btw which platform are you on?

update Humm then there is the question of your bidirectional protocol. It is easy to do that part wrongly. I would suggest a fork for "each request to a connected client" (you need the socket descriptor of that client) IOW a fork for each server write... with COW schemes this does not have to be so costly. You could even prototype with doing a fork-exec of scripts in a server-request/ directory. It all depends on the average time of the request/answer (and your protocol). You'll need also to monitor the number of processes.

cheers --stephan