Revision a680ea0c
Von Moritz Bunkus vor mehr als 8 Jahren hinzugefügt
SL/Dispatcher.pm | ||
---|---|---|
22 | 22 |
use DateTime; |
23 | 23 |
use Encode; |
24 | 24 |
use English qw(-no_match_vars); |
25 |
use FCGI; |
|
25 | 26 |
use File::Basename; |
27 |
use IO::File; |
|
26 | 28 |
use List::MoreUtils qw(all); |
27 | 29 |
use List::Util qw(first); |
28 | 30 |
use SL::ArchiveZipFixes; |
... | ... | |
221 | 223 |
"SL::Controller::$_[0]"->new->_run_action($_[1]); |
222 | 224 |
} |
223 | 225 |
|
226 |
sub handle_all_requests { |
|
227 |
my ($self) = @_; |
|
228 |
|
|
229 |
my $request = FCGI::Request(); |
|
230 |
while ($request->Accept() >= 0) { |
|
231 |
$self->handle_request($request); |
|
232 |
if (_memory_usage_is_too_high()) { |
|
233 |
$request->Flush(); |
|
234 |
last; |
|
235 |
} |
|
236 |
} |
|
237 |
} |
|
238 |
|
|
224 | 239 |
sub handle_request { |
225 | 240 |
my $self = shift; |
226 | 241 |
$self->{request} = shift; |
... | ... | |
481 | 496 |
::end_of_request(); |
482 | 497 |
} |
483 | 498 |
|
499 |
sub _parse_number_with_unit { |
|
500 |
my ($number) = @_; |
|
501 |
|
|
502 |
return undef unless defined $number; |
|
503 |
return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi; |
|
504 |
|
|
505 |
my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024); |
|
506 |
|
|
507 |
return $1 * $factors{uc $2}; |
|
508 |
} |
|
509 |
|
|
510 |
sub _memory_usage_is_too_high { |
|
511 |
return undef unless $::lx_office_conf{system}; |
|
512 |
|
|
513 |
my %limits = ( |
|
514 |
rss => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}), |
|
515 |
size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}), |
|
516 |
); |
|
517 |
|
|
518 |
# $::lxdebug->dump(0, "limits", \%limits); |
|
519 |
|
|
520 |
return undef unless $limits{rss} || $limits{vsz}; |
|
521 |
|
|
522 |
my %usage; |
|
523 |
|
|
524 |
my $in = IO::File->new("/proc/$$/status", "r") or return undef; |
|
525 |
|
|
526 |
while (<$in>) { |
|
527 |
chomp; |
|
528 |
$usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix; |
|
529 |
} |
|
530 |
|
|
531 |
$in->close; |
|
532 |
|
|
533 |
# $::lxdebug->dump(0, "usage", \%usage); |
|
534 |
|
|
535 |
foreach my $type (keys %limits) { |
|
536 |
next if !$limits{$type}; |
|
537 |
next if $limits{$type} >= ($usage{$type} // 0); |
|
538 |
|
|
539 |
$::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes"); |
|
540 |
|
|
541 |
return 1; |
|
542 |
} |
|
543 |
|
|
544 |
return 0; |
|
545 |
} |
|
546 |
|
|
484 | 547 |
package main; |
485 | 548 |
|
486 | 549 |
use strict; |
Auch abrufbar als: Unified diff
Dispatcher: Requstloop vom dispatcher.fpl nach Dispatcher.pm verschoben
Projekt »keep your main namespace clean«.