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.16 by root, Fri Nov 30 05:11:23 2001 UTC vs.
Revision 1.28 by root, Thu Jan 3 01:20:17 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines