+use PublicInbox::MID qw(mid2path);
+use Devel::Peek qw(SvREFCNT);
+use PublicInbox::MIME;
+use POSIX qw(strftime);
+
+my $cleanup_timer;
+eval {
+ $cleanup_timer = 'disabled';
+ require PublicInbox::EvCleanup;
+ $cleanup_timer = undef; # OK if we get here
+};
+my $cleanup_broken = $@;
+
+my $CLEANUP = {}; # string(inbox) -> inbox
+sub cleanup_task () {
+ $cleanup_timer = undef;
+ my $next = {};
+ for my $ibx (values %$CLEANUP) {
+ my $again;
+ foreach my $f (qw(mm search)) {
+ delete $ibx->{$f} if SvREFCNT($ibx->{$f}) == 1;
+ }
+ my $expire = time - 60;
+ if (my $git = $ibx->{git}) {
+ $again = $git->cleanup($expire);
+ }
+ if (my $gits = $ibx->{-repo_objs}) {
+ foreach my $git (@$gits) {
+ $again = 1 if $git->cleanup($expire);
+ }
+ }
+ $again ||= !!($ibx->{mm} || $ibx->{search});
+ $next->{"$ibx"} = $ibx if $again;
+ }
+ $CLEANUP = $next;
+}
+
+sub _cleanup_later ($) {
+ my ($self) = @_;
+ return if $cleanup_broken;
+ return unless PublicInbox::EvCleanup::enabled();
+ $cleanup_timer ||= PublicInbox::EvCleanup::later(*cleanup_task);
+ $CLEANUP->{"$self"} = $self;
+}
+
+sub _set_uint ($$$) {
+ my ($opts, $field, $default) = @_;
+ my $val = $opts->{$field};
+ if (defined $val) {
+ $val = $val->[-1] if ref($val) eq 'ARRAY';
+ $val = undef if $val !~ /\A\d+\z/;
+ }
+ $opts->{$field} = $val || $default;
+}
+
+sub _set_limiter ($$$) {
+ my ($self, $pi_config, $pfx) = @_;
+ my $lkey = "-${pfx}_limiter";
+ $self->{$lkey} ||= eval {
+ # full key is: publicinbox.$NAME.httpbackendmax
+ my $mkey = $pfx.'max';
+ my $val = $self->{$mkey} or return;
+ my $lim;
+ if ($val =~ /\A\d+\z/) {
+ require PublicInbox::Qspawn;
+ $lim = PublicInbox::Qspawn::Limiter->new($val);
+ } elsif ($val =~ /\A[a-z][a-z0-9]*\z/) {
+ $lim = $pi_config->limiter($val);
+ warn "$mkey limiter=$val not found\n" if !$lim;
+ } else {
+ warn "$mkey limiter=$val not understood\n";
+ }
+ $lim;
+ }
+}