Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a Perl project that has a client sending requests to a server and the ssl connection used for this is kept open until the response is complete. What I want to do is add a layer between that queues the requests and possibly combines them when is makes sense to do so. So it will listen for client requests and then bundle them to send to the server. When the server responds, the response will be broken up to send back to the requesting clients. I'm not a Perl expert but I've been doing software for a while. Here is what I have tried to do on the server side. I created a thread for listening for incoming request, one for listening to responses from the server and one for working the Thread Queue I created to hold the requests. I'm not sure this is the correct architecture in the first place, but my problem seems to be that I can't put the connection into the queue so it can be used later when this layer needs to send a response to the clients.

Is there a way to do this? Or should I push for a redesign of the way communications are done between client and server? I'm starting to think a redesign would be best, but this would require a listener on the client side for responses and matching them up with previous requests.

Any response would be appreciated

Replies are listed 'Best First'.
Re: threadpools and tcp connections
by zentara (Cardinal) on Dec 26, 2010 at 23:23 UTC
    See Simple threaded chat server. The sweet things about threads, is that all threads can share the same filehandles thru the fileno, which can be passed thru shared variables.

    So... you can have your main thread do the accept on the connections, and hand them off to a set of threads, by passing the fileno to it. See where the thread itself can remove itself from a select list at the comment #remove multi-echo-clients from echo list.

    But yes, it all basically works good together, threads and tcp.


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: threadpools and tcp connections
by Corion (Patriarch) on Dec 26, 2010 at 23:23 UTC

    You can't pass objects between threads, so putting the TCP connection into the Thread::Queue will not work. But as the TCP connection is not reall y needed in the worker thread, you can store the TCP connection in a shared hash or keep it completely in the client and just send some ID with the request. When you get the ID back, you know which socket connection the answer belongs to.