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

Comparing cvsroot/Coro/myhttpd/access.pl (file contents):
Revision 1.8 by root, Sun Aug 26 14:55:46 2001 UTC vs.
Revision 1.21 by root, Mon Dec 3 05:52:37 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines