package rewrite;use nginx;use Hash::ConsistentHash;use String::CRC32;use IO::Socket;use Net::SMTP; use MIME::Base64; use IPC::ShareLite;use Storable qw(freeze thaw);use IPC::SysV qw(S_IRWXU IPC_CREAT);use IPC::Semaphore;my @serv_list = qw();my $share = IPC::ShareLite->new( -key => 1949, -create => 'yes', -destroy => 'yes') or die $!;my $sem = IPC::Semaphore->new(1564, 1, S_IRWXU|IPC_CREAT) || die "IPC::Semaphore->new: $!\n";sub handler { my $r = shift; my $uri = $r->uri; my $domain = $r->header_in(host); my $test="" ; if ($sem->getval(0) == 0){ $test = $share->fetch; } if ($test ne "") { splice(@serv_list); my $list = thaw($share->fetch) || print "Wrong\n\n"; @serv_list = @$list; } my $chash = Hash::ConsistentHash->new( buckets => [@serv_list], hash_func => \&crc32 ); my $url = $chash->get_bucket($uri); #$url = "http://".$url."/".$domain.$uri; $url = "http://".$url.$uri; $r->header_out(Location => $url); $r->status(302); $r->send_http_header; return OK;}1;__END__