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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines