Gungho/Component/Throttle/Domain.pm untested

filter: Statementstmt 0.0% 0 / 40 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 68 CC 6 SCAR 25.0
CC
6
Cov
0%
throttle
20.0
setup
12.0
BEGIN
2.0
CRAP 12.2
1 # $Id: /mirror/gungho/lib/Gungho/Component/Throttle/Domain.pm 8775 2007-11-08T09:53:44.496906Z lestrrat $
2 #
3 # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4 # All rights reserved.
5  
6 package Gungho::Component::Throttle::Domain;
7 use strict;
8 use warnings;
9 use base qw(Gungho::Component::Throttle::Throttler);
10  
11 __PACKAGE__->mk_classdata($_) for qw(matcher);
12  
13 sub setup
14 {
15 my $self = shift;
16  
17 my $config = $self->config->{throttle}{domain};
18  
19 $self->prepare_throttler(
20 map { ($_ => $config->{$_}) }
21 qw(max_items interval db_file throttler cache)
22 );
23  
24 my $domains = $config->{domains} || [];
25 my $matcher;
26 if (@$domains) {
27 my $sub = <<EOSUB;
28 sub {
29 my \$request = shift;
30 my \$url = \$request->url;
31 my \$host = \$url->host;
32 EOSUB
33  
34 foreach my $d (@$domains) {
35 if (my $re = $d->{match}) {
36 # protect ourselves from "/"
37 $re =~ s/\//\\\//g;
38 $sub .= " (\$host =~ /$re/) and return 1;\n";
39 }
40 }
41 $sub .= "\nreturn 0;\n}";
42  
43 $matcher = eval $sub or die;
44 }
45 $self->matcher($matcher);
46  
47 $self->next::method(@_);
48 }
49  
50 sub throttle
51 {
52 my $c = shift;
53 my $request = shift;
54  
55 my $do_throttle = 1;
56 my $code = $c->matcher;
57 if ($code) {
58 $do_throttle = $code->($request);
59 }
60  
61 if ($do_throttle) {
62 my $t = $c->throttler;
63 if (! $t->try_push(key => $request->notes('original_host') || $request->url->host)) {
64 $c->log->debug( $request->url . " throttled by Throttle::Domain" );
65 return ();
66 }
67 }
68 return $c->next::method($request);
69 }
70  
71 1;
72  
73 __END__