| 1 | # $Id: /mirror/gungho/lib/Gungho/Component/Throttle/Simple.pm 31301 2007-11-29T11:51:44.807693Z lestrrat $ | ||
| 2 | # | ||
| 3 | # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp> | ||
| 4 | # All rights reserved. | ||
| 5 | |||
| 6 | package Gungho::Component::Throttle::Simple; | ||
| 7 | use strict; | ||
| 8 | use warnings; | ||
| 9 | use base qw(Gungho::Component::Throttle::Throttler); | ||
| 10 | |||
| 11 | sub setup | ||
| 12 | { | ||
| 13 | my $self = shift; | ||
| 14 | |||
| 15 | my $config = $self->config->{throttle}{simple}; | ||
| 16 | |||
| 17 | $self->prepare_throttler( | ||
| 18 | map { ($_ => $config->{$_}) } | ||
| 19 | qw(max_items interval db_file throttler cache) | ||
| 20 | ); | ||
| 21 | $self->next::method(@_); | ||
| 22 | } | ||
| 23 | |||
| 24 | sub throttle | ||
| 25 | { | ||
| 26 | my $c = shift; | ||
| 27 | my $request = shift; | ||
| 28 | my $t = $c->throttler; | ||
| 29 | |||
| 30 | if ( ! $t->try_push() ) { | ||
| 31 | $c->log->debug("[THROTTLE] " . $request->uri . " throttled by Throttle::Simple"); | ||
| 32 | return (); | ||
| 33 | } | ||
| 34 | |||
| 35 | return $c->next::method($request); | ||
| 36 | } | ||
| 37 | |||
| 38 | 1; | ||
| 39 | |||
| 40 | __END__ |