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.17 by root, Sat Dec 1 01:09:56 2001 UTC vs.
Revision 1.25 by root, Tue Dec 4 03:43:54 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines