1 | package transferqueue; |
1 | package transferqueue; |
|
|
2 | |
|
|
3 | my @reserve = ( |
|
|
4 | [ 1_200_000, 2], |
|
|
5 | [ 8_000_000, 1], |
|
|
6 | [ 75_000_000, 1], |
|
|
7 | ); |
2 | |
8 | |
3 | sub new { |
9 | sub new { |
4 | my $class = shift; |
10 | my $class = shift; |
5 | bless { |
11 | my $self = bless { |
6 | conns => $_[0], |
12 | slots => 1, |
|
|
13 | maxsize => 0, |
|
|
14 | lastspb => 0, |
|
|
15 | avgspb => 0, |
|
|
16 | @_, |
7 | }, $class; |
17 | }, $class; |
|
|
18 | $self->{reschedule} = Event->timer( |
|
|
19 | after => 10, |
|
|
20 | interval => 3, |
|
|
21 | cb => sub { $self->wake_next }, |
|
|
22 | ); |
|
|
23 | $self; |
8 | } |
24 | } |
9 | |
25 | |
10 | sub start_transfer { |
26 | sub start_transfer { |
11 | my $self = shift; |
27 | my $self = shift; |
|
|
28 | my $size = $_[0]; |
12 | |
29 | |
13 | my $trans = bless [ $self ], transfer::; |
30 | if ($self->{maxsize} && $self->{maxsize} < $size) { |
14 | Scalar::Util::weaken($trans->[0]); |
31 | $size = $self->{maxsize}; |
|
|
32 | } |
15 | |
33 | |
|
|
34 | my $transfer = bless { |
|
|
35 | queue => $self, |
|
|
36 | time => $::NOW, |
|
|
37 | size => $size, |
|
|
38 | coro => $Coro::current, |
|
|
39 | started => 0, |
|
|
40 | }, transfer::; |
|
|
41 | |
16 | push @{$self->{wait}}, $trans; |
42 | push @{$self->{wait}}, $transfer; |
17 | Scalar::Util::weaken($self->{wait}[-1]); |
|
|
18 | |
43 | |
19 | --$self->{conns}; |
|
|
20 | $self->wake_next; |
44 | $self->wake_next; |
21 | |
45 | |
22 | $trans; |
46 | $transfer; |
|
|
47 | } |
|
|
48 | |
|
|
49 | sub sort { |
|
|
50 | my @queue = grep $_, @{$_[0]{wait}}; |
|
|
51 | |
|
|
52 | $_->{spb} = ($::NOW-$_->{time}) / ($_->{size} || 1) for @queue; |
|
|
53 | |
|
|
54 | $_[0]{wait} = [sort { $b->{spb} <=> $a->{spb} } @queue]; |
|
|
55 | |
|
|
56 | Scalar::Util::weaken $_ for @{$_[0]{wait}}; |
23 | } |
57 | } |
24 | |
58 | |
25 | sub wake_next { |
59 | sub wake_next { |
26 | my $self = shift; |
60 | my $self = shift; |
27 | |
61 | |
28 | if ($self->{conns} >= 0) { |
62 | $self->sort; |
|
|
63 | |
29 | while(@{$self->{wait}}) { |
64 | while (@{$self->{wait}}) { |
|
|
65 | my $size = $self->{wait}[0]{size}; |
|
|
66 | my $min = 0; |
|
|
67 | for (@reserve) { |
|
|
68 | last if $size <= $_->[0]; |
|
|
69 | $min += $_->[1]; |
|
|
70 | } |
|
|
71 | last unless $self->{slots} > $min; |
30 | my $transfer = shift @{$self->{wait}}; |
72 | my $transfer = shift @{$self->{wait}}; |
31 | if ($transfer) { |
73 | $self->{lastspb} = $transfer->{spb}; |
|
|
74 | $self->{avgspb} = $self->{avgspb} * 0.99 + $transfer->{spb} * 0.01; |
|
|
75 | $self->{started}++; |
32 | $transfer->wake; |
76 | $transfer->wake; |
33 | last; |
77 | last; |
34 | } |
|
|
35 | } |
|
|
36 | } |
78 | } |
37 | } |
79 | } |
38 | |
80 | |
39 | sub waiters { |
81 | sub waiters { |
40 | map $_->[1], @{$_[0]{wait}}; |
82 | $_[0]->sort; |
|
|
83 | @{$_[0]{wait}}; |
|
|
84 | } |
|
|
85 | |
|
|
86 | sub DESTROY { |
|
|
87 | my $self = shift; |
|
|
88 | |
|
|
89 | $self->{reschedule}->cancel; |
41 | } |
90 | } |
42 | |
91 | |
43 | package transfer; |
92 | package transfer; |
44 | |
93 | |
45 | use Coro::Timer (); |
94 | use Coro::Timer (); |
46 | |
95 | |
47 | sub wake { |
96 | sub wake { |
48 | my $self = shift; |
97 | my $self = shift; |
|
|
98 | |
49 | $self->[2] = 1; |
99 | $self->{alloc} = 1; |
50 | ref $self->[1] and $self->[1]->ready; |
100 | $self->{queue}{slots}--; |
|
|
101 | $self->{wake} and $self->{wake}->ready; |
51 | } |
102 | } |
52 | |
103 | |
53 | sub try { |
104 | sub try { |
54 | my $self = shift; |
105 | my $self = shift; |
55 | |
106 | |
56 | unless ($self->[2]) { |
107 | $self->{alloc} || do { |
57 | my $timeout = Coro::Timer::timeout $_[0]; |
108 | my $timeout = Coro::Timer::timeout $_[0]; |
58 | $self->[1] = $Coro::current; |
109 | local $self->{wake} = $self->{coro}; |
59 | |
110 | |
60 | Coro::schedule; |
111 | Coro::schedule; |
61 | |
112 | |
62 | undef $self->[1]; |
113 | $self->{alloc}; |
63 | } |
114 | } |
64 | |
|
|
65 | return $self->[2]; |
|
|
66 | } |
115 | } |
67 | |
116 | |
68 | sub DESTROY { |
117 | sub DESTROY { |
69 | my $self = shift; |
118 | my $self = shift; |
70 | $self->[0]{conns}++; |
119 | |
|
|
120 | if ($self->{alloc}) { |
|
|
121 | $self->{queue}{slots}++; |
71 | $self->[0]->wake_next; |
122 | $self->{queue}->wake_next; |
|
|
123 | } |
72 | } |
124 | } |
73 | |
125 | |
74 | package conn; |
126 | package conn; |
75 | |
127 | |
76 | our %blockuri; |
128 | our %blockuri; |