Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision a680ea0c

Von Moritz Bunkus vor mehr als 8 Jahren hinzugefügt

  • ID a680ea0c9d138dc94575c9fa94214d01ef7cd1a6
  • Vorgänger 9a2e28ce
  • Nachfolger 24f6b9ed

Dispatcher: Requstloop vom dispatcher.fpl nach Dispatcher.pm verschoben

Projekt »keep your main namespace clean«.

Unterschiede anzeigen:

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;
dispatcher.fpl
2 2

  
3 3
use strict;
4 4

  
5
use FCGI;
6
use IO::File;
7 5
use SL::Dispatcher;
8 6
use SL::FCGIFixes;
9 7
use SL::LXDebug;
10 8

  
11
sub _parse_number_with_unit {
12
  my ($number) = @_;
13

  
14
  return undef   unless defined $number;
15
  return $number unless $number =~ m{^ \s* (\d+) \s* ([kmg])b \s* $}xi;
16

  
17
  my %factors = (K => 1024, M => 1024 * 1024, G => 1024 * 1024 * 1024);
18

  
19
  return $1 * $factors{uc $2};
20
}
21

  
22
sub _memory_usage_is_too_high {
23
  return undef unless $::lx_office_conf{system};
24

  
25
  my %limits = (
26
    rss  => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_rss}),
27
    size => _parse_number_with_unit($::lx_office_conf{system}->{memory_limit_vsz}),
28
  );
29

  
30
  # $::lxdebug->dump(0, "limits", \%limits);
31

  
32
  return undef unless $limits{rss} || $limits{vsz};
33

  
34
  my %usage;
35

  
36
  my $in = IO::File->new("/proc/$$/status", "r") or return undef;
37

  
38
  while (<$in>) {
39
    chomp;
40
    $usage{lc $1} = _parse_number_with_unit($2) if m{^ vm(rss|size): \s* (\d+ \s* [kmg]b) \s* $}ix;
41
  }
42

  
43
  $in->close;
44

  
45
  # $::lxdebug->dump(0, "usage", \%usage);
46

  
47
  foreach my $type (keys %limits) {
48
    next if !$limits{$type};
49
    next if $limits{$type} >= ($usage{$type} // 0);
50

  
51
    $::lxdebug->message(LXDebug::WARN(), "Exiting due to memory size limit reached for type '${type}': limit " . $limits{$type} . " bytes, usage " . $usage{$type} . " bytes");
52

  
53
    return 1;
54
  }
55

  
56
  return 0;
57
}
58

  
59 9
our $dispatcher = SL::Dispatcher->new('FastCGI');
60 10
$dispatcher->pre_startup_setup;
61 11
SL::FCGIFixes::apply_fixes();
62 12
$dispatcher->pre_startup_checks;
63

  
64
my $request = FCGI::Request();
65
while ($request->Accept() >= 0) {
66
  $dispatcher->handle_request($request);
67
  if (_memory_usage_is_too_high()) {
68
    $request->Flush();
69
    last;
70
  }
71
}
13
$dispatcher->handle_all_requests;
72 14

  
73 15
1;

Auch abrufbar als: Unified diff