Permalink
Browse files

* Initial commit for 0.01.

  • Loading branch information...
0 parents commit 0f1a6252fd2ffdc0d679d6ee8d9509be9bb2666d @audreyt committed Jun 13, 2011
Showing with 286 additions and 0 deletions.
  1. +3 −0 Changes
  2. +8 −0 Makefile.PL
  3. +59 −0 README
  4. +69 −0 README.mkdn
  5. +120 −0 lib/Plack/Middleware/SizeLimit.pm
  6. +27 −0 t/0-signature.t
3 Changes
@@ -0,0 +1,3 @@
+[Changes for 0.01 - 2011年 6月13日 周一 21時20分16秒 CST]
+
+* Initial release to CPAN.
8 Makefile.PL
@@ -0,0 +1,8 @@
+#!/usr/bin/env perl
+use inc::Module::Package 'Au:dry';
+__END__
+
+# Deps list generated by:
+author_requires 'Module::Package' => '0.24';
+
+author_requires 'Module::Package::Au' => '0.01';
59 README
@@ -0,0 +1,59 @@
+NAME
+ Plack::Middleware::SizeLimit - Terminate processes if they grow too
+ large
+
+SYNOPSIS
+ use Plack::Builder;
+
+ builder {
+ enable "Plack::Middleware::SizeLimit",
+ max_unshared_size_in_kb => '4096', # 4MB
+ # min_shared_size_in_kb => '8192', # 8MB
+ # max_process_size_in_kb => '16384', # 16MB
+ check_every_n_requests => 2;
+ $app;
+ };
+
+DESCRIPTION
+ This middleware is a port of the excellent Apache::SizeLimit module for
+ multi-process Plack servers, such as Starman and Starlet.
+
+CONFIGURATIONS
+ max_unshared_size_in_kb
+ The maximum amount of *unshared* memory the process can use; usually
+ this option is all one needs.
+
+ Experience on one heavily trafficked mod_perl site showed that
+ setting this option and leaving the others unset is the most
+ effective policy.
+
+ This is because it only kills off processes that are truly using too
+ much physical RAM, allowing most processes to live longer and
+ reducing the process churn rate.
+
+ min_shared_size_in_kb
+ Sets the minimum amount of shared memory the process must have.
+
+ max_process_size_in_kb
+ The maximum size of the process, including both shared and unshared
+ memory.
+
+ check_every_n_requests
+ Since checking the process size can take a few system calls on some
+ platforms (e.g. linux), you may specify this option to check the
+ process size every N requests.
+
+SEE ALSO
+ Starman, Starlet
+
+AUTHORS
+ 唐鳳 <cpan@audreyt.org>
+
+CC0 1.0 Universal
+ To the extent possible under law, 唐鳳 has waived all copyright and
+ related or neighboring rights to App-Uni.
+
+ This work is published from Taiwan.
+
+ <http://creativecommons.org/publicdomain/zero/1.0>
+
69 README.mkdn
@@ -0,0 +1,69 @@
+# NAME
+
+Plack::Middleware::SizeLimit - Terminate processes if they grow too large
+
+# SYNOPSIS
+
+ use Plack::Builder;
+
+ builder {
+ enable "Plack::Middleware::SizeLimit",
+ max_unshared_size_in_kb => '4096', # 4MB
+ # min_shared_size_in_kb => '8192', # 8MB
+ # max_process_size_in_kb => '16384', # 16MB
+ check_every_n_requests => 2;
+ $app;
+ };
+
+
+
+# DESCRIPTION
+
+This middleware is a port of the excellent [Apache::SizeLimit](http://search.cpan.org/perldoc?Apache::SizeLimit) module
+for multi-process Plack servers, such as [Starman](http://search.cpan.org/perldoc?Starman) and [Starlet](http://search.cpan.org/perldoc?Starlet).
+
+# CONFIGURATIONS
+
+- max_unshared_size_in_kb
+
+The maximum amount of _unshared_ memory the process can use;
+usually this option is all one needs.
+
+Experience on one heavily trafficked [mod_perl](http://search.cpan.org/perldoc?mod_perl) site showed that
+setting this option and leaving the others unset is the most effective
+policy.
+
+This is because it only kills off processes that are truly using too much
+physical RAM, allowing most processes to live longer and reducing the
+process churn rate.
+
+- min_shared_size_in_kb
+
+Sets the minimum amount of shared memory the process must have.
+
+- max_process_size_in_kb
+
+The maximum size of the process, including both shared and unshared memory.
+
+- check_every_n_requests
+
+Since checking the process size can take a few system calls on some
+platforms (e.g. linux), you may specify this option to check the process
+size every N requests.
+
+# SEE ALSO
+
+[Starman](http://search.cpan.org/perldoc?Starman), [Starlet](http://search.cpan.org/perldoc?Starlet)
+
+# AUTHORS
+
+唐鳳 <cpan@audreyt.org>
+
+# CC0 1.0 Universal
+
+To the extent possible under law, 唐鳳 has waived all copyright and related
+or neighboring rights to App-Uni.
+
+This work is published from Taiwan.
+
+[http://creativecommons.org/publicdomain/zero/1.0](http://creativecommons.org/publicdomain/zero/1.0)
120 lib/Plack/Middleware/SizeLimit.pm
@@ -0,0 +1,120 @@
+package Plack::Middleware::SizeLimit;
+
+use strict;
+use warnings;
+use 5.008;
+use parent qw(
+ Plack::Middleware
+ Process::SizeLimit::Core
+);
+use Plack::Util::Accessor qw(
+ max_unshared_size_in_kb
+ min_shared_size_in_kb
+ max_process_size_in_kb
+ check_every_n_requests
+);
+
+our $VERSION = '0.01';
+
+sub prepare_app {
+ my $self = shift;
+ $self->set_check_interval($self->check_every_n_requests || 1);
+ $self->set_max_process_size($self->max_process_size_in_kb);
+ $self->set_min_shared_size($self->min_shared_size_in_kb);
+ $self->set_max_unshared_size($self->max_unshared_size_in_kb);
+}
+
+sub call {
+ my ($self, $env) = @_;
+
+ my $res = $self->app->($env);
+
+ if (my $interval = $self->check_every_n_requests) {
+ my $pinc = $self->get_and_pinc_request_count;
+ return $res if ($pinc % $interval);
+ }
+
+ if ($self->_limits_are_exceeded) {
+ $env->{'psgix.harakiri'} = 1;
+ }
+
+ return $res;
+}
+
+1;
+__END__
+
+=head1 NAME
+
+Plack::Middleware::SizeLimit - Terminate processes if they grow too large
+
+=head1 SYNOPSIS
+
+ use Plack::Builder;
+
+ builder {
+ enable "Plack::Middleware::SizeLimit",
+ max_unshared_size_in_kb => '4096', # 4MB
+ # min_shared_size_in_kb => '8192', # 8MB
+ # max_process_size_in_kb => '16384', # 16MB
+ check_every_n_requests => 2;
+ $app;
+ };
+
+
+=head1 DESCRIPTION
+
+This middleware is a port of the excellent L<Apache::SizeLimit> module
+for multi-process Plack servers, such as L<Starman> and L<Starlet>.
+
+=head1 CONFIGURATIONS
+
+=over 4
+
+=item max_unshared_size_in_kb
+
+The maximum amount of I<unshared> memory the process can use;
+usually this option is all one needs.
+
+Experience on one heavily trafficked L<mod_perl> site showed that
+setting this option and leaving the others unset is the most effective
+policy.
+
+This is because it only kills off processes that are truly using too much
+physical RAM, allowing most processes to live longer and reducing the
+process churn rate.
+
+=item min_shared_size_in_kb
+
+Sets the minimum amount of shared memory the process must have.
+
+=item max_process_size_in_kb
+
+The maximum size of the process, including both shared and unshared memory.
+
+=item check_every_n_requests
+
+Since checking the process size can take a few system calls on some
+platforms (e.g. linux), you may specify this option to check the process
+size every N requests.
+
+=back
+
+=head1 SEE ALSO
+
+L<Starman>, L<Starlet>
+
+=head1 AUTHORS
+
+唐鳳 E<lt>cpan@audreyt.orgE<gt>
+
+=head1 CC0 1.0 Universal
+
+To the extent possible under law, 唐鳳 has waived all copyright and related
+or neighboring rights to App-Uni.
+
+This work is published from Taiwan.
+
+L<http://creativecommons.org/publicdomain/zero/1.0>
+
+=cut
27 t/0-signature.t
@@ -0,0 +1,27 @@
+#!/usr/bin/perl
+
+use strict;
+print "1..1\n";
+
+if (!$ENV{TEST_SIGNATURE}) {
+ print "ok 1 # skip set the environment variable TEST_SIGNATURE to enable this test\n";
+}
+elsif (!-s 'SIGNATURE') {
+ print "ok 1 # skip No signature file found\n";
+}
+elsif (!eval { require Module::Signature; 1 }) {
+ print "ok 1 # skip ",
+ "Next time around, consider install Module::Signature, ",
+ "so you can verify the integrity of this distribution.\n";
+}
+elsif (!eval { require Socket; Socket::inet_aton('pgp.mit.edu') }) {
+ print "ok 1 # skip ",
+ "Cannot connect to the keyserver\n";
+}
+else {
+ (Module::Signature::verify() == Module::Signature::SIGNATURE_OK())
+ or print "not ";
+ print "ok 1 # Valid signature\n";
+}
+
+__END__

0 comments on commit 0f1a625

Please sign in to comment.