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