ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/access.pl
(Generate patch)

Comparing Coro/myhttpd/access.pl (file contents):
Revision 1.8 by root, Sun Aug 26 14:55:46 2001 UTC vs.
Revision 1.24 by root, Tue Dec 4 02:46:29 2001 UTC

1package transferqueue;
2
3my @reserve = (
4 [ 1_000_000, 1],
5 [ 10_000_000, 1],
6);
7
8sub new {
9 my $class = shift;
10 bless {
11 slots => $_[0],
12 lastspb => 0,
13 avgspb => 0,
14 }, $class;
15}
16
17sub 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
36sub 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
46sub 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
69sub waiters {
70 $_[0]->sort;
71 @{$_[0]{wait}};
72}
73
74package transfer;
75
76use Coro::Timer ();
77
78sub wake {
79 my $self = shift;
80
81 $self->{alloc} = 1;
82 $self->{queue}{slots}--;
83 $self->{wake} and $self->{wake}->ready;
84}
85
86sub 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
99sub DESTROY {
100 my $self = shift;
101
102 if ($self->{alloc}) {
103 $self->{queue}{slots}++;
104 $self->{queue}->wake_next;
105 }
106}
107
1package conn; 108package conn;
2 109
3our %blockuri; 110our %blockuri;
4our $blockref; 111our $blockref;
5 112
56 163
57read_blockuri; 164read_blockuri;
58read_blockref; 165read_blockref;
59 166
60use Tie::Cache; 167use Tie::Cache;
61tie %whois_cache, Tie::Cache::, $::MAX_CONNECTS * 1.5; 168tie %whois_cache, Tie::Cache::, 32;
62 169
63sub access_check { 170sub access_check {
64 my $self = shift; 171 my $self = shift;
65 172
66 my $ref = $self->{h}{referer}; 173 my $ref = $self->{h}{referer};
69 176
70 $self->err_block_referer 177 $self->err_block_referer
71 if $self->{h}{referer} =~ $blockref; 178 if $self->{h}{referer} =~ $blockref;
72 179
73 my $whois = $whois_cache{$self->{remote_addr}} 180 my $whois = $whois_cache{$self->{remote_addr}}
74 ||= ::ip_request($self->{remote_addr}); 181 ||= netgeo::ip_request($self->{remote_addr});
75 182
76 my $country = "XX"; 183 my $country = "XX";
77 184
78 if ($whois =~ /^\*cy: (\S+)/m) { 185 if ($whois =~ /^\*cy: (\S+)/m) {
79 $country = uc $1; 186 $country = uc $1;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines