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.24 by root, Tue Dec 4 02:46:29 2001 UTC vs.
Revision 1.30 by root, Sun May 19 21:00:47 2002 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines