NAME

Net::DynamicDaemon - library for multi-process daemons

SYNOPSIS

use Net::DynamicDaemon;

Net::DynamicDaemon->run({
    -pidfile    => 'daemon.pid',
    -port       => 9900,
    -on_request => sub {
        my $fh = shift;

        my $line = <$fh>;
        print {$fh} $line;
    },
    -on_idle => sub {
        print "idle\n";
    },
});

DESCRIPTION

Net::DynamicDaemon is a library for implementing multi-process server applications in a very simple way. The number of processes is automatically adjusted, just like FastCGI.

run

Net::DynamicDaemon->run({
    -pidfile       => '/tmp/foo.pid', # optional
    -port          => 80,             # REQUIRED
    -min_procs     => 2,              # optional; defaults to 1
    -max_procs     => 10,             # optional; defaults to 100
    -on_request    => sub {           # REQUIRED
                          my $socket = shift;
                          ...
                      },
    -max_requests  => 100,            # optional; defaults to 100
    -no_fork       => 0,              # optional; defaults to 0
    -on_idle       => sub { }         # optional
    -idle_interval => 5,              # optional; defaults to 1
  });

Open and listen to a TCP socket until the process receives SIGQUIT, SIGTERM, SIGHUP or SIGINT.

-pidfile

If this option is present, a PID file will be created at the given location. The PID file will be automatically deleted when the daemon quits.

-port

The TCP port to which the daemon listens.

-min_procs

Minimum number of worker processes, which accepts connections and processes requests. The number of worker processes decreases down to this value as the number of incoming requests falls. See -max_procs and -no_fork.

-max_procs

Maximum number of worker processes. The number of worker processes increases up to this value as the number of incoming requests raises. See -min_procs and -no_fork.

-on_request

The request handler subroutine which takes one argument as an accepted socket (IO::Handle). The return value of the subroutine is ignored. The connection will be automatically closed after the end of execution of handler.

-max_requests

Each of worker processes kills itself after processing the given number of requests, to minimise the fear of memory leaking. (You know, it is almost impossible to track the source of leaking memory in Perl. Do not waste your precious time.)

-no_fork

Run the daemon in no-fork mode. In this mode, the daemon spawns no child processes, and processes each requests one by one. Use this option only for debugging and profiling of your daemons. The options -min_procs, -max_procs and -max_requests are ignored with this option enabled.

-on_idle

A subroutine which takes no arguments. It will be called periodically with the given interval. See -idle_interval.

-idle_interval

The interval of calling -on_idle in seconds.

COPYRIGHT AND LICENSE

Copyright (C) 2007-2008 YMIRLINK Inc.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.