Skip to content

Instantly share code, notes, and snippets.

@mythosil
Created July 22, 2011 16:57
Show Gist options
  • Save mythosil/1099857 to your computer and use it in GitHub Desktop.
Save mythosil/1099857 to your computer and use it in GitHub Desktop.
prefork echo server
use strict;
use warnings;
use IO::Socket::INET;
use Parallel::Prefork;
sub MaxRequestsPerChild { 100 };
my $listen = IO::Socket::INET->new(
Listen => 5,
LocalHost => '0.0.0.0',
LocalPort => '1986',
Proto => 'tcp',
) or die $!;
my $pm = Parallel::Prefork->new({
max_workers => 5,
trap_signals => {
TERM => 'TERM',
HUP => 'TERM',
},
});
while ($pm->signal_received ne 'TERM') {
$pm->start and next;
my $reqs_remaining = MaxRequestsPerChild;
$SIG{TERM} = sub {
$reqs_remaining = 0;
};
while ($reqs_remaining-- > 0) {
my $s = $listen->accept;
while (<$s>) {
print $s $_; # echo back
}
close($s);
}
close($listen);
$pm->finish;
}
$pm->wait_all_children;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment