1 | package transferqueue; |
|
|
2 | |
|
|
3 | my @reserve = ( |
|
|
4 | [ 1_200_000, 2], |
|
|
5 | [ 8_000_000, 1], |
|
|
6 | [ 75_000_000, 1], |
|
|
7 | ); |
|
|
8 | |
|
|
9 | sub new { |
|
|
10 | my $class = shift; |
|
|
11 | my $self = bless { |
|
|
12 | slots => 1, |
|
|
13 | maxsize => 0, |
|
|
14 | lastspb => 0, |
|
|
15 | avgspb => 0, |
|
|
16 | @_, |
|
|
17 | }, $class; |
|
|
18 | $self->{reschedule} = Event->timer( |
|
|
19 | after => 10, |
|
|
20 | interval => 3, |
|
|
21 | cb => sub { $self->wake_next }, |
|
|
22 | ); |
|
|
23 | $self; |
|
|
24 | } |
|
|
25 | |
|
|
26 | sub start_transfer { |
|
|
27 | my $self = shift; |
|
|
28 | my $size = $_[0]; |
|
|
29 | |
|
|
30 | if ($self->{maxsize} && $self->{maxsize} < $size) { |
|
|
31 | $size = $self->{maxsize}; |
|
|
32 | } |
|
|
33 | |
|
|
34 | my $transfer = bless { |
|
|
35 | queue => $self, |
|
|
36 | time => $::NOW, |
|
|
37 | size => $size, |
|
|
38 | coro => $Coro::current, |
|
|
39 | started => 0, |
|
|
40 | }, transfer::; |
|
|
41 | |
|
|
42 | push @{$self->{wait}}, $transfer; |
|
|
43 | |
|
|
44 | $self->wake_next; |
|
|
45 | |
|
|
46 | $transfer; |
|
|
47 | } |
|
|
48 | |
|
|
49 | sub sort { |
|
|
50 | my @queue = grep $_, @{$_[0]{wait}}; |
|
|
51 | |
|
|
52 | $_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue; |
|
|
53 | |
|
|
54 | $_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue]; |
|
|
55 | |
|
|
56 | Scalar::Util::weaken $_ for @{$_[0]{wait}}; |
|
|
57 | } |
|
|
58 | |
|
|
59 | sub wake_next { |
|
|
60 | my $self = shift; |
|
|
61 | |
|
|
62 | $self->sort; |
|
|
63 | |
|
|
64 | while (@{$self->{wait}}) { |
|
|
65 | my $size = $self->{wait}[0]{size}; |
|
|
66 | my $min = 0; |
|
|
67 | for (@reserve) { |
|
|
68 | last if $size <= $_->[0]; |
|
|
69 | $min += $_->[1]; |
|
|
70 | } |
|
|
71 | last unless $self->{slots} > $min; |
|
|
72 | my $transfer = shift @{$self->{wait}}; |
|
|
73 | $self->{lastspb} = $transfer->{spb}; |
|
|
74 | $self->{avgspb} = $self->{avgspb} * 0.99 + $transfer->{spb} * 0.01; |
|
|
75 | $self->{started}++; |
|
|
76 | $transfer->wake; |
|
|
77 | last; |
|
|
78 | } |
|
|
79 | } |
|
|
80 | |
|
|
81 | sub waiters { |
|
|
82 | $_[0]->sort; |
|
|
83 | @{$_[0]{wait}}; |
|
|
84 | } |
|
|
85 | |
|
|
86 | sub DESTROY { |
|
|
87 | my $self = shift; |
|
|
88 | |
|
|
89 | $self->{reschedule}->cancel; |
|
|
90 | } |
|
|
91 | |
|
|
92 | package transfer; |
|
|
93 | |
|
|
94 | use Coro::Timer (); |
|
|
95 | |
|
|
96 | sub wake { |
|
|
97 | my $self = shift; |
|
|
98 | |
|
|
99 | $self->{alloc} = 1; |
|
|
100 | $self->{queue}{slots}--; |
|
|
101 | $self->{wake} and $self->{wake}->ready; |
|
|
102 | } |
|
|
103 | |
|
|
104 | sub try { |
|
|
105 | my $self = shift; |
|
|
106 | |
|
|
107 | $self->{alloc} || do { |
|
|
108 | my $timeout = Coro::Timer::timeout $_[0]; |
|
|
109 | local $self->{wake} = $self->{coro}; |
|
|
110 | |
|
|
111 | Coro::schedule; |
|
|
112 | |
|
|
113 | $self->{alloc}; |
|
|
114 | } |
|
|
115 | } |
|
|
116 | |
|
|
117 | sub DESTROY { |
|
|
118 | my $self = shift; |
|
|
119 | |
|
|
120 | if ($self->{alloc}) { |
|
|
121 | $self->{queue}{slots}++; |
|
|
122 | $self->{queue}->wake_next; |
|
|
123 | } |
|
|
124 | } |
|
|
125 | |
|
|
126 | package conn; |
1 | package conn; |
127 | |
2 | |
128 | our %blockuri; |
3 | our %blockuri; |
129 | our $blockref; |
4 | our $blockref; |
130 | |
5 | |
… | |
… | |
193 | my %disallow; |
68 | my %disallow; |
194 | |
69 | |
195 | $self->err_block_referer |
70 | $self->err_block_referer |
196 | if $self->{h}{referer} =~ $blockref; |
71 | if $self->{h}{referer} =~ $blockref; |
197 | |
72 | |
198 | my $whois = $whois_cache{$self->{remote_addr}} |
73 | my $ra = $self->{remote_addr}; |
199 | ||= netgeo::ip_request($self->{remote_addr}); |
74 | my $whois = $whois_cache{$ra} ||= netgeo::ip_request($ra); |
200 | |
75 | |
201 | my $country = "XX"; |
76 | my $country = "XX"; |
202 | |
77 | |
203 | if ($whois =~ /^\*cy: (\S+)/m) { |
78 | if ($whois =~ /^\*cy: (\S+)/m) { |
204 | $country = uc $1; |
79 | $country = uc $1; |