line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Queue::Leaky; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
16792
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Queue::Leaky::Types; |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
has 'max_items' => ( |
9
|
|
|
|
|
|
|
is => 'rw', |
10
|
|
|
|
|
|
|
isa => 'Int', |
11
|
|
|
|
|
|
|
required => 1, |
12
|
|
|
|
|
|
|
default => 0, |
13
|
|
|
|
|
|
|
); |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
has 'key_generator' => ( |
16
|
|
|
|
|
|
|
is => 'rw', |
17
|
|
|
|
|
|
|
isa => 'CodeRef', |
18
|
|
|
|
|
|
|
required => 1, |
19
|
|
|
|
|
|
|
default => sub { |
20
|
|
|
|
|
|
|
return sub { |
21
|
|
|
|
|
|
|
my $self = shift; |
22
|
|
|
|
|
|
|
$self->queue; |
23
|
|
|
|
|
|
|
}; |
24
|
|
|
|
|
|
|
}, |
25
|
|
|
|
|
|
|
); |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
{ |
28
|
|
|
|
|
|
|
my $default = sub { |
29
|
|
|
|
|
|
|
my $class = shift; |
30
|
|
|
|
|
|
|
return sub { |
31
|
|
|
|
|
|
|
Class::MOP::load_class($class); |
32
|
|
|
|
|
|
|
$class->new; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has 'queue' => ( |
37
|
|
|
|
|
|
|
is => 'rw', |
38
|
|
|
|
|
|
|
does => 'Queue::Leaky::Driver', |
39
|
|
|
|
|
|
|
required => 1, |
40
|
|
|
|
|
|
|
coerce => 1, |
41
|
|
|
|
|
|
|
default => $default->( 'Queue::Leaky::Driver::Simple' ), |
42
|
|
|
|
|
|
|
handles => [ qw(next) ], |
43
|
|
|
|
|
|
|
); |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
has 'state' => ( |
46
|
|
|
|
|
|
|
is => 'rw', |
47
|
|
|
|
|
|
|
does => 'Queue::Leaky::State', |
48
|
|
|
|
|
|
|
required => 1, |
49
|
|
|
|
|
|
|
coerce => 1, |
50
|
|
|
|
|
|
|
default => $default->( 'Queue::Leaky::State::Memory' ), |
51
|
|
|
|
|
|
|
handles => { |
52
|
|
|
|
|
|
|
map { ("state_$_" => $_) } qw(get set remove incr decr) |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
no Moose; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub insert { |
62
|
|
|
|
|
|
|
my $self = shift; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
my $key = $self->key_generator->($self, @_); |
65
|
|
|
|
|
|
|
my $count = $self->state_incr($key); |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
if ($self->max_items && $self->max_items < $count) { |
68
|
|
|
|
|
|
|
$self->state_decr($key); |
69
|
|
|
|
|
|
|
return (); |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
my $rv = $self->queue->insert(@_); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
$self->state_decr($key) unless $rv; |
75
|
|
|
|
|
|
|
return $rv; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub fetch { |
79
|
|
|
|
|
|
|
my $self = shift; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $rv = $self->queue->fetch(@_); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
if ($rv) { |
84
|
|
|
|
|
|
|
my $key = $self->key_generator->($self, @_); |
85
|
|
|
|
|
|
|
$self->state_decr($key); |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
return $rv; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub clear { |
91
|
|
|
|
|
|
|
my $self = shift; |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my $rv = $self->queue->clear(@_); |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
if ($rv) { |
96
|
|
|
|
|
|
|
my $key = $self->key_generator->($self, @_); |
97
|
|
|
|
|
|
|
$self->state_remove($key) if $key; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
return $rv; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1; |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
__END__ |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=encoding utf-8 |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=for stopwords |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
=head1 NAME |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
Queue::Leaky - Queues with leaky buckets |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
=head1 SYNOPSIS |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
use Queue::Leaky; |
117
|
|
|
|
|
|
|
my $queue = Queue::Leaky->new; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
$queue->inesrt( ... ); |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
while ( 1 ) { |
122
|
|
|
|
|
|
|
if ($queue->next) { |
123
|
|
|
|
|
|
|
my $message = $queue->fetch; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
$queue->clear; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 DESCRIPTION |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
Queue::Leaky is employed as a traffic regulator. |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 AUTHOR |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Taro Funaki E<lt>t@33rpm.jpE<gt> |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 LICENSE |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify |
140
|
|
|
|
|
|
|
it under the same terms as Perl itself. |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 SEE ALSO |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |