Supply Back Pressure
In Raku, a Supply is one of the primary
tools for sending messages between threads. From the way a Supply is
structured, it is obvious that is provides a means for one ore more tasks to
send events to multiple recipient tasks. What is less obvious, however, is that
a Supply imposes a cost on the sender.
Consider this program:
my $counter = Supplier.new;
start react whenever $counter.Supply {
say "A pre-whenever $_";
sleep rand;
say "A post-whenever $_";
}
start react whenever $counter.Supply {
say "B pre-whenever $_";
sleep rand;
say "B post-whenever $_";
}
start for 1...* {
say "pre-emit $_";
$counter.emit($_);
say "post-emit $_";
}
sleep 10;
Here we have three tasks running, each in a separate thread. We let the main
program quit after 10 seconds. The first threads two receive messages from the
$counter.Supply. The third thread feeds a sequence of integers to this supply.
You might be tempted to think that the final task will race through the delivery
of events, but if so, you’d be wrong.