O'Reilly logo

Writing Apache Modules with Perl and C by Lincoln Stein, Doug MacEachern

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A.4. The Apache::Resource Class

Apache::Resource allows you to set resource limitations on the Apache server process. You can limit the amount of CPU time a child process will use, the amount of memory it allocates, the number of files it can have open simultaneously, the number of processes it can spawn, and other low-level resources. This allows you to limit the overall impact of a runaway process or to degrade processing gracefully if the server is overloaded by too many requests.

In order to use Apache::Resource, you must have the Perl BSD::Resource module installed. This module does not come installed in Perl by default. As the name implies, BSD::Resource will only compile on BSD-derived versions of Unix and a few close approximations. It will not run correctly on Win32 systems.

To use Apache::Resource, place the following declarations in one of your server configuration files:

PerlSetEnv  PERL_RLIMIT_DEFAULTS
PerlModule  Apache::Resource
PerlChildInitHandler Apache::Resource

This chooses reasonable defaults for the resource limits, loads the Apache::Resource module, and sets it to run whenever a child process is initialized.

You can further customize the module by using PerlSetEnv to declare more specific resource limits for CPU usage, memory, and so on. There are actually two limits you can set for each of these resources: a hard limit that can't be changed and a soft limit that can be increased by modules if they choose to do so (up to the hard limit). You can use a single ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required