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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines