1 | package tbf; |
1 | package tbf; |
2 | |
2 | |
3 | # kind of token-bucket-filter |
3 | # kind of token-bucket-filter |
4 | |
4 | |
5 | my $max_per_client = $::TBF_MAX_PER_CLIENT || 24000; |
5 | my $max_per_client = $::TBF_MAX_PER_CLIENT || 118000; |
6 | |
6 | |
7 | sub new { |
7 | sub new { |
8 | my $class = shift; |
8 | my $class = shift; |
9 | my %arg = @_; |
9 | my %arg = @_; |
10 | my $self = bless \%arg, $class; |
10 | my $self = bless \%arg, $class; |
11 | |
11 | |
12 | $self->{maxbucket} ||= $self->{rate} * 3; # max 3s bucket |
12 | $self->{maxbucket} ||= $self->{rate} * 60; # max bucket |
13 | $self->{minbucket} ||= $self->{rate}; # minimum bucket to share |
13 | $self->{minbucket} ||= $self->{rate}; # minimum bucket to share |
14 | $self->{interval} ||= $::BUFSIZE / $max_per_client; # good default interval |
14 | $self->{interval} ||= $::BUFSIZE / $max_per_client; # good default interval |
15 | |
15 | |
16 | if ($self->{rate}) { |
16 | if ($self->{rate}) { |
17 | $self->{w} = EV::periodic 0, $self->{interval}, undef, sub { |
17 | $self->{w} = EV::periodic 0, $self->{interval}, undef, sub { |
… | |
… | |
58 | sub request { |
58 | sub request { |
59 | my ($self, $bytes, $weight) = @_; |
59 | my ($self, $bytes, $weight) = @_; |
60 | |
60 | |
61 | $weight ||= 1; |
61 | $weight ||= 1; |
62 | |
62 | |
63 | my $coro = $Coro::current; |
|
|
64 | my $id = $_tbf_id++; |
63 | my $id = $_tbf_id++; |
|
|
64 | my $cb = Coro::rouse_cb; |
65 | |
65 | |
66 | $self->{waitw} += $weight; |
66 | $self->{waitw} += $weight; |
67 | $self->{waitq}{$id} = [$weight, 0, $bytes, sub { |
67 | $self->{waitq}{$id} = [$weight, 0, $bytes, sub { |
68 | delete $self->{waitq}{$id}; |
68 | delete $self->{waitq}{$id}; |
69 | $self->{waitw} -= $weight; |
69 | $self->{waitw} -= $weight; |
70 | $coro->ready; |
70 | &$cb; |
71 | }]; |
71 | }]; |
72 | |
72 | |
73 | Coro::schedule; |
73 | Coro::rouse_wait; |
74 | } |
74 | } |
75 | |
75 | |
76 | 1; |
76 | 1; |