1 |
package transferqueue; |
2 |
|
3 |
my @reserve = ( |
4 |
[ 1_200_000, 1], |
5 |
[ 3_000_000, 1], |
6 |
[ 75_000_000, 1], |
7 |
); |
8 |
|
9 |
sub new { |
10 |
my $class = shift; |
11 |
my $self = bless { |
12 |
slots => $_[0], |
13 |
lastspb => 0, |
14 |
avgspb => 0, |
15 |
}, $class; |
16 |
$self->{reschedule} = Event->timer( |
17 |
after => 10, |
18 |
interval => 3, |
19 |
cb => sub { $self->wake_next }, |
20 |
); |
21 |
$self; |
22 |
} |
23 |
|
24 |
sub start_transfer { |
25 |
my $self = shift; |
26 |
my $size = $_[0]; |
27 |
|
28 |
my $transfer = bless { |
29 |
queue => $self, |
30 |
time => $::NOW, |
31 |
size => $size, |
32 |
coro => $Coro::current, |
33 |
started => 0, |
34 |
}, transfer::; |
35 |
|
36 |
push @{$self->{wait}}, $transfer; |
37 |
|
38 |
$self->wake_next; |
39 |
|
40 |
$transfer; |
41 |
} |
42 |
|
43 |
sub sort { |
44 |
my @queue = grep $_, @{$_[0]{wait}}; |
45 |
|
46 |
$_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue; |
47 |
|
48 |
$_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue]; |
49 |
|
50 |
Scalar::Util::weaken $_ for @{$_[0]{wait}}; |
51 |
} |
52 |
|
53 |
sub wake_next { |
54 |
my $self = shift; |
55 |
|
56 |
$self->sort; |
57 |
|
58 |
while (@{$self->{wait}}) { |
59 |
my $size = $self->{wait}[0]{size}; |
60 |
my $min = 0; |
61 |
for (@reserve) { |
62 |
last if $size <= $_->[0]; |
63 |
$min += $_->[1]; |
64 |
} |
65 |
last unless $self->{slots} > $min; |
66 |
my $transfer = shift @{$self->{wait}}; |
67 |
$self->{lastspb} = $transfer->{spb}; |
68 |
$self->{avgspb} ||= $transfer->{spb}; |
69 |
$self->{avgspb} = $self->{avgspb} * 0.95 + $transfer->{spb} * 0.05; |
70 |
$self->{started}++; |
71 |
$transfer->wake; |
72 |
last; |
73 |
} |
74 |
} |
75 |
|
76 |
sub waiters { |
77 |
$_[0]->sort; |
78 |
@{$_[0]{wait}}; |
79 |
} |
80 |
|
81 |
sub DESTROY { |
82 |
my $self = shift; |
83 |
|
84 |
$self->{reschedule}->cancel; |
85 |
} |
86 |
|
87 |
package transfer; |
88 |
|
89 |
use Coro::Timer (); |
90 |
|
91 |
sub wake { |
92 |
my $self = shift; |
93 |
|
94 |
$self->{alloc} = 1; |
95 |
$self->{queue}{slots}--; |
96 |
$self->{wake} and $self->{wake}->ready; |
97 |
} |
98 |
|
99 |
sub try { |
100 |
my $self = shift; |
101 |
|
102 |
$self->{alloc} || do { |
103 |
my $timeout = Coro::Timer::timeout $_[0]; |
104 |
local $self->{wake} = $self->{coro}; |
105 |
|
106 |
Coro::schedule; |
107 |
|
108 |
$self->{alloc}; |
109 |
} |
110 |
} |
111 |
|
112 |
sub DESTROY { |
113 |
my $self = shift; |
114 |
|
115 |
if ($self->{alloc}) { |
116 |
$self->{queue}{slots}++; |
117 |
$self->{queue}->wake_next; |
118 |
} |
119 |
} |
120 |
|
121 |
package conn; |
122 |
|
123 |
our %blockuri; |
124 |
our $blockref; |
125 |
|
126 |
sub read_blockuri { |
127 |
local *B; |
128 |
my %group; |
129 |
%blockuri = (); |
130 |
if (open B, "<blockuri") { |
131 |
while (<B>) { |
132 |
chomp; |
133 |
if (/^group\s+(\S+)\s+(.*)/i) { |
134 |
$group{$1} = [split /\s+/, $2]; |
135 |
} elsif (/^!([^\t]*)\t\s*(.*)/) { |
136 |
my $g = $1; |
137 |
my @r; |
138 |
for (split /\s+/, $2) { |
139 |
push @r, $group{$_} ? @{$group{$_}} : $_; |
140 |
} |
141 |
print "not($g) => (@r)\n"; |
142 |
push @{$blockuri{$_}}, $g for @r; |
143 |
push @blockuri, [qr/$g/i, \@r]; |
144 |
} elsif (/\S/) { |
145 |
print "blockuri: unparsable line: $_\n"; |
146 |
} |
147 |
} |
148 |
for (keys %blockuri) { |
149 |
my $qr = join ")|(?:", @{$blockuri{$_}}; |
150 |
$blockuri{$_} = qr{(?:$qr)}i; |
151 |
} |
152 |
} else { |
153 |
print "no blockuri\n"; |
154 |
} |
155 |
} |
156 |
|
157 |
sub read_blockref { |
158 |
local *B; |
159 |
my @blockref; |
160 |
if (open B, "<blockreferer") { |
161 |
while (<B>) { |
162 |
chomp; |
163 |
if (/^([^\t]*)\t\s*(.*)/) { |
164 |
push @blockref, $1; |
165 |
} elsif (/\S/) { |
166 |
print "blockref: unparsable line: $_\n"; |
167 |
} |
168 |
} |
169 |
$blockref = join ")|(?:", @blockref; |
170 |
$blockref = qr{^(?:$blockref)}i; |
171 |
} else { |
172 |
print "no blockref\n"; |
173 |
$blockref = qr{^x^}; |
174 |
} |
175 |
} |
176 |
|
177 |
read_blockuri; |
178 |
read_blockref; |
179 |
|
180 |
use Tie::Cache; |
181 |
tie %whois_cache, Tie::Cache::, 32; |
182 |
|
183 |
sub access_check { |
184 |
my $self = shift; |
185 |
|
186 |
my $ref = $self->{h}{referer}; |
187 |
my $uri = $self->{path}; |
188 |
my %disallow; |
189 |
|
190 |
$self->err_block_referer |
191 |
if $self->{h}{referer} =~ $blockref; |
192 |
|
193 |
my $whois = $whois_cache{$self->{remote_addr}} |
194 |
||= netgeo::ip_request($self->{remote_addr}); |
195 |
|
196 |
my $country = "XX"; |
197 |
|
198 |
if ($whois =~ /^\*cy: (\S+)/m) { |
199 |
$country = uc $1; |
200 |
} else { |
201 |
$self->slog(9, "no country($whois)"); |
202 |
} |
203 |
|
204 |
$self->{country} = $country; |
205 |
|
206 |
$self->err_block_country($whois) |
207 |
if $self->{path} =~ $blockuri{$country}; |
208 |
} |
209 |
|
210 |
1; |