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