test script is here.
#!/usr/local/bin/perl
use Test::MockTime qw(:all);
use strict;
use feature qw(say);
use Data::Throttler;
my $start = time;
{
set_absolute_time($start);
my $th = Data::Throttler->new(
max_items => 1,
interval => 10,
);
say $th->try_push( key => "key1" ); # 1
say $th->buckets_dump;
set_absolute_time($start+1);
say $th->try_push( key => "key1" ); # 0
say $th->buckets_dump;
}
{
set_absolute_time($start);
my $th = Data::Throttler->new(
max_items => 1,
interval => 2,
);
say $th->try_push( key => "key1" ); # 1
say $th->buckets_dump;
set_absolute_time($start+1);
say $th->try_push( key => "key1" ); # 1
say $th->buckets_dump;
}
{
set_absolute_time($start);
my $th = Data::Throttler->new(
max_items => 1,
interval => 1,
);
say $th->try_push( key => "key1" ); # 1
say $th->buckets_dump;
set_absolute_time($start+1);
say $th->try_push( key => "key1" ); # 1
say $th->try_push( key => "key1" ); # 1 bug
say $th->try_push( key => "key1" ); # 1 bug
say $th->buckets_dump;
}
output
1
.-----------------------------------------------.
| # | idx | Time: 15:05:48 | Key | Count | <-15:05:48
+----+-----+---------------------+------+-------+
| 1 | 0 | 15:05:39 - 15:05:39 | | |
| 2 | 1 | 15:05:40 - 15:05:40 | | |
| 3 | 2 | 15:05:41 - 15:05:41 | | |
| 4 | 3 | 15:05:42 - 15:05:42 | | |
| 5 | 4 | 15:05:43 - 15:05:43 | | |
| 6 | 5 | 15:05:44 - 15:05:44 | | |
| 7 | 6 | 15:05:45 - 15:05:45 | | |
| 8 | 7 | 15:05:46 - 15:05:46 | | |
| 9 | 8 | 15:05:47 - 15:05:47 | | |
| 10 | 9 | 15:05:48 - 15:05:48 | key1 | 1 | <-15:05:48 good at first
'----+-----+---------------------+------+-------'
0
.-----------------------------------------------.
| # | idx | Time: 15:05:49 | Key | Count | <-15:05:49
+----+-----+---------------------+------+-------+
| 1 | 2 | 15:05:41 - 15:05:41 | | |
| 2 | 3 | 15:05:42 - 15:05:42 | | |
| 3 | 4 | 15:05:43 - 15:05:43 | | |
| 4 | 5 | 15:05:44 - 15:05:44 | | |
| 5 | 6 | 15:05:45 - 15:05:45 | | |
| 6 | 7 | 15:05:46 - 15:05:46 | | |
| 7 | 8 | 15:05:47 - 15:05:47 | | |
| 8 | 9 | 15:05:48 - 15:05:48 | key1 | 1 |
| 9 | 0 | 15:05:49 - 15:05:49 | | | <-15:05:49
| 10 | 1 | 15:05:50 - 15:05:50 | | | <- too many
'----+-----+---------------------+------+-------'
1
.----------------------------------------------.
| # | idx | Time: 15:05:48 | Key | Count |
+---+-----+---------------------+------+-------+
| 1 | 0 | 15:05:47 - 15:05:47 | | |
| 2 | 1 | 15:05:48 - 15:05:48 | key1 | 1 |
'---+-----+---------------------+------+-------'
1
.----------------------------------------------.
| # | idx | Time: 15:05:49 | Key | Count |
+---+-----+---------------------+------+-------+
| 1 | 0 | 15:05:49 - 15:05:49 | key1 | 1 |
| 2 | 1 | 15:05:50 - 15:05:50 | | | <- too many
'---+-----+---------------------+------+-------'
1
.----------------------------------------------.
| # | idx | Time: 15:05:48 | Key | Count |
+---+-----+---------------------+------+-------+
| 1 | 0 | 15:05:48 - 15:05:48 | key1 | 1 |
'---+-----+---------------------+------+-------'
1
1
1
.---------------------------------------------.
| # | idx | Time: 15:05:49 | Key | Count | <- 15:05:49
+---+-----+---------------------+-----+-------+
| 1 | 0 | 15:05:50 - 15:05:50 | | | <- Where is 15:05:49?
'---+-----+---------------------+-----+-------'
pache is here.
% diff -u Throttler.pm.orig Throttler.pm
--- Throttler.pm.orig 2019-07-01 06:25:07.203131000 +0900
+++ Throttler.pm 2019-07-01 15:09:53.136232000 +0900
@@ -421,7 +421,7 @@
return 1;
}
- while($self->last_bucket()->{time}->min <= $time) {
+ until($self->last_bucket()->{time}->member($time)) {
$self->bucket_add();
}
test script is here.
#!/usr/local/bin/perl use Test::MockTime qw(:all); use strict; use feature qw(say); use Data::Throttler; my $start = time; { set_absolute_time($start); my $th = Data::Throttler->new( max_items => 1, interval => 10, ); say $th->try_push( key => "key1" ); # 1 say $th->buckets_dump; set_absolute_time($start+1); say $th->try_push( key => "key1" ); # 0 say $th->buckets_dump; } { set_absolute_time($start); my $th = Data::Throttler->new( max_items => 1, interval => 2, ); say $th->try_push( key => "key1" ); # 1 say $th->buckets_dump; set_absolute_time($start+1); say $th->try_push( key => "key1" ); # 1 say $th->buckets_dump; } { set_absolute_time($start); my $th = Data::Throttler->new( max_items => 1, interval => 1, ); say $th->try_push( key => "key1" ); # 1 say $th->buckets_dump; set_absolute_time($start+1); say $th->try_push( key => "key1" ); # 1 say $th->try_push( key => "key1" ); # 1 bug say $th->try_push( key => "key1" ); # 1 bug say $th->buckets_dump; }output
pache is here.
% diff -u Throttler.pm.orig Throttler.pm --- Throttler.pm.orig 2019-07-01 06:25:07.203131000 +0900 +++ Throttler.pm 2019-07-01 15:09:53.136232000 +0900 @@ -421,7 +421,7 @@ return 1; } - while($self->last_bucket()->{time}->min <= $time) { + until($self->last_bucket()->{time}->member($time)) { $self->bucket_add(); }