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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines