From aab36b81bd8ee4beadc2a6ea6cebec7c2c9fcf0a Mon Sep 17 00:00:00 2001 From: Florian Pritz Date: Thu, 28 Dec 2017 11:09:49 +0100 Subject: mailer.pl: Use MCE::Flow Slightly better performance and more control. Signed-off-by: Florian Pritz --- mailer.pl | 86 ++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 39 deletions(-) mode change 100644 => 100755 mailer.pl diff --git a/mailer.pl b/mailer.pl old mode 100644 new mode 100755 index c3b53da..cab1509 --- a/mailer.pl +++ b/mailer.pl @@ -12,6 +12,8 @@ use Getopt::Long; use List::Util; use MCE; use MCE::Loop; +use MCE::Flow; +use MCE::Shared; use Path::Tiny; use Pod::Usage; use Time::HiRes qw(gettimeofday tv_interval); @@ -35,6 +37,25 @@ my $chunksize = 1; # end config # #### +=head1 NAME + +mailer.pl - send lots of mails + +=head1 SYNOPSIS + +mailer.pl options ... + + Options: + --help, -h short help message + --to recipient(s) + --from from + --subject subject + --content-html body of the mail (html part) (optional) + --content-txt body of the mail (text part) + --debug output extra debug information + +=cut + my %opts = (); Getopt::Long::Configure ("bundling"); @@ -54,19 +75,6 @@ my $from = $opts{from}; my $content_html; my $content_txt; -my $mce_input; - -if ($opts{to} =~ m/.+\@.+\..+/) { - $mce_input = [$opts{to}]; -} else { - if (-r $opts{to}) { - open my $fh, "<", $opts{to}; - $mce_input = $fh; - } else { - print STDERR "Error: can't read recipient file: $!\n"; - } -} - die "Error: subject not set\n" unless ($opts{subject}); die "Error: from not set\n" unless ($opts{from}); die "Error: content-txt not set\n" unless ($opts{"content-txt"}); @@ -94,40 +102,40 @@ $msg->transport($transport); print "done\n" if $opts{debug}; print "creating workers\n" if $opts{debug}; -MCE::Loop::init {max_workers => $thread_count, chunk_size => $chunksize}; print "Sending\n"; my $send_time = [gettimeofday]; -mce_loop { - my ($mce, $chunk_ref, $chunk_id) = @_; - for my $to (@{$chunk_ref}) { +my $queue = MCE::Shared->queue(await => 1, fast => 1); + +mce_flow { + task_name => ['producer', 'sender'], + max_workers => [1, $thread_count]}, +sub { + if ($opts{to} =~ m/.+\@.+\..+/) { + $queue->enqueue($opts{to}); + } else { + if (-r $opts{to}) { + open my $fh, "<", $opts{to}; + while (<$fh>) { + $queue->enqueue($_); + $queue->await($thread_count); + } + close $fh; + } else { + print STDERR "Error: can't read recipient file: $!\n"; + } + } + $queue->end(); +}, +sub { + while (defined (my $to = $queue->dequeue())) { $msg->to($to); $msg->send(); - print "sent mail in worker ".MCE->wid." from chunk ".$chunk_id."\n" if $opts{debug}; + print "sent mail in worker ".MCE->wid."\n" if $opts{debug}; } $transport->disconnect; -} $mce_input; +}; print "done after ".tv_interval($send_time)." seconds\n"; -__END__ - -=head1 NAME - -mailer.pl - send lots of mails - -=head1 SYNOPSIS - -mailer.pl options ... - - Options: - --help, -h short help message - --to recipient(s) - --from from - --subject subject - --content-html body of the mail (html part) (optional) - --content-txt body of the mail (text part) - --debug output extra debug information - -=cut -- cgit v1.2.3-24-g4f1b