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.25 by root, Tue Dec 4 03:43:54 2001 UTC vs.
Revision 1.30 by root, Sun May 19 21:00:47 2002 UTC

1package transferqueue;
2
3my @reserve = (
4 [ 1_200_000, 1],
5 [ 3_000_000, 1],
6 [ 75_000_000, 1],
7);
8
9sub new {
10 my $class = shift;
11 bless {
12 slots => $_[0],
13 lastspb => 0,
14 avgspb => 0,
15 }, $class;
16}
17
18sub start_transfer {
19 my $self = shift;
20 my $size = $_[0];
21
22 my $transfer = bless {
23 queue => $self,
24 time => $::NOW,
25 size => $size,
26 coro => $Coro::current,
27 started => 0,
28 }, transfer::;
29
30 push @{$self->{wait}}, $transfer;
31
32 $self->wake_next;
33
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}};
45}
46
47sub wake_next {
48 my $self = shift;
49
50 $self->sort;
51
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 }
68}
69
70sub waiters {
71 $_[0]->sort;
72 @{$_[0]{wait}};
73}
74
75package transfer;
76
77use Coro::Timer ();
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
87sub try {
88 my $self = shift;
89
90 $self->{alloc} || do {
91 my $timeout = Coro::Timer::timeout $_[0];
92 local $self->{wake} = $self->{coro};
93
94 Coro::schedule;
95
96 $self->{alloc};
97 }
98}
99
100sub DESTROY {
101 my $self = shift;
102
103 if ($self->{alloc}) {
104 $self->{queue}{slots}++;
105 $self->{queue}->wake_next;
106 }
107}
108
109package conn; 1package conn;
110 2
111our %blockuri; 3our %blockuri;
112our $blockref; 4our $blockref;
113 5

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines