Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Designing an enqueing application proxy

by BrowserUk (Patriarch)
on Jan 04, 2008 at 02:54 UTC ( [id://660367]=note: print w/replies, xml ) Need Help??


in reply to Designing an enqueing application proxy

While you wait for someone to post a POE solution (could take a while), you might get away with something as simple as this:

#! perl -slw use strict; use IO::Socket; use IO::Select; use threads; use threads::shared; use Thread::Queue; my $done :shared = 0; my $Qin = new Thread::Queue; my $Qout = new Thread::Queue; sub listener { my %conns; my $listener = IO::Socket::INET->new( LocalAddr => 'localhost:54321', Listen => 10, Reuse => 1 ) or die "Failed to create listener: $!"; my $sel = new IO::Select( $listener ); until( $done ) { if( $sel->can_read( 0.01 ) ) { my $client = $listener->accept; $conns{ $client } = $client; my $req = <$client>; ## Assumes request is a single line $Qin->enqueue( "$client : $req" ); warn "Accepted and queued request '$req'"; } elsif( $Qout->pending ) { my( $client, $res ) = split ' : ', $Qout->dequeue, 2; print { $conns{ $client } } $res; close $conns{ $client }; delete $conns{ $client }; } } close $listener; } my $lt = threads->create( \&listener ); while( my( $client, $req ) = split ' : ', $Qin->dequeue, 2 ) { async{ my $serv = IO::Socket::INET->new( 'localhost:12345' ); print $serv $req; warn "dequeue and forwarded '$req'"; my $res = <$serv>; warn "Retrieved and queued '$res'"; $Qout->enqueue( "$client : $res" ); close $serv; }->detach; select '','','', 0.1; ## Ensure no more than 10/s }

This creates 2 threads. The listener thread, which accepts connections, maintains a (non-shared) hash of client sockets and queues the requests as they are recieved. Then the main thread dequeues the requests, spawns a thread to forward that request and await the reply before queing that back to the listener thread. A 1/10 second delay in the second threads processing loop ensures the request rate doesn't exceed 10/second. When the spawned threads receive their replies, they are queued back to the listener thread for dispatch back to the appropriate socket.

Of course, a thread pool solution for the upstream connections might be more efficient (as well as a tad more complicated), but whether that is necessary depends a lot upon the burst rate of your PE process?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://660367]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 12:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found