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.12 by root, Thu Nov 29 01:53:40 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;
24 Scalar::Util::weaken($self->{wait}[-1]);
17 25
18 if (--$self->{conns} >= 0) {
19 $self->wake_next; 26 $self->wake_next;
20 }
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
28 return unless $self->{conns} >= 0; 34 $self->sort;
29 35
30 (pop @{$self->{wait}})->wake if @{$self->{wait}}; 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 $_[0]{wait} = [
50 sort { $b->{spb} <=> $a->{spb} }
51 grep { $_ && ($_->{spb} = ($::NOW-$_->{time})/($_->{size}||1)), $_ }
52 @{$_[0]{wait}}
53 ];
31} 54}
32 55
33sub waiters { 56sub waiters {
34 map $_->[1], @{$_[0]{wait}}; 57 $_[0]->sort;
58 @{$_[0]{wait}};
35} 59}
36 60
37package transfer; 61package transfer;
38 62
39use Coro::Timer (); 63use Coro::Timer ();
40 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
41sub try { 73sub try {
42 my $self = shift; 74 my $self = shift;
43 my $timeout = Coro::Timer::timeout $_[0];
44 75
45 Coro::schedule; 76 $self->{alloc} || do {
77 my $timeout = Coro::Timer::timeout $_[0];
78 local $self->{wake} = $self->{coro};
46 79
47 return $self->[2]; 80 Coro::schedule;
48}
49 81
50sub wake { 82 $self->{alloc};
51 my $self = shift; 83 }
52 $self->[2] = 1;
53 $self->[1]->ready;
54} 84}
55 85
56sub DESTROY { 86sub DESTROY {
57 my $self = shift; 87 my $self = shift;
58 $self->[0]{conns}++; 88
89 if ($self->{alloc}) {
90 $self->{queue}{slots}++;
59 $self->[0]->wake_next; 91 $self->{queue}->wake_next;
92 }
60} 93}
61 94
62package conn; 95package conn;
63 96
64our %blockuri; 97our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines