]> git.sesse.net Git - pr0n/blob - perl/Sesse/pr0n/Overload.pm
Add a new 2304x1728 resolution (twice 1152x864), for fitting better into 30-inch...
[pr0n] / perl / Sesse / pr0n / Overload.pm
1 # Note: This package is shared between server processes as much as we can,
2 #       for obvious reasons (you don't want just half the server to go in
3 #       overload mode if you can help it)
4
5 package Sesse::pr0n::Overload;
6 use strict;
7 use warnings;
8
9 BEGIN {
10         use Exporter ();
11         our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
12
13         $VERSION     = 1.00;
14         @ISA         = qw(Exporter);
15         @EXPORT      = qw();
16         %EXPORT_TAGS = qw();
17         @EXPORT_OK   = qw();
18 }
19 our ($last_update, $loadavg, $in_overload);
20
21 sub is_in_overload {
22         my $r = shift;
23
24         # Manually set overload mode
25         if (lc($r->dir_config('OverloadMode')) eq 'on') {
26                 return 1;
27         }
28
29         # By default we are not in overload mode
30         if (!defined($in_overload)) {
31                 $in_overload = 0;
32         }
33
34         my $enable_threshold = $r->dir_config('OverloadEnableThreshold') || 10.0;
35         my $disable_threshold = $r->dir_config('OverloadDisableThreshold') || 5.0;
36         
37         # Check if our load average estimate is more than a minute old
38         if (!defined($last_update) || (time - $last_update) > 60) {
39                 open LOADAVG, "</proc/loadavg"
40                         or die "/proc/loadavg: $!";
41                 my $line = <LOADAVG>;
42                 close LOADAVG;
43                 
44                 $line =~ /^(\d+\.\d+) / or die "Couldn't parse /proc/loadavg";
45                 
46                 $loadavg = $1;
47                 $last_update = time;
48
49                 if ($in_overload) {
50                         if ($loadavg < $disable_threshold) {
51                                 $r->log->info("Current load average is $loadavg (threshold: $disable_threshold), leaving overload mode");
52                                 $in_overload = 0;
53                         } else {
54                                 $r->log->warn("Current load average is $loadavg (threshold: $disable_threshold), staying in overload mode");
55                         }
56                 } else {
57                         if ($loadavg > $enable_threshold) {
58                                 $r->log->warn("Current load average is $loadavg (threshold: $enable_threshold), entering overload mode");
59                                 $in_overload = 1;
60                         } else {
61                                 $r->log->info("Current load average is $loadavg (threshold: $enable_threshold)");
62                         }
63                 }
64         }
65
66         return $in_overload;
67 }
68
69 1;
70