Skip to content

Instantly share code, notes, and snippets.

@macros
Created August 11, 2009 23:52
Show Gist options
  • Save macros/166194 to your computer and use it in GitHub Desktop.
Save macros/166194 to your computer and use it in GitHub Desktop.
init_workers();
while(1) {
if ($apache_load > 100 ) {
remove_worker();
} elsif ( $queue_size > 10000 ) {
add_worker();
}
sleep 5;
}
sub init_workers {
my $min_threads = $daemon->options->{'minthreads'};
for (1..$min_threads) {
add_worker();
}
}
sub add_worker {
my $curr_size = scalar(@threads);
my $max_threads = $daemon->options->{'maxthreads'};
if ($curr_size < $max_threads) {
print "Creating new thread\n";
push @threads, threads->new(\&do_work);
}
}
sub remove_worker {
my $curr_size = scalar(@threads);
my $min_threads = $daemon->options->{'minthreads'};
if ($curr_size > $min_threads ) {
print "Removing thread\n";
my $thread = unshift(@threads);
$thread->detach();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment