diff options
| author | franck cuny <franck@lumberjaph.net> | 2010-03-31 22:30:23 +0200 |
|---|---|---|
| committer | franck cuny <franck@lumberjaph.net> | 2010-03-31 22:30:23 +0200 |
| commit | 2a68ed3d0138827f4fed8a3520f7859493da4e1b (patch) | |
| tree | 9cb12972b32952c45d920ac1c906aedc6416a8c6 /lib/Plack/Middleware/Throttle | |
| parent | initial commit (diff) | |
| download | plack-middleware-throttle-2a68ed3d0138827f4fed8a3520f7859493da4e1b.tar.gz | |
import old Plack::Middleware::APIRateLimit (renamed as miyagawa's suggestion) and stole API from rack::throttle
Diffstat (limited to 'lib/Plack/Middleware/Throttle')
| -rw-r--r-- | lib/Plack/Middleware/Throttle/Backend/Hash.pm | 20 | ||||
| -rw-r--r-- | lib/Plack/Middleware/Throttle/Daily.pm | 17 | ||||
| -rw-r--r-- | lib/Plack/Middleware/Throttle/Hourly.pm | 17 | ||||
| -rw-r--r-- | lib/Plack/Middleware/Throttle/Interval.pm | 12 | ||||
| -rw-r--r-- | lib/Plack/Middleware/Throttle/Limiter.pm | 21 |
5 files changed, 87 insertions, 0 deletions
diff --git a/lib/Plack/Middleware/Throttle/Backend/Hash.pm b/lib/Plack/Middleware/Throttle/Backend/Hash.pm new file mode 100644 index 0000000..9144e36 --- /dev/null +++ b/lib/Plack/Middleware/Throttle/Backend/Hash.pm @@ -0,0 +1,20 @@ +package Plack::Middleware::Throttle::Backend::Hash; + +use Moose; + +has store => ( + is => 'rw', + isa => 'HashRef', + traits => ['Hash'], + lazy => 1, + default => sub { {} }, + handles => { get => 'get', set => 'set' } +); + +sub incr { + my ( $self, $key ) = @_; + my $value = ( $self->get($key) || 0 ) + 1; + $self->set( $key => $value ); +} + +1; diff --git a/lib/Plack/Middleware/Throttle/Daily.pm b/lib/Plack/Middleware/Throttle/Daily.pm new file mode 100644 index 0000000..d28d1d7 --- /dev/null +++ b/lib/Plack/Middleware/Throttle/Daily.pm @@ -0,0 +1,17 @@ +package Plack::Middleware::Throttle::Daily; + +use Moose; +extends 'Plack::Middleware::Throttle::Limiter'; + +sub cache_key { + my ( $self, $env ) = @_; + $self->client_identifier($env) . "_" + . DateTime->now->strftime("%Y-%m-%d"); +} + +sub reset_time { + my $dt = DateTime->now; + (24 * 3600) - (( 60 * $dt->minute ) + $dt->second); +} + +1; diff --git a/lib/Plack/Middleware/Throttle/Hourly.pm b/lib/Plack/Middleware/Throttle/Hourly.pm new file mode 100644 index 0000000..818d70b --- /dev/null +++ b/lib/Plack/Middleware/Throttle/Hourly.pm @@ -0,0 +1,17 @@ +package Plack::Middleware::Throttle::Hourly; + +use Moose; +extends 'Plack::Middleware::Throttle::Limiter'; + +sub cache_key { + my ( $self, $env ) = @_; + $self->client_identifier($env) . "_" + . DateTime->now->strftime("%Y-%m-%d-%H"); +} + +sub reset_time { + my $dt = DateTime->now; + 3600 - (( 60 * $dt->minute ) + $dt->second); +} + +1; diff --git a/lib/Plack/Middleware/Throttle/Interval.pm b/lib/Plack/Middleware/Throttle/Interval.pm new file mode 100644 index 0000000..cbe7d59 --- /dev/null +++ b/lib/Plack/Middleware/Throttle/Interval.pm @@ -0,0 +1,12 @@ +package Plack::Middleware::Throttle::Interval; + +use Moose; +extends 'Plack::Middleware::Throttle'; + +sub allowed { +} + +sub cache_key { +} + +1; diff --git a/lib/Plack/Middleware/Throttle/Limiter.pm b/lib/Plack/Middleware/Throttle/Limiter.pm new file mode 100644 index 0000000..626732d --- /dev/null +++ b/lib/Plack/Middleware/Throttle/Limiter.pm @@ -0,0 +1,21 @@ +package Plack::Middleware::Throttle::Limiter; + +use Moose; +extends 'Plack::Middleware::Throttle'; + +sub request_done { + my ( $self, $env ) = @_; + my $key = $self->cache_key($env); + + $self->backend->incr($key); + + my $request_done = $self->backend->get($key); + + if ( !$request_done ) { + $self->backend->set( $key, 1 ); + } + + $request_done; +} + +1; |
