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.13 by root, Thu Nov 29 02:30:04 2001 UTC vs.
Revision 1.20 by root, Mon Dec 3 05:40:09 2001 UTC

1package transferqueue; 1package transferqueue;
2 2
3sub new { 3sub new {
4 my $class = shift; 4 my $class = shift;
5 bless { 5 bless {
6 conns => $_[0], 6 slots => $_[0],
7 lastspb => 0,
8 avgspb => 0,
7 }, $class; 9 }, $class;
8} 10}
9 11
10sub start_transfer { 12sub start_transfer {
11 my $self = shift; 13 my $self = shift;
14 my $size = $_[0];
12 15
13 my $trans = bless [ $self, $Coro::current ], transfer::; 16 my $transfer = bless {
14 Scalar::Util::weaken($trans->[0]); 17 queue => $self,
18 time => $::NOW,
19 size => $size,
20 coro => $Coro::current,
21 }, transfer::;
15 22
16 push @{$self->{wait}}, $trans; 23 push @{$self->{wait}}, $transfer;
17 Scalar::Util::weaken($self->{wait}[-1]); 24 Scalar::Util::weaken($self->{wait}[-1]);
18 25
19 if (--$self->{conns} >= 0) {
20 $self->wake_next; 26 $self->wake_next;
21 }
22 27
23 $trans; 28 $trans;
24} 29}
25 30
26sub wake_next { 31sub wake_next {
27 my $self = shift; 32 my $self = shift;
28 33
29 return unless $self->{conns} >= 0; 34 $self->sort;
30 35
31 if (@{$self->{wait}}) { 36 while($self->{slots} && @{$self->{wait}}) {
32 while() {
33 my $transfer = shift @{$self->{wait}}; 37 my $transfer = shift @{$self->{wait}};
34 if ($transfer) { 38 if ($transfer) {
39 $self->{lastspb} = $transfer->{spb};
40 $self->{avgspb} ||= $transfer->{spb};
41 $self->{avgspb} = $self->{avgspb} * 0.95 + $transfer->{spb} * 0.05;
35 $transfer->wake; 42 $transfer->wake;
36 last; 43 last;
37 }
38 } 44 }
39 } 45 }
40} 46}
41 47
48sub sort {
49 $_[0]{wait} = [
50 sort { $b->{spb} <=> $a->{spb} }
51 grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ }
52 @{$_[0]{wait}}
53 ];
54}
55
42sub waiters { 56sub waiters {
43 map $_->[1], @{$_[0]{wait}}; 57 $_[0]->sort;
58 @{$_[0]{wait}};
44} 59}
45 60
46package transfer; 61package transfer;
47 62
48use Coro::Timer (); 63use Coro::Timer ();
49 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
50sub try { 73sub try {
51 my $self = shift; 74 my $self = shift;
52 my $timeout = Coro::Timer::timeout $_[0];
53 75
54 Coro::schedule; 76 $self->{alloc} || do {
77 my $timeout = Coro::Timer::timeout $_[0];
78 local $self->{wake} = $self->{coro};
55 79
56 return $self->[2]; 80 Coro::schedule;
57}
58 81
59sub wake { 82 $self->{alloc};
60 my $self = shift; 83 }
61 $self->[2] = 1;
62 $self->[1]->ready;
63} 84}
64 85
65sub DESTROY { 86sub DESTROY {
66 my $self = shift; 87 my $self = shift;
67 $self->[0]{conns}++; 88
89 if ($self->{alloc}) {
90 $self->{queue}{slots}++;
68 $self->[0]->wake_next; 91 $self->{queue}->wake_next;
92 }
69} 93}
70 94
71package conn; 95package conn;
72 96
73our %blockuri; 97our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines