File Coverage

blib/lib/POE/Component/ResourcePool/Resource/TryList.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3             package POE::Component::ResourcePool::Resource::TryList;
4 2     2   30646 use Moose;
  0            
  0            
5              
6             with 'POE::Component::ResourcePool::Resource';
7              
8             has resources => (
9             isa => "ArrayRef[POE::Component::ResourcePool::Resource]",
10             is => "rw",
11             required => 1,
12             auto_deref => 1,
13             );
14              
15             sub _for_each_resource {
16             my ( $self, $method, @args ) = @_;
17              
18             foreach my $resource ( $self->resources ) {
19             $resource->$method(@args);
20             }
21             }
22              
23             sub forget_request {
24             my ( $self, @args ) = @_;
25             $self->_for_each_resource( forget_request => @args );
26             }
27              
28             sub register_request {
29             my ( $self, @args ) = @_;
30             $self->_for_each_resource( register_request => @args );
31             }
32              
33             before register_pool => sub {
34             my ( $self, @args ) = @_;
35             $self->_for_each_resource( register_pool => @args );
36             };
37              
38             before unregister_pool => sub {
39             my ( $self, @args ) = @_;
40             $self->_for_each_resource( unregister_pool => @args );
41             };
42              
43             sub try_allocating {
44             my ( $self, @args ) = @_;
45              
46             foreach my $resource ( $self->resources ) {
47             if ( my @allocation = $resource->try_allocating( @args ) ) {
48             return ( $resource, @allocation );
49             }
50             }
51              
52             return;
53             }
54              
55             sub could_allocate {
56             my ( $self, @args ) = @_;
57              
58             foreach my $resource ( $self->resources ) {
59             if ( $resource->could_allocate( @args ) ) {
60             return 1;
61             }
62             }
63              
64             return;
65             }
66              
67             sub finalize_allocation {
68             my ( $self, $pool, $request, $resource, @allocation ) = @_;
69              
70             $resource->finalize_allocation( $pool, $request, @allocation );
71             }
72              
73             sub free_allocation {
74             my ( $self, $pool, $request, $resource, @allocation ) = @_;
75              
76             $resource->free_allocation( $pool, $request, @allocation );
77             }
78              
79             __PACKAGE__
80              
81             __END__
82              
83             =pod
84              
85             =head1 NAME
86              
87             POE::Component::ResourcePool::Resource::TryList - Delegate to a number of
88             resources.
89              
90             =head1 SYNOPSIS
91              
92             use POE::Component::ResourcePool::Resource::TryList;
93              
94             my $good = POE::Component::ResourcePool::Resource::Good->new( ... );
95              
96             my $better = POE::Component::ResourcePool::Resource::Better->new( ... );
97              
98             my $best = POE::Component::ResourcePool::Resource::TryList->new(
99             resources => [ $better, $good ],
100             );
101              
102             =head1 DESCRIPTION
103              
104             This class allows you to specify fallback lists for resources easily.
105              
106             The resources will be delegated to appropriately.
107              
108             The only difference is that sometimes resources that return false from
109             C<could_allocate> will still have C<try_allocating> called on them, because the
110             try list only requires that one of the sub resources return true from
111             C<could_allocate>.
112              
113             It is trivial to subclass and override it such that C<could_allocate> only
114             returns true if all the sub resources do, thus resolving this issue. However,
115             this seems to have a diminished practical value to me.
116              
117             =head1 ATTRIBUTES
118              
119             =over 4
120              
121             =item resources
122              
123             The array of resources to deleate to.
124              
125             =back
126              
127             =cut
128              
129