about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/PublicInbox/Import.pm3
-rw-r--r--lib/PublicInbox/V2Writable.pm3
-rw-r--r--lib/PublicInbox/Watch.pm31
3 files changed, 31 insertions, 6 deletions
diff --git a/lib/PublicInbox/Import.pm b/lib/PublicInbox/Import.pm
index 700b4026..ee5ca2ea 100644
--- a/lib/PublicInbox/Import.pm
+++ b/lib/PublicInbox/Import.pm
@@ -461,6 +461,9 @@ sub init_bare {
         }
 }
 
+# true if locked and active
+sub active { !!$_[0]->{out} }
+
 sub done {
         my ($self) = @_;
         my $w = delete $self->{out} or return;
diff --git a/lib/PublicInbox/V2Writable.pm b/lib/PublicInbox/V2Writable.pm
index f2288904..553dd839 100644
--- a/lib/PublicInbox/V2Writable.pm
+++ b/lib/PublicInbox/V2Writable.pm
@@ -655,6 +655,9 @@ sub checkpoint ($;$) {
 # public
 sub barrier { checkpoint($_[0], 1) };
 
+# true if locked and active
+sub active { !!$_[0]->{im} }
+
 # public
 sub done {
         my ($self) = @_;
diff --git a/lib/PublicInbox/Watch.pm b/lib/PublicInbox/Watch.pm
index 5f786139..0bb92d0a 100644
--- a/lib/PublicInbox/Watch.pm
+++ b/lib/PublicInbox/Watch.pm
@@ -134,15 +134,34 @@ sub _done_for_now {
 sub remove_eml_i { # each_inbox callback
         my ($ibx, $arg) = @_;
         my ($self, $eml, $loc) = @$arg;
+
         eval {
-                my $im = _importer_for($self, $ibx);
-                $im->remove($eml, 'spam');
-                if (my $scrub = $ibx->filter($im)) {
-                        my $scrubbed = $scrub->scrub($eml, 1);
-                        if ($scrubbed && $scrubbed != REJECT) {
-                                $im->remove($scrubbed, 'spam');
+                # try to avoid taking a lock or unnecessary spawning
+                my $im = $self->{importers}->{"$ibx"};
+                my $scrubbed;
+                if ((!$im || !$im->active) && $ibx->over) {
+                        if (content_exists($ibx, $eml)) {
+                                # continue
+                        } elsif (my $scrub = $ibx->filter($im)) {
+                                $scrubbed = $scrub->scrub($eml, 1);
+                                if ($scrubbed && $scrubbed != REJECT &&
+                                          !content_exists($ibx, $scrubbed)) {
+                                        return;
+                                }
+                        } else {
+                                return;
                         }
                 }
+
+                $im //= _importer_for($self, $ibx); # may spawn fast-import
+                $im->remove($eml, 'spam');
+                $scrubbed //= do {
+                        my $scrub = $ibx->filter($im);
+                        $scrub ? $scrub->scrub($eml, 1) : undef;
+                };
+                if ($scrubbed && $scrubbed != REJECT) {
+                        $im->remove($scrubbed, 'spam');
+                }
         };
         if ($@) {
                 warn "error removing spam at: $loc from $ibx->{name}: $@\n";