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
| [reply] [Watch: Dir/Any] [d/l] [select] |
@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. | [reply] [Watch: Dir/Any] [d/l] |
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.
| [reply] [Watch: Dir/Any] |
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". ;-)
| [reply] [Watch: Dir/Any] |