diff options
author | franck cuny <franck@lumberjaph.net> | 2010-03-30 15:36:02 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-03-30 15:36:02 +0200 |
commit | 397a9dc9fcfd3e61de12bcf6e7e39ca660155348 (patch) | |
tree | a397a4bfc708f156b7c13eec3762320f7e5b9993 | |
parent | remove Moose, update call method, return remaining seconds instead of epoch t... (diff) | |
download | plack-middleware-apiratelimit-397a9dc9fcfd3e61de12bcf6e7e39ca660155348.tar.gz |
update POD, fix redis backend
-rw-r--r-- | lib/Plack/Middleware/APIRateLimit.pm | 14 | ||||
-rw-r--r-- | lib/Plack/Middleware/APIRateLimit/Backend.pm | 4 | ||||
-rw-r--r-- | lib/Plack/Middleware/APIRateLimit/Backend/Redis.pm | 31 |
3 files changed, 32 insertions, 17 deletions
diff --git a/lib/Plack/Middleware/APIRateLimit.pm b/lib/Plack/Middleware/APIRateLimit.pm index 5819bee..6a86446 100644 --- a/lib/Plack/Middleware/APIRateLimit.pm +++ b/lib/Plack/Middleware/APIRateLimit.pm @@ -1,5 +1,8 @@ package Plack::Middleware::APIRateLimit; +use strict; +use warnings; + use Carp; use Scalar::Util; use Plack::Util; @@ -54,6 +57,11 @@ sub call { $self->backend->incr($key); my $request_done = $self->backend->get($key); + if (!$request_done) { + $self->backend->set($key, 1); + $request_done = 1; + } + return $self->over_rate_limit() if $request_done > $self->requests_per_hour; @@ -117,6 +125,9 @@ Plack::Middleware::APIRateLimit - A Plack Middleware for API Throttling enable "APIRateLimit", requests_per_hour => 2, backend => "Hash"; # or enable "APIRateLimit", requests_per_hour => 2, backend => ["Redis", {port => 6379, server => '127.0.0.1'}]; + # or + enable "APIRateLimit", request_per_hour => 2, backend => Redis->new(server => '127.0.0.1:6379'); + sub { [ '200', [ 'Content-Type' => 'text/html' ], ['hello world'] ] }; }; @@ -151,7 +162,8 @@ When will the counter be reseted (in epoch) =item B<backend> Which backend to use. Currently only Hash and Redis are supported. If no -backend is specified, Hash is used by default. +backend is specified, Hash is used by default. Backend must implement B<set>, +B<get> and B<incr>. =item B<requests_per_hour> diff --git a/lib/Plack/Middleware/APIRateLimit/Backend.pm b/lib/Plack/Middleware/APIRateLimit/Backend.pm index 5628e86..c88d654 100644 --- a/lib/Plack/Middleware/APIRateLimit/Backend.pm +++ b/lib/Plack/Middleware/APIRateLimit/Backend.pm @@ -11,4 +11,8 @@ sub get { confess "Backend must implement a get method"; } +sub set { + confess "Backend must implement a set method"; +} + 1; diff --git a/lib/Plack/Middleware/APIRateLimit/Backend/Redis.pm b/lib/Plack/Middleware/APIRateLimit/Backend/Redis.pm index e647f6a..2bb6251 100644 --- a/lib/Plack/Middleware/APIRateLimit/Backend/Redis.pm +++ b/lib/Plack/Middleware/APIRateLimit/Backend/Redis.pm @@ -3,41 +3,40 @@ package Plack::Middleware::APIRateLimit::Backend::Redis; use Moose; extends 'Plack::Middleware::APIRateLimit::Backend'; -use AnyEvent::Redis; +use Redis; has store => ( is => 'rw', - isa => 'AnyEvent::Redis', + isa => 'Redis', lazy => 1, default => sub { - return AnyEvent::Redis->new( - host => '127.0.0.1', - port => 6378, + return Redis->new( + server => '127.0.0.1:6379' ); } ); sub BUILD { - my $self = shift; - if (scalar @_) { - $self->store(AnyEvent::Redis->new(@_)); + my ( $self, $opt ) = @_; + if ($opt) { + $self->store( Redis->new(%$opt) ); } return $self; } sub get { my ( $self, $key ) = @_; - my $val = $self->store->get($key)->recv; - if ( !$val ) { - $self->store->set( $key => 1 )->recv; - $val = 1; - } - return $val; + $self->store->get($key); +} + +sub set { + my ($self, $key, $value) = @_; + $self->store->set($key, $value); } sub incr { - my ($self, $key) = @_; - return $self->store->incr($key)->recv; + my ( $self, $key ) = @_; + $self->store->incr($key); } 1; |