1Mojo::Server::Prefork(3U)ser Contributed Perl DocumentatiMoonjo::Server::Prefork(3)
2
3
4
6 Mojo::Server::Prefork - Pre-forking non-blocking I/O HTTP and WebSocket
7 server
8
10 use Mojo::Server::Prefork;
11
12 my $prefork = Mojo::Server::Prefork->new(listen => ['http://*:8080']);
13 $prefork->unsubscribe('request')->on(request => sub {
14 my ($prefork, $tx) = @_;
15
16 # Request
17 my $method = $tx->req->method;
18 my $path = $tx->req->url->path;
19
20 # Response
21 $tx->res->code(200);
22 $tx->res->headers->content_type('text/plain');
23 $tx->res->body("$method request for $path!");
24
25 # Resume transaction
26 $tx->resume;
27 });
28 $prefork->run;
29
31 Mojo::Server::Prefork is a full featured, UNIX optimized, pre-forking
32 non-blocking I/O HTTP and WebSocket server, built around the very well
33 tested and reliable Mojo::Server::Daemon, with IPv6, TLS, SNI, UNIX
34 domain socket, Comet (long polling), keep-alive and multiple event loop
35 support. Note that the server uses signals for process management, so
36 you should avoid modifying signal handlers in your applications.
37
38 For better scalability (epoll, kqueue) and to provide non-blocking name
39 resolution, SOCKS5 as well as TLS support, the optional modules EV
40 (4.0+), Net::DNS::Native (0.15+), IO::Socket::Socks (0.64+) and
41 IO::Socket::SSL (1.84+) will be used automatically if possible.
42 Individual features can also be disabled with the "MOJO_NO_NNR",
43 "MOJO_NO_SOCKS" and "MOJO_NO_TLS" environment variables.
44
45 See "DEPLOYMENT" in Mojolicious::Guides::Cookbook for more.
46
48 The Mojo::Server::Prefork manager process can be controlled at runtime
49 with the following signals.
50
51 INT, TERM
52 Shut down server immediately.
53
54 QUIT
55 Shut down server gracefully.
56
57 TTIN
58 Increase worker pool by one.
59
60 TTOU
61 Decrease worker pool by one.
62
64 Mojo::Server::Prefork worker processes can be controlled at runtime
65 with the following signals.
66
67 QUIT
68 Stop worker gracefully.
69
71 Mojo::Server::Prefork inherits all events from Mojo::Server::Daemon and
72 can emit the following new ones.
73
74 finish
75 $prefork->on(finish => sub {
76 my ($prefork, $graceful) = @_;
77 ...
78 });
79
80 Emitted when the server shuts down.
81
82 $prefork->on(finish => sub {
83 my ($prefork, $graceful) = @_;
84 say $graceful ? 'Graceful server shutdown' : 'Server shutdown';
85 });
86
87 heartbeat
88 $prefork->on(heartbeat => sub {
89 my ($prefork, $pid) = @_;
90 ...
91 });
92
93 Emitted when a heartbeat message has been received from a worker.
94
95 $prefork->on(heartbeat => sub {
96 my ($prefork, $pid) = @_;
97 say "Worker $pid has a heartbeat";
98 });
99
100 reap
101 $prefork->on(reap => sub {
102 my ($prefork, $pid) = @_;
103 ...
104 });
105
106 Emitted when a child process exited.
107
108 $prefork->on(reap => sub {
109 my ($prefork, $pid) = @_;
110 say "Worker $pid stopped";
111 });
112
113 spawn
114 $prefork->on(spawn => sub {
115 my ($prefork, $pid) = @_;
116 ...
117 });
118
119 Emitted when a worker process is spawned.
120
121 $prefork->on(spawn => sub {
122 my ($prefork, $pid) = @_;
123 say "Worker $pid started";
124 });
125
126 wait
127 $prefork->on(wait => sub {
128 my $prefork = shift;
129 ...
130 });
131
132 Emitted when the manager starts waiting for new heartbeat messages.
133
134 $prefork->on(wait => sub {
135 my $prefork = shift;
136 my $workers = $prefork->workers;
137 say "Waiting for heartbeat messages from $workers workers";
138 });
139
141 Mojo::Server::Prefork inherits all attributes from Mojo::Server::Daemon
142 and implements the following new ones.
143
144 accepts
145 my $accepts = $prefork->accepts;
146 $prefork = $prefork->accepts(100);
147
148 Maximum number of connections a worker is allowed to accept, before
149 stopping gracefully and then getting replaced with a newly started
150 worker, passed along to "max_accepts" in Mojo::IOLoop, defaults to
151 10000. Setting the value to 0 will allow workers to accept new
152 connections indefinitely. Note that up to half of this value can be
153 subtracted randomly to improve load balancing, and to make sure that
154 not all workers restart at the same time.
155
156 cleanup
157 my $bool = $prefork->cleanup;
158 $prefork = $prefork->cleanup($bool);
159
160 Delete "pid_file" automatically once it is not needed anymore, defaults
161 to a true value.
162
163 graceful_timeout
164 my $timeout = $prefork->graceful_timeout;
165 $prefork = $prefork->graceful_timeout(15);
166
167 Maximum amount of time in seconds stopping a worker gracefully may take
168 before being forced, defaults to 120. Note that this value should
169 usually be a little larger than the maximum amount of time you expect
170 any one request to take.
171
172 heartbeat_interval
173 my $interval = $prefork->heartbeat_interval;
174 $prefork = $prefork->heartbeat_interval(3);
175
176 Heartbeat interval in seconds, defaults to 5.
177
178 heartbeat_timeout
179 my $timeout = $prefork->heartbeat_timeout;
180 $prefork = $prefork->heartbeat_timeout(2);
181
182 Maximum amount of time in seconds before a worker without a heartbeat
183 will be stopped gracefully, defaults to 30. Note that this value should
184 usually be a little larger than the maximum amount of time you expect
185 any one operation to block the event loop.
186
187 pid_file
188 my $file = $prefork->pid_file;
189 $prefork = $prefork->pid_file('/tmp/prefork.pid');
190
191 Full path of process id file, defaults to "prefork.pid" in a temporary
192 directory.
193
194 spare
195 my $spare = $prefork->spare;
196 $prefork = $prefork->spare(4);
197
198 Temporarily spawn up to this number of additional workers if there is a
199 need, defaults to 2. This allows for new workers to be started while
200 old ones are still shutting down gracefully, drastically reducing the
201 performance cost of worker restarts.
202
203 workers
204 my $workers = $prefork->workers;
205 $prefork = $prefork->workers(10);
206
207 Number of worker processes, defaults to 4. A good rule of thumb is two
208 worker processes per CPU core for applications that perform mostly non-
209 blocking operations, blocking operations often require more and benefit
210 from decreasing concurrency with "clients" in Mojo::Server::Daemon
211 (often as low as 1).
212
214 Mojo::Server::Prefork inherits all methods from Mojo::Server::Daemon
215 and implements the following new ones.
216
217 check_pid
218 my $pid = $prefork->check_pid;
219
220 Get process id for running server from "pid_file" or delete it if
221 server is not running.
222
223 say 'Server is not running' unless $prefork->check_pid;
224
225 ensure_pid_file
226 $prefork->ensure_pid_file($pid);
227
228 Ensure "pid_file" exists.
229
230 healthy
231 my $healthy = $prefork->healthy;
232
233 Number of currently active worker processes with a heartbeat.
234
235 run
236 $prefork->run;
237
238 Run server and wait for "MANAGER SIGNALS".
239
241 Mojolicious, Mojolicious::Guides, <https://mojolicious.org>.
242
243
244
245perl v5.30.1 2020-01-30 Mojo::Server::Prefork(3)