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.10 by root, Thu Aug 30 02:58:17 2001 UTC vs.
Revision 1.21 by root, Mon Dec 3 05:52:37 2001 UTC

1package transferqueue;
2
3sub new {
4 my $class = shift;
5 bless {
6 slots => $_[0],
7 lastspb => 0,
8 avgspb => 0,
9 }, $class;
10}
11
12sub start_transfer {
13 my $self = shift;
14 my $size = $_[0];
15
16 my $transfer = bless {
17 queue => $self,
18 time => $::NOW,
19 size => $size,
20 coro => $Coro::current,
21 }, transfer::;
22
23 push @{$self->{wait}}, $transfer;
24 Scalar::Util::weaken($self->{wait}[-1]);
25
26 $self->wake_next;
27
28 $trans;
29}
30
31sub wake_next {
32 my $self = shift;
33
34 $self->sort;
35
36 while($self->{slots} && @{$self->{wait}}) {
37 my $transfer = shift @{$self->{wait}};
38 if ($transfer) {
39 $self->{lastspb} = $transfer->{spb};
40 $self->{avgspb} ||= $transfer->{spb};
41 $self->{avgspb} = $self->{avgspb} * 0.95 + $transfer->{spb} * 0.05;
42 $transfer->wake;
43 last;
44 }
45 }
46}
47
48sub sort {
49 my @queue = grep $_, @{$_[0]{wait}};
50
51 $_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue;
52
53 $_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue];
54}
55
56sub waiters {
57 $_[0]->sort;
58 @{$_[0]{wait}};
59}
60
61package transfer;
62
63use Coro::Timer ();
64
65sub wake {
66 my $self = shift;
67
68 $self->{alloc} = 1;
69 $self->{queue}{slots}--;
70 $self->{wake} and $self->{wake}->ready;
71}
72
73sub try {
74 my $self = shift;
75
76 $self->{alloc} || do {
77 my $timeout = Coro::Timer::timeout $_[0];
78 local $self->{wake} = $self->{coro};
79
80 Coro::schedule;
81
82 $self->{alloc};
83 }
84}
85
86sub DESTROY {
87 my $self = shift;
88
89 if ($self->{alloc}) {
90 $self->{queue}{slots}++;
91 $self->{queue}->wake_next;
92 }
93}
94
1package conn; 95package conn;
2 96
3our %blockuri; 97our %blockuri;
4our $blockref; 98our $blockref;
5 99

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines