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.18 by root, Sat Dec 1 01:37:24 2001 UTC vs.
Revision 1.21 by root, Mon Dec 3 05:52:37 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 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
34 $self->sort;
35
28 while(@{$self->{wait}}) { 36 while($self->{slots} && @{$self->{wait}}) {
29 my $transfer = shift @{$self->{wait}}; 37 my $transfer = shift @{$self->{wait}};
30 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;
31 $transfer->wake; 42 $transfer->wake;
32 last; 43 last;
33 } 44 }
34 } 45 }
35} 46}
36 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
37sub waiters { 56sub waiters {
38 map $_->[1], @{$_[0]{wait}}; 57 $_[0]->sort;
58 @{$_[0]{wait}};
39} 59}
40 60
41package transfer; 61package transfer;
42 62
43use Coro::Timer (); 63use Coro::Timer ();
44 64
45sub wake { 65sub wake {
46 my $self = shift; 66 my $self = shift;
67
47 $self->[2] = 1; 68 $self->{alloc} = 1;
48 ref $self->[1] and $self->[1]->ready; 69 $self->{queue}{slots}--;
70 $self->{wake} and $self->{wake}->ready;
49} 71}
50 72
51sub try { 73sub try {
52 my $self = shift; 74 my $self = shift;
53 75
54 unless ($self->[2]) { 76 $self->{alloc} || do {
55 my $timeout = Coro::Timer::timeout $_[0]; 77 my $timeout = Coro::Timer::timeout $_[0];
56 local $self->[1] = $Coro::current; 78 local $self->{wake} = $self->{coro};
57 79
58 Coro::schedule; 80 Coro::schedule;
81
82 $self->{alloc};
59 } 83 }
60
61 return $self->[2];
62} 84}
63 85
64sub DESTROY { 86sub DESTROY {
65 my $self = shift; 87 my $self = shift;
66 $self->[0]{conns}++; 88
67 $self->[0]->wake_next if $self->[2]; 89 if ($self->{alloc}) {
90 $self->{queue}{slots}++;
91 $self->{queue}->wake_next;
92 }
68} 93}
69 94
70package conn; 95package conn;
71 96
72our %blockuri; 97our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines