| 1 | package PAGI::App::Throttle; | ||||||||||||||||||||||||||||||||||||||
| 2 | 1 | $PAGI::App::Throttle::VERSION = '0.002001'; | |||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | PAGI::App::Throttle - Rate-limited request processing | ||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | use PAGI::App::Throttle; | ||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | my $app = PAGI::App::Throttle->new( | ||||||||||||||||||||||||||||||||||||||
| 16 | app => $inner_app, | ||||||||||||||||||||||||||||||||||||||
| 17 | rate => 10, # requests per second | ||||||||||||||||||||||||||||||||||||||
| 18 | burst => 20, # max burst | ||||||||||||||||||||||||||||||||||||||
| 19 | key_for => sub { $_[0]->{client}[0] }, # key by IP | ||||||||||||||||||||||||||||||||||||||
| 20 | )->to_app; | ||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||
| 22 | =cut | ||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||
| 24 | # Token bucket state per key | ||||||||||||||||||||||||||||||||||||||
| 25 | 1 | my %buckets; | |||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | sub new { | ||||||||||||||||||||||||||||||||||||||
| 28 | 4 | ▶ | my ($class, %args) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||
| 30 | return bless { | ||||||||||||||||||||||||||||||||||||||
| 31 | app => $args{app}, | ||||||||||||||||||||||||||||||||||||||
| 32 | rate => $args{rate} // 10, | ||||||||||||||||||||||||||||||||||||||
| 33 | burst => $args{burst} // ($args{rate} // 10), | ||||||||||||||||||||||||||||||||||||||
| 34 | key_for => $args{key_for}, | ||||||||||||||||||||||||||||||||||||||
| 35 | on_limit => $args{on_limit}, | ||||||||||||||||||||||||||||||||||||||
| 36 | 4 | ▶ | headers => $args{headers} // 1, | ||||||||||||||||||||||||||||||||||||
|
Condition44%4/9
$args{"rate"} // 10
$args{"burst"} // ($args{"rate"} // 10)
$args{"rate"} // 10
$args{"headers"} // 1
MC/DC0%0/2
$args{"burst"} // ($args{"rate"} // 10)
$args{"burst"}
$args{"rate"}
MC/DC100%1/1
$args{"headers"} // 1
$args{"headers"}
Truth table33%1/3
A // B
A= $args{"burst"}B= $args{"rate"}
Truth table100%2/2
A
A= $args{"headers"}
| |||||||||||||||||||||||||||||||||||||||
| 37 | }, $class; | ||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||
| 40 | sub to_app { | ||||||||||||||||||||||||||||||||||||||
| 41 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
to_app: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | 4 | my $app = $self->{app}; | |||||||||||||||||||||||||||||||||||||
| 44 | 4 | my $rate = $self->{rate}; | |||||||||||||||||||||||||||||||||||||
| 45 | 4 | my $burst = $self->{burst}; | |||||||||||||||||||||||||||||||||||||
| 46 | 4 | my $key_for = $self->{key_for}; | |||||||||||||||||||||||||||||||||||||
| 47 | 4 | my $on_limit = $self->{on_limit}; | |||||||||||||||||||||||||||||||||||||
| 48 | 4 | my $add_headers = $self->{headers}; | |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | 7 | ▶ | return async sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||
| 51 | 7 | my ($scope, $receive, $send) = @_; | |||||||||||||||||||||||||||||||||||||
| 52 | 7 | ▶ | my $key = $key_for ? $key_for->($scope) : 'global'; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 53 | 7 | ▶ | $key //= 'global'; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$key //= "global"
MC/DC0%0/1
$key //= "global"
$key
Truth table50%1/2
A
A= $key
| |||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||
| 55 | # Initialize bucket | ||||||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | $buckets{$key} //= { | ||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$buckets{$key} //= {"tokens", $burst, "last_time", time}
MC/DC100%1/1
$buckets{$key} //= {"tokens", $burst, "last_time", time}
$buckets{$key}
Truth table100%2/2
A
A= $buckets{$key}
| |||||||||||||||||||||||||||||||||||||||
| 57 | tokens => $burst, | ||||||||||||||||||||||||||||||||||||||
| 58 | last_time => time(), | ||||||||||||||||||||||||||||||||||||||
| 59 | }; | ||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||
| 61 | 7 | my $bucket = $buckets{$key}; | |||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | # Refill tokens based on elapsed time | ||||||||||||||||||||||||||||||||||||||
| 64 | 7 | my $now = time(); | |||||||||||||||||||||||||||||||||||||
| 65 | 7 | my $elapsed = $now - $bucket->{last_time}; | |||||||||||||||||||||||||||||||||||||
| 66 | 7 | $bucket->{tokens} += $elapsed * $rate; | |||||||||||||||||||||||||||||||||||||
| 67 | 7 | ▶ | $bucket->{tokens} = $burst if $bucket->{tokens} > $burst; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 68 | 7 | $bucket->{last_time} = $now; | |||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||
| 70 | # Check if request allowed | ||||||||||||||||||||||||||||||||||||||
| 71 | 7 | ▶ | if ($bucket->{tokens} >= 1) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 72 | 5 | $bucket->{tokens} -= 1; | |||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||
| 74 | # Wrap send to add rate limit headers | ||||||||||||||||||||||||||||||||||||||
| 75 | 5 | my $wrapped_send = $send; | |||||||||||||||||||||||||||||||||||||
| 76 | 5 | ▶ | if ($add_headers) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 77 | 10 | ▶ | $wrapped_send = async sub { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||
| 78 | 10 | my ($event) = @_; | |||||||||||||||||||||||||||||||||||||
| 79 | 10 | ▶ | if ($event->{type} eq 'http.response.start') { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 80 | 5 | ▶ | my @headers = @{$event->{headers} // []}; | ||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$event->{"headers"} // []
MC/DC0%0/1
$event->{"headers"} // []
$event->{"headers"}
Truth table50%1/2
A
A= $event->{"headers"}
| |||||||||||||||||||||||||||||||||||||||
| 81 | 5 | push @headers, ['x-ratelimit-limit', $burst]; | |||||||||||||||||||||||||||||||||||||
| 82 | 5 | push @headers, ['x-ratelimit-remaining', int($bucket->{tokens})]; | |||||||||||||||||||||||||||||||||||||
| 83 | 5 | push @headers, ['x-ratelimit-reset', int($now + ($burst - $bucket->{tokens}) / $rate)]; | |||||||||||||||||||||||||||||||||||||
| 84 | 5 | $event = { %$event, headers => \@headers }; | |||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||
| 86 | 10 | await $send->($event); | |||||||||||||||||||||||||||||||||||||
| 87 | 5 | }; | |||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||
| 90 | 5 | await $app->($scope, $receive, $wrapped_send); | |||||||||||||||||||||||||||||||||||||
| 91 | } else { | ||||||||||||||||||||||||||||||||||||||
| 92 | # Rate limited | ||||||||||||||||||||||||||||||||||||||
| 93 | 2 | my $retry_after = int((1 - $bucket->{tokens}) / $rate) + 1; | |||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | if ($on_limit) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 96 | 0 | await $on_limit->($scope, $receive, $send, $retry_after); | |||||||||||||||||||||||||||||||||||||
| 97 | } else { | ||||||||||||||||||||||||||||||||||||||
| 98 | 2 | my @headers = ( | |||||||||||||||||||||||||||||||||||||
| 99 | ['content-type', 'text/plain'], | ||||||||||||||||||||||||||||||||||||||
| 100 | ['retry-after', $retry_after], | ||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($add_headers) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 104 | 2 | push @headers, ['x-ratelimit-limit', $burst]; | |||||||||||||||||||||||||||||||||||||
| 105 | 2 | push @headers, ['x-ratelimit-remaining', 0]; | |||||||||||||||||||||||||||||||||||||
| 106 | 2 | push @headers, ['x-ratelimit-reset', int($now + $retry_after)]; | |||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | await $send->({ | ||||||||||||||||||||||||||||||||||||||
| 110 | type => 'http.response.start', | ||||||||||||||||||||||||||||||||||||||
| 111 | status => 429, | ||||||||||||||||||||||||||||||||||||||
| 112 | headers => \@headers, | ||||||||||||||||||||||||||||||||||||||
| 113 | 2 | }); | |||||||||||||||||||||||||||||||||||||
| 114 | 2 | await $send->({ | |||||||||||||||||||||||||||||||||||||
| 115 | type => 'http.response.body', | ||||||||||||||||||||||||||||||||||||||
| 116 | body => 'Too Many Requests', | ||||||||||||||||||||||||||||||||||||||
| 117 | more => 0, | ||||||||||||||||||||||||||||||||||||||
| 118 | }); | ||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||
| 121 | 4 | }; | |||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||
| 124 | # Class method to reset a key's bucket | ||||||||||||||||||||||||||||||||||||||
| 125 | sub reset { | ||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | my ($class, $key) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reset: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||
| 128 | 0 | delete $buckets{$key}; | |||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||
| 131 | # Class method to reset all buckets | ||||||||||||||||||||||||||||||||||||||
| 132 | sub reset_all { | ||||||||||||||||||||||||||||||||||||||
| 133 | 4 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
reset_all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||
| 135 | 4 | %buckets = (); | |||||||||||||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | # Class method to get bucket info | ||||||||||||||||||||||||||||||||||||||
| 139 | sub info { | ||||||||||||||||||||||||||||||||||||||
| 140 | 0 | ▶ | my ($class, $key) = @_; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
info: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | return undef unless $buckets{$key}; | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 143 | return { | ||||||||||||||||||||||||||||||||||||||
| 144 | tokens => $buckets{$key}{tokens}, | ||||||||||||||||||||||||||||||||||||||
| 145 | last_time => $buckets{$key}{last_time}, | ||||||||||||||||||||||||||||||||||||||
| 146 | 0 | }; | |||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||
| 149 | 1 | 1; | |||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||||||||