Skip to content

Instantly share code, notes, and snippets.

@Epictetus
Forked from mala/memcachedos.pl
Created August 23, 2010 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Epictetus/545563 to your computer and use it in GitHub Desktop.
Save Epictetus/545563 to your computer and use it in GitHub Desktop.
#!/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