Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: Help finding relevant modules to write a port forwarder from pool

by mje (Curate)
on Oct 08, 2009 at 15:20 UTC ( [id://800002]=note: print w/replies, xml ) Need Help??


in reply to Re: Help finding relevant modules to write a port forwarder from pool
in thread Help finding relevant modules to write a port forwarder from pool

Thanks merlyn but unfortunately at this stage in the chain the protocol is not HTTP but just a small string of JSON passed in and a string of JSON passed back.

HTTP is used further up the chain but it would be a lot of work to set up multiple web servers at this stage.

The ultimate processes receiving the requests are connected to a DB and have a number of prepared statements already prepared so although these could fork, the reconnection time and prepare times would make it a waste and hence the solution I am looking for.

Replies are listed 'Best First'.
Re^3: Help finding relevant modules to write a port forwarder from pool
by MarkovChain (Sexton) on Oct 08, 2009 at 15:32 UTC

    Check out POE http://poe.perl.org. If it's your first foray, it will take a while to get used to it (Do a search on PM for poe tutorial and you will come up with a discussion). IMHO it would be a better investment to lean POE and do this using it rather than coding it at a low level. Plus POE is non-blocking.

    AnyEvent is another alternative.

    - Markov

      Some of the other processes in this project already use POE and I am familiar with it. This process is just to mimic an expensive piece of hardware at this stage and as far as I can see it simply boils down to:

      @pool = (1..N); listen on port N loop { select(waiting for connect); accept pick port from pool fork child does connect, reads socket, writes to forwarder socket, reads results and writes back to client. closes sockets and exits. parent looks for dead children to add port back to pool }

      and POE seems a bit of an overkill for this. Guess I'll knock up the above first.

        If POE seems like overkill, take a look at IO::Lambda. It's a bit weird, but it should be able to make the thing you need without having to fork for each connection.

        -- Randal L. Schwartz, Perl hacker

        The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re^3: Help finding relevant modules to write a port forwarder from pool
by afoken (Chancellor) on Oct 10, 2009 at 07:09 UTC

    I once wrote a simple-and-stupid protocol-neutral load balancer in Perl and C, where Perl ran as a daemon selecting the best server, and C ran a TCP-to-TCP forwarder.

    For each incoming TCP connection, the C code asked the Perl code for the best server (using UDP), connected to that server, and forwarded bytes in both directions until both sides had closed the connection. The perl code had a list of available servers for each TCP port, and a little monitor function that regularily checked that the servers were still alive.

    That worked quite well with HTTP, telnet (just to test the code), and a propritary protocol for a native API. So, if your JSON passing protocol opens a new TCP connection for each request, this type of load balancer should work for you. It will probably also work if you stuff a few JSON strings through the connection, but it will not work if you keep the TCP connection open as long as possible.

    Unfortunately, I have no permission to publish the code, but feel free to ask for more details.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 15:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found