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