line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
package POE::Component::ResourcePool::Resource::Collection; |
4
|
4
|
|
|
4
|
|
80619
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
with qw(POE::Component::ResourcePool::Resource); |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has values => ( |
9
|
|
|
|
|
|
|
isa => "ArrayRef", |
10
|
|
|
|
|
|
|
is => "rw", |
11
|
|
|
|
|
|
|
required => 1, |
12
|
|
|
|
|
|
|
); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub try_allocating { |
15
|
|
|
|
|
|
|
my ( $self, $pool, $request, $count ) = @_; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
$count ||= 1; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
my $values = $self->values; |
20
|
|
|
|
|
|
|
if ( @$values >= $count ) { |
21
|
|
|
|
|
|
|
return @{ $values }[ 0 .. $count-1 ]; |
22
|
|
|
|
|
|
|
} else { |
23
|
|
|
|
|
|
|
return; |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
sub finalize_allocation { |
28
|
|
|
|
|
|
|
my ( $self, $pool, $request, @values ) = @_; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
splice @{ $self->values }, 0, scalar @values; |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
return @values == 1 ? $values[1] : \@values; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub free_allocation { |
36
|
|
|
|
|
|
|
my ( $self, $pool, $request, @values ) = @_; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
push @{ $self->values }, @values; |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
$self->notify_all_pools; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
__PACKAGE__ |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
__END__ |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=pod |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=head1 NAME |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
POE::Component::ResourcePool::Resource::Collection - A collection of valeus to |
53
|
|
|
|
|
|
|
be shared (e.g. handles). |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head1 SYNOPSIS |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
use POE::Component::ResourcePool::Resource::Collection; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
my $collection = POE::Component::ResourcePool::Resource::Collection->new( |
60
|
|
|
|
|
|
|
values => [ $handle1, $handle2, $handle3 ], |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
# ... |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
my $pool = POE::Component::ResourcePool->new( |
66
|
|
|
|
|
|
|
resources => { |
67
|
|
|
|
|
|
|
handles => $collection, |
68
|
|
|
|
|
|
|
}, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
$pool->request( |
72
|
|
|
|
|
|
|
params => { |
73
|
|
|
|
|
|
|
handles => $how_many, |
74
|
|
|
|
|
|
|
}, |
75
|
|
|
|
|
|
|
... |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=head1 DESCRIPTION |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
This resource allows the sharing of values from a collection in a round the |
81
|
|
|
|
|
|
|
robin fashion. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
It is useful for e.g. limiting the number of database handles to a certain |
84
|
|
|
|
|
|
|
bound, but unlike the semaphore will pool the actual values instead of just |
85
|
|
|
|
|
|
|
counting. |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
The parameter to the request can be a number denoting how many values are |
88
|
|
|
|
|
|
|
required, with 1 being the default. |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
Unlike the semaphore resource C<could_allocate> is not strict, and will always |
91
|
|
|
|
|
|
|
return true even if the count is bigger than the initial value list's size. |
92
|
|
|
|
|
|
|
This is to facilitate editing of the value collection array yourself. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
If you modify the C<values> attribute be sure to call C<notify_all_pools> in |
95
|
|
|
|
|
|
|
order to check for potentially affected requests. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Note that you cannot reliably remove values from the C<values> array because |
98
|
|
|
|
|
|
|
currently allocated values are not found in the list, but will be added later. |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
Subclassing this class with additional value tracking semantics should help |
101
|
|
|
|
|
|
|
alleviate any issues due to this. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 METHODS |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=over 4 |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=item try_allocating |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
If there are enough values in C<values> to satisfy the count (defaults to 1) |
110
|
|
|
|
|
|
|
then these items are return. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Otherwise allocation will fail. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=item finalize_allocation |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
Splices the allocated values out of the C<values> array. |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=item free_allocation |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
Pushes the allocated values to the end of the C<values> array. |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=back |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=over 4 |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=item values |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
An array reference of values to be allocated. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
Duplicate values are treated as separate values, and will not be checked for |
133
|
|
|
|
|
|
|
(this is a feature). |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=back |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
|