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.24 by root, Tue Dec 4 02:46:29 2001 UTC

1package transferqueue; 1package transferqueue;
2
3my @reserve = (
4 [ 1_000_000, 1],
5 [ 10_000_000, 1],
6);
2 7
3sub new { 8sub new {
4 my $class = shift; 9 my $class = shift;
5 bless { 10 bless {
6 conns => $_[0], 11 slots => $_[0],
12 lastspb => 0,
13 avgspb => 0,
7 }, $class; 14 }, $class;
8} 15}
9 16
10sub start_transfer { 17sub start_transfer {
11 my $self = shift; 18 my $self = shift;
19 my $size = $_[0];
12 20
13 my $trans = bless [ $self ], transfer::; 21 my $transfer = bless {
22 queue => $self,
23 time => $::NOW,
24 size => $size,
25 coro => $Coro::current,
26 started => 0,
27 }, transfer::;
14 28
15 push @{$self->{wait}}, $trans; 29 push @{$self->{wait}}, $transfer;
16 Scalar::Util::weaken($self->{wait}[-1]);
17 30
18 if (--$self->{conns} >= 0) {
19 $self->wake_next; 31 $self->wake_next;
20 }
21 32
22 $trans; 33 $transfer;
34}
35
36sub sort {
37 my @queue = grep $_, @{$_[0]{wait}};
38
39 $_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue;
40
41 $_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue];
42
43 Scalar::Util::weaken $_ for @{$_[0]{wait}};
23} 44}
24 45
25sub wake_next { 46sub wake_next {
26 my $self = shift; 47 my $self = shift;
27 48
49 $self->sort;
50
28 while(@{$self->{wait}}) { 51 while (@{$self->{wait}}) {
52 my $size = $self->{wait}[0]{size};
53 my $min = 0;
54 for (@reserve) {
55 last if $size <= $_->[0];
56 $min += $_->[1];
57 }
58 last unless $self->{slots} > $min;
29 my $transfer = shift @{$self->{wait}}; 59 my $transfer = shift @{$self->{wait}};
30 if ($transfer) { 60 $self->{lastspb} = $transfer->{spb};
61 $self->{avgspb} ||= $transfer->{spb};
62 $self->{avgspb} = $self->{avgspb} * 0.95 + $transfer->{spb} * 0.05;
63 $self->{started}++;
31 $transfer->wake; 64 $transfer->wake;
32 last; 65 last;
33 }
34 } 66 }
35} 67}
36 68
37sub waiters { 69sub waiters {
38 map $_->[1], @{$_[0]{wait}}; 70 $_[0]->sort;
71 @{$_[0]{wait}};
39} 72}
40 73
41package transfer; 74package transfer;
42 75
43use Coro::Timer (); 76use Coro::Timer ();
44 77
45sub wake { 78sub wake {
46 my $self = shift; 79 my $self = shift;
80
47 $self->[2] = 1; 81 $self->{alloc} = 1;
48 ref $self->[1] and $self->[1]->ready; 82 $self->{queue}{slots}--;
83 $self->{wake} and $self->{wake}->ready;
49} 84}
50 85
51sub try { 86sub try {
52 my $self = shift; 87 my $self = shift;
53 88
54 unless ($self->[2]) { 89 $self->{alloc} || do {
55 my $timeout = Coro::Timer::timeout $_[0]; 90 my $timeout = Coro::Timer::timeout $_[0];
56 local $self->[1] = $Coro::current; 91 local $self->{wake} = $self->{coro};
57 92
58 Coro::schedule; 93 Coro::schedule;
94
95 $self->{alloc};
59 } 96 }
60
61 return $self->[2];
62} 97}
63 98
64sub DESTROY { 99sub DESTROY {
65 my $self = shift; 100 my $self = shift;
66 $self->[0]{conns}++; 101
67 $self->[0]->wake_next if $self->[2]; 102 if ($self->{alloc}) {
103 $self->{queue}{slots}++;
104 $self->{queue}->wake_next;
105 }
68} 106}
69 107
70package conn; 108package conn;
71 109
72our %blockuri; 110our %blockuri;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines