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 2     2   852 use strict;
  2         3  
  2         62  
4 2     2   10 use warnings;
  2         4  
  2         83  
5              
6             our $VERSION = '0.017'; # 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 2     2   12 use Future::AsyncAwait;
  2         4  
  2         19  
17             my %class_for_type;
18              
19             sub new {
20 4     4 0 42 my ($class, %args) = @_;
21 4         23 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         2 my ($self) = @_;
34 1         5 $self->{delay} = 0;
35 1         5 $self
36             }
37              
38             sub register {
39 4     4 0 18 my ($class, %args) = @_;
40 4         16 for my $k (keys %args) {
41 4         13 $class_for_type{$k} = $args{$k}
42             }
43             $class
44 4         13 }
45              
46             sub instantiate {
47 4     4 0 19 my ($class, %args) = @_;
48             my $type = delete $args{type}
49 4 50       14 or die 'backoff type required';
50 4 50       15 my $target_class = $class_for_type{$type}
51             or die 'unknown backoff type ' . $type;
52 4         27 return $target_class->new(%args);
53             }
54              
55             1;
56