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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines