Net::Se...PreFork(3)
π Net::Se...PreFork(3) π
okay bestie, welcome to the User Contributed Perl Documentation page for Net::Server::PreFork β the PreFork personality. itβs giving βread the parent pods first, iβm seriousβ energy, and honestly? iconic. π
π NAME π
Net::Server::PreFork β a Net::Server personality. periodt. π
β¨ SYNOPSIS β¨
bestie, the whole shape is literally this:
use base qw(Net::Server::PreFork);
sub process_request {
#...code...
}
__PACKAGE__->run();
π¦ DESCRIPTION π¦
okay so first things first: please read the pod on Net::Server and Net::Server::PreForkSimple first. no skipping, bestie. π
This module is a personality, or extension, or sub class, of the Net::Server::PreForkSimple class which is a sub class of Net::Server. See Net::Server::PreForkSimple.
This personality binds to one or more ports and then forks min_servers child process. The server will make sure that at any given time there are min_spare_servers available to receive a client request, up to max_servers. Each of these children will process up to max_requests client connections. This type is good for a heavily hit site, and should scale well for most applications. (Multi port accept is accomplished using flock to serialize the children).
π at this time, it does not appear that this module will pass tests on Win32 systems. Any ideas or patches for making the tests pass would be welcome. thatβs the tea.
π SAMPLE CODE π
Please see the sample listed in Net::Server. thatβs it, thatβs the section. π«Ά
π COMMAND LINE ARGUMENTS π
In addition to the command line arguments of the Net::Server base class and the Net::Server::PreForkSimple parent class, Net::Server::PreFork contains several other configurable parameters. You really should also see Net::Server::PreForkSimple.
hereβs the Key / Value / Default lineup, preserved exactly because we do not play with facts:
Key Value Default
min_servers \d+ 5
min_spare_servers \d+ 2
max_spare_servers \d+ 10
max_servers \d+ 50
max_requests \d+ 1000
serialize (flock|semaphore
|pipe|none) undef
# serialize defaults to flock on multi_port or on Solaris
lock_file "filename" File::Temp->new
check_for_dead \d+ 30
check_for_waiting \d+ 10
max_dequeue \d+ undef
check_for_dequeue \d+ undef
child_communication 1 undef
the option tea β
min_serversβ value\d+, default5. The minimum number of servers to keep running.min_spare_serversβ value\d+, default2. The minimum number of servers to have waiting for requests. Minimum and maximum numbers should not be set too close to each other or the server will fork and kill children too often. π¬max_spare_serversβ value\d+, default10. The maximum number of servers to have waiting for requests. Seemin_spare_servers.max_serversβ value\d+, default50. The maximum number of child servers to start. This does not apply to dequeue processes.max_requestsβ value\d+, default1000.serializeβ value(flock|semaphore|pipe|none), defaultundef;# serialize defaults to flock on multi_port or on Solaris. See the documentation underNet::Server::PreForkSimple.lock_fileβ value"filename", defaultFile::Temp->new.check_for_deadβ value\d+, default30.check_for_waitingβ value\d+, default10. Seconds to wait before checking to see if we can kill off some waiting servers.check_for_spawnβ Seconds between checking to see if we need to spawn more children.min_child_ttlβ Minimum number of seconds between starting children and killing a child process.max_dequeueβ value\d+, defaultundef.check_for_dequeueβ value\d+, defaultundef.child_communicationβ value1, defaultundef. Enable child communication to parent via unix sockets. If set to true, will let children write to the socket contained in$self->{'server'}->{'parent_sock'}. The parent will be notified throughchild_is_talking_hookwhere the first argument is the socket to the child. The child's socket is stored in$self->{'server'}->{'children'}->{$child_pid}->{'sock'}.
π§ CONFIGURATION FILE π§
Net::Server::PreFork allows for the use of a configuration file to read in server parameters. The format of this conf file is simple key value pairs. Comments and white space are ignored.
hereβs the whole example, copy-paste energy, no edits:
#-------------- file test.conf --------------
### server information
min_servers 20
max_servers 80
min_spare_servers 10
min_spare_servers 15
max_requests 1000
### user and group to become
user somebody
group everybody
### logging ?
log_file /var/log/server.log
log_level 3
pid_file /tmp/server.pid
### access control
allow .+\.(net|com)
allow domain\.com
deny a.+
### background the process?
background 1
### ports to bind
host 127.0.0.1
port localhost:20204
port 20205
### reverse lookups ?
# reverse_lookups on
### enable child communication ?
# child_communication
#-------------- file test.conf --------------
π₯ PROCESS FLOW π₯
Process flow follows Net::Server until the loop phase. At this point min_servers are forked and wait for connections. When a child accepts a connection, finishes processing a client, or exits, it relays that information to the parent, which keeps track and makes sure there are enough children to fulfill min_servers, min_spare_servers, max_spare_servers, and max_servers.
πͺ HOOKS πͺ
The PreFork server has the following hooks in addition to the hooks provided by PreForkSimple. See Net::Server::PreForkSimple.
$self->run_n_children_hook()β This hook occurs at the top ofrun_n_childrenwhich is called each time the server goes to start more child processes. This gives the parent a chance to do a little of its own accounting (as desired). Idea for this hook came from James FitzGibbon.$self->parent_read_hook()β This hook occurs any time that the parent reads information from the child. The line from the child is sent as an argument.$self->child_is_talking_hook()β This hook occurs ifchild_communicationis true and the child has written to$self->{'server'}->{'parent_sock'}. The first argument will be the open socket to the child.$self->idle_loop_hook()β This hook is called in every pass through the main process wait loop, everycheck_for_waitingseconds. The first argument is a reference to an array of file descriptors that can be read at the moment.$self->cleanup_dead_child_hook( $child )β This hook is called when a dead child is detected. A child is considered dead when the pid does no longer exist. This hook could be used to cleanup possible temporary files or locks left over by a dead child.
π HOT DEPLOY π
Since version 2.000, the PreFork server has accepted the TTIN and TTOU signals. When a TTIN is received, the min and max_servers are increased by 1. If a TTOU signal is received the min max_servers are decreased by 1. This allows for adjusting the number of handling processes without having to restart the server.
π BUGS π
Tests don't seem to work on Win32. Any ideas or patches would be welcome. no cap, thatβs the whole bug section. π
π TO DO π
See Net::Server.
πΈ AUTHOR πΈ
Paul T. Seamons paul@seamons.com
π«Ά THANKS π«Ά
See Net::Server.
π SEE ALSO π
Please see also:
Net::Server::ForkNet::Server::INETNet::Server::PreForkSimpleNet::Server::MultiTypeNet::Server::SingleNet::Server::SIGNet::Server::DaemonizeNet::Server::Proto
π sign-off: this page is fromperl v5.42.0, dated2023-03-15, and itβsNet::Se...PreFork(3). byeeee, go fork responsibly π β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Net-Server 2.014-bp160.1.12 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms