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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines