| 1 | package Myriad::Util::Defer; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | ▶ | use Myriad::Class type => 'role'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 5 | our $VERSION = '1.001'; # VERSION | |||||||||||
| 6 | 5 | our $AUTHORITY = 'cpan:DERIV'; # AUTHORITY | |||||||||||
| 7 | |||||||||||||
| 8 | =encoding utf8 | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | Myriad::Util::Defer - provide a deferred wrapper attribute | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 DESCRIPTION | ||||||||||||
| 15 | |||||||||||||
| 16 | This is used to make an async method delay processing until later. | ||||||||||||
| 17 | |||||||||||||
| 18 | It can be controlled by the C<MYRIAD_RANDOM_DELAY> environment variable, | ||||||||||||
| 19 | and defaults to no delay. | ||||||||||||
| 20 | |||||||||||||
| 21 | =cut | ||||||||||||
| 22 | |||||||||||||
| 23 | 5 | ▶ | use Attribute::Handlers; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 5 | ▶ | use Class::Method::Modifiers; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | 5 | ▶ | use constant RANDOM_DELAY => $ENV{MYRIAD_RANDOM_DELAY} || 0; | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MYRIAD_RANDOM_DELAY'} || 0
MC/DC100%1/1
$ENV{'MYRIAD_RANDOM_DELAY'} || 0
$ENV{'MYRIAD_RANDOM_DELAY'}
Truth table100%2/2
A
A= $ENV{'MYRIAD_RANDOM_DELAY'}
| |||||||||||||
| 27 | |||||||||||||
| 28 | 5 | ▶ | use Sub::Util; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | 2 | ▶ | sub MODIFY_CODE_ATTRIBUTES ($class, $code, @attrs) { | ||||||||||
|
Subroutine
MODIFY_CODE_ATTRIBUTES: called CC=3 SCAR=11.3
| |||||||||||||
| 30 | 2 | my $name = Sub::Util::subname($code); | |||||||||||
| 31 | 2 | my ($method_name) = $name =~ m{::([^:]+)$}; | |||||||||||
| 32 | 2 | for my $attr (@attrs) { | |||||||||||
| 33 | 2 | ▶ | if($attr eq 'Defer') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 34 | 2 | $class->defer_method($method_name, $name); | |||||||||||
| 35 | } else { | ||||||||||||
| 36 | 0 | die 'unknown attribute ' . $attr; | |||||||||||
| 37 | } | ||||||||||||
| 38 | } | ||||||||||||
| 39 | 2 | return; | |||||||||||
| 40 | } | ||||||||||||
| 41 | |||||||||||||
| 42 | # Helper method that allows us to return a not-quite-immediate | ||||||||||||
| 43 | # Future from some inherently non-async code. | ||||||||||||
| 44 | 2 | ▶ | sub defer_method ($package, $name, $fqdn) { | ||||||||||
|
Subroutine
defer_method: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 45 | 2 | $log->tracef('will defer handler for %s::%s by %f', $package, $name, RANDOM_DELAY); | |||||||||||
| 46 | 2 | my $code = $package->can($name); | |||||||||||
| 47 | 4 | ▶ | my $replacement = async sub ($self, @args) { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | # effectively $loop->later, but in an await-compatible way: | ||||||||||||
| 49 | # either zero (default behaviour) or if we have a random | ||||||||||||
| 50 | # delay assigned, use that to drive a uniform rand() call | ||||||||||||
| 51 | 2 | $log->tracef('call to %s::%s, deferring start', $package, $name); | |||||||||||
| 52 | 2 | await $self->loop->delay_future( | |||||||||||
| 53 | after => rand(RANDOM_DELAY) | ||||||||||||
| 54 | ); | ||||||||||||
| 55 | |||||||||||||
| 56 | 2 | $log->tracef('deferred call to %s::%s runs now', $package, $name); | |||||||||||
| 57 | |||||||||||||
| 58 | 2 | return await $self->$code( | |||||||||||
| 59 | @args | ||||||||||||
| 60 | ); | ||||||||||||
| 61 | 2 | }; | |||||||||||
| 62 | { | ||||||||||||
| 63 | 5 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 64 | 5 | ▶ | no warnings 'redefine'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 65 | 2 | *{join '::', $package, $name} = $replacement if RANDOM_DELAY; | |||||||||||
| 66 | } | ||||||||||||
| 67 | } | ||||||||||||
| 68 | |||||||||||||
| 69 | 5 | 1; | |||||||||||
| 70 | |||||||||||||
| 71 | =head1 AUTHOR | ||||||||||||
| 72 | |||||||||||||
| 73 | Deriv Group Services Ltd. C<< DERIV@cpan.org >>. | ||||||||||||
| 74 | |||||||||||||
| 75 | See L<Myriad/CONTRIBUTORS> for full details. | ||||||||||||
| 76 | |||||||||||||
| 77 | =head1 LICENSE | ||||||||||||
| 78 | |||||||||||||
| 79 | Copyright Deriv Group Services Ltd 2020-2022. Licensed under the same terms as Perl itself. | ||||||||||||
| 80 | |||||||||||||