| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Argon::Queue; |
|
2
|
|
|
|
|
|
|
# ABSTRACT: Bounded, prioritized queue class |
|
3
|
|
|
|
|
|
|
$Argon::Queue::VERSION = '0.17'; |
|
4
|
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
175264
|
use strict; |
|
|
1
|
|
|
|
|
6
|
|
|
|
1
|
|
|
|
|
24
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
|
1
|
|
|
|
|
1
|
|
|
|
1
|
|
|
|
|
19
|
|
|
7
|
1
|
|
|
1
|
|
4
|
use Carp; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
41
|
|
|
8
|
1
|
|
|
1
|
|
294
|
use Moose; |
|
|
1
|
|
|
|
|
371185
|
|
|
|
1
|
|
|
|
|
6
|
|
|
9
|
1
|
|
|
1
|
|
6921
|
use Argon::Constants qw(:priorities); |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
164
|
|
|
10
|
1
|
|
|
1
|
|
259
|
use Argon::Tracker; |
|
|
1
|
|
|
|
|
5
|
|
|
|
1
|
|
|
|
|
43
|
|
|
11
|
1
|
|
|
1
|
|
9
|
use Argon::Log; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
691
|
|
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
has max => ( |
|
15
|
|
|
|
|
|
|
is => 'rw', |
|
16
|
|
|
|
|
|
|
isa => 'Int', |
|
17
|
|
|
|
|
|
|
default => 0, |
|
18
|
|
|
|
|
|
|
); |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
has tracker => ( |
|
21
|
|
|
|
|
|
|
is => 'ro', |
|
22
|
|
|
|
|
|
|
isa => 'Argon::Tracker', |
|
23
|
|
|
|
|
|
|
lazy => 1, |
|
24
|
|
|
|
|
|
|
builder => '_build_tracker', |
|
25
|
|
|
|
|
|
|
handles => { |
|
26
|
|
|
|
|
|
|
}, |
|
27
|
|
|
|
|
|
|
); |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _build_tracker { |
|
30
|
2
|
|
|
2
|
|
6
|
my $self = shift; |
|
31
|
2
|
|
|
|
|
24
|
Argon::Tracker->new( |
|
32
|
|
|
|
|
|
|
capacity => $self->max, |
|
33
|
|
|
|
|
|
|
length => $self->max * $self->max, |
|
34
|
|
|
|
|
|
|
); |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
has msgs => ( |
|
38
|
|
|
|
|
|
|
is => 'ro', |
|
39
|
|
|
|
|
|
|
isa => 'ArrayRef[ArrayRef[Argon::Message]]', |
|
40
|
|
|
|
|
|
|
default => sub { [[], [], []] }, |
|
41
|
|
|
|
|
|
|
); |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has count => ( |
|
44
|
|
|
|
|
|
|
is => 'rw', |
|
45
|
|
|
|
|
|
|
isa => 'Int', |
|
46
|
|
|
|
|
|
|
default => 0, |
|
47
|
|
|
|
|
|
|
traits => ['Counter'], |
|
48
|
|
|
|
|
|
|
handles => { |
|
49
|
|
|
|
|
|
|
inc_count => 'inc', |
|
50
|
|
|
|
|
|
|
dec_count => 'dec', |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
); |
|
53
|
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has balanced => ( |
|
55
|
|
|
|
|
|
|
is => 'rw', |
|
56
|
|
|
|
|
|
|
isa => 'Int', |
|
57
|
|
|
|
|
|
|
default => sub { time }, |
|
58
|
|
|
|
|
|
|
); |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
after max => sub { |
|
61
|
|
|
|
|
|
|
my $self = shift; |
|
62
|
|
|
|
|
|
|
if (@_) { |
|
63
|
|
|
|
|
|
|
$self->tracker->capacity($self->max); |
|
64
|
|
|
|
|
|
|
$self->tracker->length($self->max * $self->max); |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
}; |
|
67
|
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
|
69
|
8
|
|
|
8
|
1
|
330
|
sub is_empty { $_[0]->count == 0 } |
|
70
|
9
|
|
|
9
|
1
|
351
|
sub is_full { $_[0]->count >= $_[0]->max } |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub put { |
|
74
|
7
|
|
|
7
|
1
|
747
|
my ($self, $msg) = @_; |
|
75
|
|
|
|
|
|
|
|
|
76
|
7
|
100
|
100
|
|
|
181
|
croak 'usage: $queue->put($msg)' |
|
|
|
|
66
|
|
|
|
|
|
77
|
|
|
|
|
|
|
unless defined $msg |
|
78
|
|
|
|
|
|
|
&& (ref $msg || '') eq 'Argon::Message'; |
|
79
|
|
|
|
|
|
|
|
|
80
|
6
|
|
|
|
|
25
|
$self->promote; |
|
81
|
|
|
|
|
|
|
|
|
82
|
6
|
100
|
|
|
|
92
|
croak 'queue full' if $self->is_full; |
|
83
|
|
|
|
|
|
|
|
|
84
|
5
|
|
|
|
|
51
|
push @{$self->msgs->[$msg->pri]}, $msg; |
|
|
5
|
|
|
|
|
163
|
|
|
85
|
|
|
|
|
|
|
|
|
86
|
5
|
|
|
|
|
201
|
$self->tracker->start($msg); |
|
87
|
5
|
|
|
|
|
216
|
$self->inc_count; |
|
88
|
5
|
|
|
|
|
164
|
$self->count; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub get { |
|
93
|
5
|
|
|
5
|
1
|
393
|
my $self = shift; |
|
94
|
5
|
100
|
|
|
|
17
|
return if $self->is_empty; |
|
95
|
|
|
|
|
|
|
|
|
96
|
4
|
|
|
|
|
13
|
foreach my $pri ($HIGH, $NORMAL, $LOW) { |
|
97
|
8
|
|
|
|
|
252
|
my $queue = $self->msgs->[$pri]; |
|
98
|
|
|
|
|
|
|
|
|
99
|
8
|
100
|
|
|
|
23
|
if (@$queue) { |
|
100
|
4
|
|
|
|
|
8
|
my $msg = shift @$queue; |
|
101
|
4
|
|
|
|
|
114
|
$self->tracker->finish($msg); |
|
102
|
4
|
|
|
|
|
172
|
$self->dec_count; |
|
103
|
|
|
|
|
|
|
|
|
104
|
4
|
|
|
|
|
35
|
return $msg; |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
} |
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub promote { |
|
110
|
9
|
|
|
9
|
0
|
25
|
my $self = shift; |
|
111
|
9
|
|
|
|
|
298
|
my $avg = $self->tracker->avg_time; |
|
112
|
9
|
|
|
|
|
27
|
my $max = $avg * 1.5; |
|
113
|
9
|
100
|
|
|
|
300
|
return 0 unless time - $self->balanced >= $max; |
|
114
|
|
|
|
|
|
|
|
|
115
|
4
|
|
|
|
|
7
|
my $moved = 0; |
|
116
|
|
|
|
|
|
|
|
|
117
|
4
|
|
|
|
|
7
|
foreach my $pri ($LOW, $NORMAL) { |
|
118
|
8
|
|
|
|
|
11
|
while (my $msg = shift @{$self->msgs->[$pri]}) { |
|
|
9
|
|
|
|
|
159
|
|
|
119
|
3
|
100
|
|
|
|
53
|
if ($self->tracker->age($msg) > $max) { |
|
120
|
1
|
|
|
|
|
2
|
push @{$self->msgs->[$pri - 1]}, $msg; |
|
|
1
|
|
|
|
|
24
|
|
|
121
|
1
|
|
|
|
|
19
|
$self->tracker->touch($msg); |
|
122
|
1
|
|
|
|
|
3
|
++$moved; |
|
123
|
|
|
|
|
|
|
} else { |
|
124
|
2
|
|
|
|
|
3
|
unshift @{$self->msgs->[$pri]}, $msg; |
|
|
2
|
|
|
|
|
40
|
|
|
125
|
2
|
|
|
|
|
5
|
last; |
|
126
|
|
|
|
|
|
|
} |
|
127
|
|
|
|
|
|
|
} |
|
128
|
|
|
|
|
|
|
} |
|
129
|
|
|
|
|
|
|
|
|
130
|
4
|
100
|
|
|
|
12
|
log_trace 'promoted %d msgs', $moved |
|
131
|
|
|
|
|
|
|
if $moved; |
|
132
|
|
|
|
|
|
|
|
|
133
|
4
|
|
|
|
|
104
|
return $moved; |
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
1; |
|
139
|
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
__END__ |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=pod |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=encoding UTF-8 |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=head1 NAME |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
Argon::Queue - Bounded, prioritized queue class |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head1 VERSION |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
version 0.17 |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
use Argon::Queue; |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
my $q = Argon::Queue->new(max => 32); |
|
159
|
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
unless ($q->is_full) { |
|
161
|
|
|
|
|
|
|
$q->put(...); |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
unless ($q->is_empty) { |
|
165
|
|
|
|
|
|
|
my $msg = $q->get; |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
The bounded priority queue used by the L<Argon::Manager> for L<Argon::Message>s |
|
171
|
|
|
|
|
|
|
submitted to the Ar network. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 max |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
The maximum number of messages supported by the queue. |
|
178
|
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=head1 METHODS |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
=head2 is_empty |
|
182
|
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
Returns true if the queue is empty. |
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head2 is_full |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
Returns true if there is at least one L<Argon::Message> in the queue. |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
=head2 put |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
Adds a message to the queue. Croaks if the supplied message is not an |
|
192
|
|
|
|
|
|
|
L<Argon::Message> or if the queue is full. |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Adding a message to the queue has the side effect of promoting any previously |
|
195
|
|
|
|
|
|
|
added messages that have been stuck at a lower level priority for an overly |
|
196
|
|
|
|
|
|
|
long time. |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 get |
|
199
|
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
Removes and returns the next L<Argon::Message> on the queue. Returns C<undef> |
|
201
|
|
|
|
|
|
|
if the queue is empty. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 AUTHOR |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Jeff Ober <sysread@fastmail.fm> |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
This software is copyright (c) 2017 by Jeff Ober. |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
|
212
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
=cut |