Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

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

by merlyn (Sage)
on Oct 08, 2009 at 15:06 UTC ( #799994=note: print w/ replies, xml ) Need Help??


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

You're looking for a "load balancer". If your protocol is HTTP, you're in luck... see PerlBal.

-- 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.


Comment on Re: Help finding relevant modules to write a port forwarder from pool
Re^2: Help finding relevant modules to write a port forwarder from pool
by mje (Deacon) on Oct 08, 2009 at 15:20 UTC

    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.

      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.

      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
Node Status?
node history
Node Type: note [id://799994]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (10)
As of 2014-09-02 18:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (29 votes), past polls