From a680ea0c9d138dc94575c9fa94214d01ef7cd1a6 Mon Sep 17 00:00:00 2001 From: Moritz Bunkus Date: Mon, 2 May 2016 14:26:15 +0200 Subject: [PATCH] Dispatcher: Requstloop vom dispatcher.fpl nach Dispatcher.pm verschoben MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Projekt »keep your main namespace clean«. --- SL/Dispatcher.pm | 63 ++++++++++++++++++++++++++++++++++++++++++++++++ dispatcher.fpl | 60 +-------------------------------------------- 2 files changed, 64 insertions(+), 59 deletions(-) diff --git a/SL/Dispatcher.pm b/SL/Dispatcher.pm index 38973216d..632202735 100644 --- a/SL/Dispatcher.pm +++ b/SL/Dispatcher.pm @@ -22,7 +22,9 @@ use Config::Std; use DateTime; use Encode; use English qw(-no_match_vars); +use FCGI; use File::Basename; +use IO::File; use List::MoreUtils qw(all); use List::Util qw(first); use SL::ArchiveZipFixes; @@ -221,6 +223,19 @@ sub _run_controller { "SL::Controller::$_[0]"->new->_run_action($_[1]); } +sub handle_all_requests { + my ($self) = @_; + + my $request = FCGI::Request(); + while ($request->Accept() >= 0) { + $self->handle_request($request); + if (_memory_usage_is_too_high()) { + $request->Flush(); + last; + } + } +} + sub handle_request { my $self = shift; $self->{request} = shift; @@ -481,6 +496,54 @@ sub _check_for_old_config_files { ::end_of_request(); } +sub _parse_number_with_unit { + my ($number) = @_; + + return undef unless defined $number; + return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi; + + my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024); + + return $1 * $factors{uc $2}; +} + +sub _memory_usage_is_too_high { + return undef unless $::lx_office_conf{system}; + + my %limits = ( + rss => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}), + size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}), + ); + + # $::lxdebug->dump(0, "limits", \%limits); + + return undef unless $limits{rss} || $limits{vsz}; + + my %usage; + + my $in = IO::File->new("/proc/$$/status", "r") or return undef; + + while (<$in>) { + chomp; + $usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix; + } + + $in->close; + + # $::lxdebug->dump(0, "usage", \%usage); + + foreach my $type (keys %limits) { + next if !$limits{$type}; + next if $limits{$type} >= ($usage{$type} // 0); + + $::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes"); + + return 1; + } + + return 0; +} + package main; use strict; diff --git a/dispatcher.fpl b/dispatcher.fpl index cd910cc8d..c83503612 100755 --- a/dispatcher.fpl +++ b/dispatcher.fpl @@ -2,72 +2,14 @@ use strict; -use FCGI; -use IO::File; use SL::Dispatcher; use SL::FCGIFixes; use SL::LXDebug; -sub _parse_number_with_unit { - my ($number) = @_; - - return undef unless defined $number; - return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi; - - my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024); - - return $1 * $factors{uc $2}; -} - -sub _memory_usage_is_too_high { - return undef unless $::lx_office_conf{system}; - - my %limits = ( - rss => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}), - size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}), - ); - - # $::lxdebug->dump(0, "limits", \%limits); - - return undef unless $limits{rss} || $limits{vsz}; - - my %usage; - - my $in = IO::File->new("/proc/$$/status", "r") or return undef; - - while (<$in>) { - chomp; - $usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix; - } - - $in->close; - - # $::lxdebug->dump(0, "usage", \%usage); - - foreach my $type (keys %limits) { - next if !$limits{$type}; - next if $limits{$type} >= ($usage{$type} // 0); - - $::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes"); - - return 1; - } - - return 0; -} - our $dispatcher = SL::Dispatcher->new('FastCGI'); $dispatcher->pre_startup_setup; SL::FCGIFixes::apply_fixes(); $dispatcher->pre_startup_checks; - -my $request = FCGI::Request(); -while ($request->Accept() >= 0) { - $dispatcher->handle_request($request); - if (_memory_usage_is_too_high()) { - $request->Flush(); - last; - } -} +$dispatcher->handle_all_requests; 1; -- 2.20.1