File Coverage

blib/lib/Database/Async/Backoff.pm
Criterion Covered Total %
statement 23 28 82.1
branch 2 4 50.0
condition 0 2 0.0
subroutine 7 10 70.0
pod 0 7 0.0
total 32 51 62.7


line stmt bran cond sub pod time code
1             package Database::Async::Backoff;
2              
3 8     8   184780 use strict;
  8         26  
  8         432  
4 8     8   51 use warnings;
  8         15  
  8         730  
5              
6             our $VERSION = '0.019'; # VERSION
7              
8             =head1 NAME
9              
10             Database::Async::Backoff - support for backoff algorithms in L
11              
12             =head1 DESCRIPTION
13              
14             =cut
15              
16 8     8   2754 use Future::AsyncAwait;
  8         117011  
  8         70  
17             my %class_for_type;
18              
19             sub new {
20 6     6 0 83 my ($class, %args) = @_;
21 6         182 bless \%args, $class
22             }
23              
24 0     0 0 0 sub initial_delay { shift->{initial_delay} }
25 0     0 0 0 sub max_delay { shift->{max_delay} }
26              
27 0     0 0 0 async sub next {
28 0         0 my ($self, $code) = @_;
29 0   0     0 return $self->{delay} ||= 1;
30             }
31              
32 1     1 0 3 async sub reset {
33 1         3 my ($self) = @_;
34 1         8 $self->{delay} = 0;
35 1         8 $self
36             }
37              
38             sub register {
39 12     12 0 58 my ($class, %args) = @_;
40 12         46 for my $k (keys %args) {
41 12         40 $class_for_type{$k} = $args{$k}
42             }
43             $class
44 12         41 }
45              
46             sub instantiate {
47 6     6 0 34 my ($class, %args) = @_;
48             my $type = delete $args{type}
49 6 50       28 or die 'backoff type required';
50 6 50       47 my $target_class = $class_for_type{$type}
51             or die 'unknown backoff type ' . $type;
52 6         76 return $target_class->new(%args);
53             }
54              
55             1;
56