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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines