Description: |
With the tremendous traffic caused by the nimdA virus, one of our really old servers couldn't keep up with the load on its web server. (Load averages hit 200 at one point.) Besides, all it really did was redirect traffic elsewhere. I threw together this perl snippet with the help of a Google search to sit on port 80 and do the redirecting more efficiently than Apache was, I think.
|
#!/usr/bin/perl
use strict;
use Socket;
local $SIG{PIPE} = 'IGNORE';
my $proto = getprotobyname('tcp');
my $port = 80;
socket(Server, PF_INET, SOCK_STREAM, $proto) or die "socket: $@";
setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die "set
+sockopt: $!";
bind(Server, sockaddr_in($port, INADDR_ANY)) or die "bind: $!";
listen(Server, SOMAXCONN) or die "listen: $!";
my $paddr;
$SIG{CHLD} = \&REAPER;
for (; $paddr = accept(Client, Server); close Client) {
print Client "HTTP/1.0 302 Over there\n";
print Client "Location: http://xxx.xxxxxx.xxx\n\n";
}
Re: http Redirector
by merlyn (Sage) on Sep 23, 2001 at 12:56 UTC
|
This is one of those programs that gets a bunch simpler when you use more modern interfaces. Programs that "use Socket" and nothing higher should generally
be rewritten.
#!/usr/bin/perl
use strict;
use IO::Socket;
my $s = IO::Socket::INET->new(
LocalPort => 80,
Listen => 5,
Reuse => 1,
MultiHomed => 1) or die;
while (my $c = $s->accept) {
print $c "HTTP/1.0 302 Over there\n";
print $c "Location: http://xxx.xxxxx.xxx\n\n";
}
-- Randal L. Schwartz, Perl hacker | [reply] [d/l] |
|
Merlyn, as always your code looks sweet. Thank you.
I have used IO::Socket before, and would have preferred
use it again. However, this machine is an antiquated (legacy)
SCO server that is thoroughly broken. We fear change on this
box, and instead are migrating all services away, to
OpenBSD computers. This philosophy led me to use the
internal socket instead.
Regardless of my reasons, I agree that your code is
preferable!
Thank you also for the benchmarking. I had no idea that
the performance difference would be so great. I had hoped to
eke out just a few more requests. Sounds like it may handle
dozens. Since the nimdA virus seems to hit servers with
16 requests at a time, we should be in good shape until we
can take that box offline.
| [reply] |
Re: http Redirector
by echo (Pilgrim) on Sep 23, 2001 at 14:26 UTC
|
This code handles one request at a time, hence serializing all requests. If the request rate is high, I don't see how this can bring more performance than Apache, which if properly tuned will have multiple processes ready to handle simultaneous requests.
| [reply] |
|
Apache has far more overhead from connect to response. And ultimately, all requests are serialized anyway, because only one process can accept at a time. And
on a monoprocessor machine, the ultimate code execution is serial as well {grin}.
I'd imagine this script (the original version or my version) would handle demonstratably more requests per second than Apache would.
-- Randal L. Schwartz, Perl hacker
update
Yes, I just benchmarked it. My apache did about 80 requests per second,
and my Perl program did about 750(!) requests per second. This was for 10
concurrent requests, timed over 1000 requests, using the apache bench program.
| [reply] |
|
|