-
-
Save Epictetus/545563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/activeperl | |
use strict; | |
use warnings; | |
use AnyEvent; | |
use AnyEvent::Socket; | |
use AnyEvent::Handle; | |
use IO::Handle; | |
die "$0 <host> <port>" unless @ARGV; | |
my ($host, $port) = @ARGV; | |
my $cv = AE::cv; | |
my $i = 0; | |
my @fh; | |
my $t1 = AE::timer 1000, 0, sub { $cv->send }; | |
my $t2 = AE::timer 0, 0.0001, sub { | |
return if @fh > 10000; | |
tcp_connect $host, $port, sub { | |
$i++; | |
my ($fh) = @_ or warn "can't connect! $!"; | |
warn "connected $i" if $fh; | |
if (!$fh) { | |
closeconn(); | |
return; | |
} | |
push @fh, $fh; | |
my $ready; | |
$ready = AE::io $fh, 1, sub { | |
print $fh "stats\n"; | |
undef $ready; | |
}; | |
}, sub { 0.1 } | |
}; | |
sub closeconn { | |
my $nfh = int( rand(@fh) ); | |
my $fh = splice( @fh, $nfh, 1 ); | |
my $ready; | |
$ready = AE::io $fh, 1, sub { | |
print $fh "stats\n"; | |
undef $ready; | |
close $fh; | |
}; | |
} | |
my $t3 = AE::timer 0, 0.001, sub { | |
if (900 < @fh) { | |
closeconn(); | |
} | |
}; | |
$cv->recv; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment