|
|
1 | package transferqueue; |
|
|
2 | |
|
|
3 | sub new { |
|
|
4 | my $class = shift; |
|
|
5 | bless { |
|
|
6 | slots => $_[0], |
|
|
7 | lastspb => 0, |
|
|
8 | }, $class; |
|
|
9 | } |
|
|
10 | |
|
|
11 | sub start_transfer { |
|
|
12 | my $self = shift; |
|
|
13 | my $size = $_[0]; |
|
|
14 | |
|
|
15 | my $trans = bless { |
|
|
16 | queue => $self, |
|
|
17 | time => $::NOW, |
|
|
18 | size => $size, |
|
|
19 | coro => $Coro::current, |
|
|
20 | }, transfer::; |
|
|
21 | |
|
|
22 | push @{$self->{wait}}, $trans; |
|
|
23 | Scalar::Util::weaken($self->{wait}[-1]); |
|
|
24 | |
|
|
25 | $self->wake_next; |
|
|
26 | |
|
|
27 | $trans; |
|
|
28 | } |
|
|
29 | |
|
|
30 | sub wake_next { |
|
|
31 | my $self = shift; |
|
|
32 | |
|
|
33 | $self->sort; |
|
|
34 | |
|
|
35 | while($self->{slots} && @{$self->{wait}}) { |
|
|
36 | my $transfer = shift @{$self->{wait}}; |
|
|
37 | if ($transfer) { |
|
|
38 | $self->{lastspb} = $transfer->{spb}; |
|
|
39 | $transfer->wake; |
|
|
40 | last; |
|
|
41 | } |
|
|
42 | } |
|
|
43 | } |
|
|
44 | |
|
|
45 | sub sort { |
|
|
46 | $_[0]{wait} = [ |
|
|
47 | sort { $b->{spb} <=> $a->{spb} } |
|
|
48 | grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ } |
|
|
49 | @{$_[0]{wait}} |
|
|
50 | ]; |
|
|
51 | } |
|
|
52 | |
|
|
53 | sub waiters { |
|
|
54 | $_[0]->sort; |
|
|
55 | @{$_[0]{wait}}; |
|
|
56 | } |
|
|
57 | |
|
|
58 | package transfer; |
|
|
59 | |
|
|
60 | use Coro::Timer (); |
|
|
61 | |
|
|
62 | sub wake { |
|
|
63 | my $self = shift; |
|
|
64 | |
|
|
65 | $self->{alloc} = 1; |
|
|
66 | $self->{queue}{slots}--; |
|
|
67 | $self->{wake} and $self->{wake}->ready; |
|
|
68 | } |
|
|
69 | |
|
|
70 | sub try { |
|
|
71 | my $self = shift; |
|
|
72 | |
|
|
73 | $self->{alloc} || do { |
|
|
74 | my $timeout = Coro::Timer::timeout $_[0]; |
|
|
75 | local $self->{wake} = $self->{coro}; |
|
|
76 | |
|
|
77 | Coro::schedule; |
|
|
78 | |
|
|
79 | $self->{alloc}; |
|
|
80 | } |
|
|
81 | } |
|
|
82 | |
|
|
83 | sub DESTROY { |
|
|
84 | my $self = shift; |
|
|
85 | |
|
|
86 | if ($self->{alloc}) { |
|
|
87 | $self->{queue}{slots}++; |
|
|
88 | $self->{queue}->wake_next; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
1 | package conn; |
92 | package conn; |
2 | |
93 | |
3 | our %blockuri; |
94 | our %blockuri; |
4 | our $blockref; |
95 | our $blockref; |
5 | |
96 | |
… | |
… | |
56 | |
147 | |
57 | read_blockuri; |
148 | read_blockuri; |
58 | read_blockref; |
149 | read_blockref; |
59 | |
150 | |
60 | use Tie::Cache; |
151 | use Tie::Cache; |
61 | tie %whois_cache, Tie::Cache::, $::MAX_CONNECTS * 1.5; |
152 | tie %whois_cache, Tie::Cache::, 32; |
62 | |
153 | |
63 | sub access_check { |
154 | sub access_check { |
64 | my $self = shift; |
155 | my $self = shift; |
65 | |
156 | |
66 | my $ref = $self->{h}{referer}; |
157 | my $ref = $self->{h}{referer}; |
… | |
… | |
69 | |
160 | |
70 | $self->err_block_referer |
161 | $self->err_block_referer |
71 | if $self->{h}{referer} =~ $blockref; |
162 | if $self->{h}{referer} =~ $blockref; |
72 | |
163 | |
73 | my $whois = $whois_cache{$self->{remote_addr}} |
164 | my $whois = $whois_cache{$self->{remote_addr}} |
74 | ||= ::ip_request($self->{remote_addr}); |
165 | ||= netgeo::ip_request($self->{remote_addr}); |
75 | |
166 | |
76 | my $country = "XX"; |
167 | my $country = "XX"; |
77 | |
168 | |
78 | if ($whois =~ /^\*cy: (\S+)/m) { |
169 | if ($whois =~ /^\*cy: (\S+)/m) { |
79 | $country = uc $1; |
170 | $country = uc $1; |