[PATCH 3/4] delayer: Introduce options for buffering and persistence

Christoph Biedl isc.cxzo at manchmal.in-ulm.de
Mon Jan 15 17:00:00 UTC 2024


---
 contrib/delayer.in | 107 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 93 insertions(+), 14 deletions(-)

diff --git a/contrib/delayer.in b/contrib/delayer.in
index 974efcd2a..8b66c3b48 100644
--- a/contrib/delayer.in
+++ b/contrib/delayer.in
@@ -19,22 +19,45 @@
 use strict;
 use warnings;
 
-my $delay = shift || die "usage: $0 delay prog-n-args\n";
+use Getopt::Long;
+use Storable;
+
+(@ARGV >= 2) || die "usage: $0 delay prog-n-args\n";
+
+my $delay = 60;
+my $buffered = 1;
+my $store;
+
+if ($ARGV[0] =~ /^[0-9]+$/) {
+    # Legacy calling convention
+    $delay = shift;
+} else {
+    GetOptions (
+        'delay=i' => \$delay,
+        'buffered!' => \$buffered,
+        'store=s' => \$store,
+    ) or die ('Usage!');
+}
 
 my $timeout = $delay;
 
 open(OUT, "|-", @ARGV) || die "open |prog-n-args: $!\n";
+if (!$buffered) {
+    select(OUT);
+    $| = 1;
+    select(STDOUT);
+}
 
-#select(OUT);
-#$| = 1;
-#select(STDOUT);
+my @queue;
+if ($store) {
+    eval { @queue = @{retrieve ($store)} };
+    unlink ($store);
+}
 
 my $rin = '';
 my $rout;
 vec($rin, fileno(STDIN), 1) = 1;
 
-my @queue;
-
 while (1) {
     my ($nfound, $timeleft) = select($rout = $rin, undef, undef, $timeout);
     my $now = time();
@@ -43,12 +66,16 @@ while (1) {
     if (vec($rout, fileno(STDIN), 1)) {
         my $line = <STDIN>;
         if (!defined $line) {    # exit NOW!
-            foreach (@queue) {
-                s/^[^:]+://g;
-                print OUT;
+            if ($store) {
+                eval { store (\@queue, $store); };
+            } else {
+                foreach (@queue) {
+                    s/^[^:]+://g;
+                    print OUT;
+                }
+                close(OUT);
+                sleep(1);
             }
-            close(OUT);
-            sleep(1);
             exit;
         }
         push(@queue, "$exp:$line");
@@ -76,6 +103,10 @@ delayer - A pipe to delay line-based input by a given time
 
 =head1 SYNOPSIS
 
+    <some program> | delayer [options] | <some other program>
+
+Legacy calling convention:
+
     <some program> | delayer <seconds> | <some other program>
 
 =head1 DESCRIPTION
@@ -106,7 +137,9 @@ and C<status-file>.
 
     innfeed-delayed!\
         :!*\
-        :Tc,Wnm*,S16384:/path/to/delayer 120 \
+        :Tc,Wnm*,S16384:/path/to/delayer \
+            --delay 120 \
+            -- \
             <newsbin>/innfeed -c innfeed-delayed.conf
 
 This will delay articles via that feed for 120 seconds.
@@ -117,14 +150,60 @@ This will delay articles via that feed for 120 seconds.
 
 =back
 
+=head1 OPTIONS
+
+=head2 C<--delay> <seconds>
+
+Delay articles by the given amount of seconds. Default is C<60>.
+
+=head2 C<--buffered>, C<--no-buffered>
+
+Per default, output is buffered. This increases the time until an
+article is actually sent if the number of articles is small. Disable
+buffering to have a more accurate delay, at a price of a (possibly
+neglectable) performance overhead.
+
+=head2 C<--store> F<file>
+
+By default, all buffered lines are written out if the input is closed,
+even if the configured delay has not been reached yet.
+
+With the given store, these lines will be written to the given file
+instead, to be used upon next startup.
+
+=head2 C<-->
+
+This separates the options to the delay from the process the output
+is written to.
+
+=head2 C<The rest>
+
+This should be the program the output is written to, optionally with
+some parameters.
+
+=head1 OPTIONS, LEGACY
+
+This interface is considered legacy and will be removed some day.
+
+=head2 C<delay>
+
+Delay articles by the given amount of seconds.
+
+=head2 C<The rest>
+
+This should be the program the output is written to, optionally with
+some parameters.
+
 =head1 BUGS
 
 If the feed is closed, all lines in the store are printed immediately,
-breaking the contract of delaying them.
+breaking the contract of delaying them. See C<--store> to avoid this.
 
 If the number of articles in that feed is rather low - just a few
 articles per delay time or less - some effects of buffering will delay
-the transmission even further.
+the transmission even further. See C<--no-buffering> to alleviate this.
+Still a line is not sent to output until another line is read from the
+input.
 
 =head1 HISTORY
 
-- 
2.39.2



More information about the inn-workers mailing list