about summary refs log tree commit homepage
path: root/lib/PublicInbox/Limiter.pm
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2023-10-25 00:29:24 +0000
committerEric Wong <e@80x24.org>2023-10-25 07:28:30 +0000
commitf81954fe591c6a6358ba528118874313e3920e83 (patch)
tree4ac3d3a12791d97a1e986cc80eb5cde9b3924834 /lib/PublicInbox/Limiter.pm
parent5061fda82a5df67f12b0b392bdad481cc07aa283 (diff)
downloadpublic-inbox-f81954fe591c6a6358ba528118874313e3920e83.tar.gz
It's slightly better organized this way, especially since
`publicinboxLimiter' has its own user-facing config section
and knobs.  I may use it in LeiMirror and CodeSearchIdx for
process management.
Diffstat (limited to 'lib/PublicInbox/Limiter.pm')
-rw-r--r--lib/PublicInbox/Limiter.pm47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/PublicInbox/Limiter.pm b/lib/PublicInbox/Limiter.pm
new file mode 100644
index 00000000..48a2b6a3
--- /dev/null
+++ b/lib/PublicInbox/Limiter.pm
@@ -0,0 +1,47 @@
+# Copyright (C) all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+package PublicInbox::Limiter;
+use v5.12;
+use PublicInbox::Spawn;
+
+sub new {
+        my ($class, $max) = @_;
+        bless {
+                # 32 is same as the git-daemon connection limit
+                max => $max || 32,
+                running => 0,
+                run_queue => [],
+                # RLIMIT_CPU => undef,
+                # RLIMIT_DATA => undef,
+                # RLIMIT_CORE => undef,
+        }, $class;
+}
+
+sub setup_rlimit {
+        my ($self, $name, $cfg) = @_;
+        for my $rlim (@PublicInbox::Spawn::RLIMITS) {
+                my $k = lc($rlim);
+                $k =~ tr/_//d;
+                $k = "publicinboxlimiter.$name.$k";
+                my $v = $cfg->{$k} // next;
+                my @rlimit = split(/\s*,\s*/, $v);
+                if (scalar(@rlimit) == 1) {
+                        push @rlimit, $rlimit[0];
+                } elsif (scalar(@rlimit) != 2) {
+                        warn "could not parse $k: $v\n";
+                }
+                eval { require BSD::Resource };
+                if ($@) {
+                        warn "BSD::Resource missing for $rlim";
+                        next;
+                }
+                for my $i (0..$#rlimit) {
+                        next if $rlimit[$i] ne 'INFINITY';
+                        $rlimit[$i] = BSD::Resource::RLIM_INFINITY();
+                }
+                $self->{$rlim} = \@rlimit;
+        }
+}
+
+1;