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