lib/Langertha/Skeid/Proxy/Realtime.pm

filter: Statementstmt 95.8% 163 / 170 Branchbran 76.0% 73 / 96 Conditioncond 80.0% 36 / 45 MC/DCmcdc 52.3% 11 / 21 Subroutinesub 96.9% 32 / 33 Podpod 75.0% 3 / 4 totaltotal 86.1% 318 / 369 CC 72 SCAR 21.4
CC
72
Cov
87%
_end
21.5
_peer_closed
13.1
__ANON__
12.0
CRAP 8.5
« lib/Langertha/Skeid/Protocol/RawBody.pm ↑ Summary lib/Langertha/Skeid/Protocol.pm »
1 package Langertha::Skeid::Proxy::Realtime;
2 99 our $VERSION = '0.005';
3 # ABSTRACT: One realtime session -- two WebSockets joined, bounded, ended once
4 99 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 99 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 99 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 99 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 99 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 99 ▶use Mojo::Util qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 99 ▶use Langertha::Skeid::Protocol::Realtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 99 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 99 has skeid => (is => 'ro', required => 1);
15 99 has log => (is => 'ro');
16  
17 # The sessions that are open, by id, so a shutdown can close them.
18 549 ▶has sessions => (is => 'ro', default => sub { {} });
Subroutine __ANON__: called CC=1 SCAR=0.0
19  
20 # The process whose event loop the shutdown listener is registered on: a forked worker has its
21 # own loop and must register again.
22 99 has _watching => (is => 'rw');
23 549 ▶has _seq => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 99 ▶sub protocol { return 'Langertha::Skeid::Protocol::Realtime' }
Subroutine protocol: called CC=1 SCAR=0.0
Pod undocumented
26  
27  
28 89 ▶sub start_session {
Subroutine start_session: called CC=5 SCAR=17.2
Pod documented
29 89 my ($self, %a) = @_;
30 89 my $limits = $a{limits};
31 89 my $id = $self->_seq + 1;
32 89 $self->_seq($id);
33 89 my $s = {
34 %a,
35 id => $id,
36 queued => { up => 0, down => 0 },
37 last => time,
38 ended => 0,
39 count => 0,
40 };
41 89 $self->sessions->{ $s->{id} } = $s;
42 89 $self->_watch_shutdown;
43  
44 89 weaken(my $weak = $self);
45 89 my ($client, $up) = @a{qw( client upstream )};
46  
47 # The transaction's own message limit, per session, both ways; and the connections' own idle
48 # timeouts as a backstop a little behind the timer that closes with a reason.
49 89 $_->max_websocket_size($limits->{max_message_bytes}) for $client, $up;
50 89 for my $tx ($client, $up) {
51 178 ▶ my $stream = Mojo::IOLoop->stream($tx->connection // '') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = "Mojo::IOLoop"->stream($tx->connection // "") or next 178 0 178
Condition50%1/2
$tx->connection // ""
l!l
1780
MC/DC0%0/1
$tx->connection // ""
$tx->connection
Truth table50%1/2
A
A= $tx->connection
Aresult
11
00
52 178 $stream->timeout($limits->{idle_timeout} + 5);
53 }
54  
55 65 ▶ $client->on(text => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
56 65 my (undef, $bytes) = @_;
57 65 ▶ $weak->_from_client($s, $bytes) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 65 0 65
58 89 });
59 1 ▶ $client->on(binary => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
60 1 ▶ $weak->_end($s, code => 1003, reason => 'binary frames are not supported',
Branch: 1 branch
BranchTrueFalseTotal
if $weak 1 0 1
61 ok => 0, status_code => 400, rejected => 1, error_type => 'invalid_request_error',
62 error_message => 'A binary frame was sent on a realtime session') if $weak;
63 89 });
64 89 ▶ $client->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
65 89 my (undef, $code, $reason) = @_;
66 89 ▶ $weak->_peer_closed($s, 'client', $code, $reason) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 89 0 89
67 89 });
68  
69 20 ▶ $up->on(text => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
70 20 my (undef, $bytes) = @_;
71 20 ▶ $weak->_from_upstream($s, $bytes) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 20 0 20
72 89 });
73 0 ▶ $up->on(binary => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
74 0 my (undef, $bytes) = @_;
75 0 ▶ return unless $weak;
Branch: 1 branch
BranchTrueFalseTotal
unless $weak 0 0 0
76 0 $s->{last} = time;
77 0 ▶ $weak->_send($s, 'down', { binary => $bytes }, length $bytes) unless $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"ended"} 0 0 0
78 89 });
79 88 ▶ $up->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
80 88 my (undef, $code, $reason) = @_;
81 88 ▶ $weak->_peer_closed($s, 'upstream', $code, $reason) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 88 0 88
82 89 });
83  
84 89 ▶ $s->{idle_timer} = Mojo::IOLoop->timer($limits->{idle_timeout} => sub { $weak->_idle($s) if $weak });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak 1 0 1
85 1 ▶ $s->{life_timer} = Mojo::IOLoop->timer($limits->{max_session_seconds} => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
86 1 ▶ $weak->_end($s, code => 1000, reason => 'session time limit') if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 1 0 1
87 89 });
88 89 return $s->{id};
89 }
90  
91  
92 1 ▶sub open_count { return scalar keys %{ $_[0]->sessions } }
Subroutine open_count: called CC=1 SCAR=0.0
Pod documented
93  
94 7 ▶sub shutdown {
Subroutine shutdown: called CC=2 SCAR=6.9
Pod documented
95 7 my ($self) = @_;
96 7 $self->_end($_, code => 1001, reason => 'server shutdown') for values %{ $self->sessions };
97 7 return;
98 }
99  
100 89 ▶sub _watch_shutdown {
Subroutine _watch_shutdown: called CC=3 SCAR=11.0
101 89 my ($self) = @_;
102 89 ▶ return if ($self->_watching // 0) == $$;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_watching // 0) == $$ 55 34 89
Condition100%2/2
$self->_watching // 0
l!l
5534
MC/DC100%1/1
$self->_watching // 0
$self->_watching
Truth table100%2/2
A
A= $self->_watching
Aresult
11
00
103 34 $self->_watching($$);
104 34 weaken(my $weak = $self);
105 34 ▶ Mojo::IOLoop->singleton->on(finish => sub { $weak->shutdown if $weak });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak 7 0 7
106 34 return;
107 }
108  
109 65 ▶sub _from_client {
Subroutine _from_client: called CC=4 SCAR=13.9
110 65 my ($self, $s, $bytes) = @_;
111 65 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 1 64 65
112 64 $s->{last} = time;
113 64 my $frame = $self->protocol->client_frame($bytes, $s->{requested_model}, $s->{served_model});
114 64 ▶ if ($frame->{action} eq 'forward') {
Branch: 2 branches
BranchTrueFalseTotal
if ($frame->{'action'} eq 'forward') { } 60 4 64
elsif ($frame->{'action'} eq 'refuse') { } 2 2 4
115 # The protocol module's text is encoded already (UTF-8 bytes).
116 60 $self->_send($s, 'up', { text => $frame->{text} }, length $frame->{text});
117 }
118 elsif ($frame->{action} eq 'refuse') {
119 2 $self->_send($s, 'down', { text => $frame->{text} }, length $frame->{text});
120 }
121 else {
122 $self->_end($s, code => $frame->{code}, reason => $frame->{reason},
123 2 ok => 0, status_code => 400, rejected => 1, error_type => 'invalid_request_error',
124 error_message => 'A frame of a realtime session was not a JSON object');
125 }
126 64 return;
127 }
128  
129 20 ▶sub _from_upstream {
Subroutine _from_upstream: called CC=8 SCAR=20.8
130 20 my ($self, $s, $bytes) = @_;
131 20 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 1 19 20
132 19 $s->{last} = time;
133 19 my $info = $self->protocol->server_frame($bytes);
134 19 ▶ if ($info->{usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"usage"}) 8 11 19
135 8 $s->{usage} = $self->protocol->add_usage($s->{usage}, $info->{usage});
136 8 my $seconds = $self->protocol->seconds($info->{usage});
137 8 ▶ $s->{seconds} = ($s->{seconds} // 0) + $seconds if defined $seconds;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seconds 4 4 8
Condition100%2/2
$s->{"seconds"} // 0
l!l
13
MC/DC100%1/1
$s->{"seconds"} // 0
$s->{"seconds"}
Truth table100%2/2
A
A= $s->{"seconds"}
Aresult
11
00
138 8 $s->{count}++;
139 }
140 19 ▶ if (defined $info->{delta_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"delta_text"}) 7 12 19
141 7 ▶ $s->{content_bytes} = ($s->{content_bytes} // 0) + length encode('UTF-8', $info->{delta_text});
Condition100%2/2
$s->{"content_bytes"} // 0
l!l
25
MC/DC100%1/1
$s->{"content_bytes"} // 0
$s->{"content_bytes"}
Truth table100%2/2
A
A= $s->{"content_bytes"}
Aresult
11
00
142 }
143 19 ▶ $s->{error_seen} = 1 if $info->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"error"} 1 18 19
144 19 $self->_send($s, 'down', { text => $bytes }, length $bytes);
145 19 return;
146 }
147  
148 # One frame towards a peer. The bytes are counted until the transaction has written them out
149 # (its drain event), so a peer that does not read is a number, not a leak.
150 81 ▶sub _send {
Subroutine _send: called CC=5 SCAR=16.1
151 81 my ($self, $s, $direction, $frame, $length) = @_;
152 81 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 0 81 81
153 81 my $max = $s->{limits}{max_queued_bytes};
154 81 ▶ if ($s->{queued}{$direction} + $length > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"queued"}{$direction} + $length > $max) 2 79 81
155 2 $self->_end($s, code => 1013, reason => 'try again later',
156 ok => 0, status_code => 503, error_type => 'realtime_queue_overflow',
157 error_message => 'A peer of the realtime session did not read what was sent to it');
158 2 return;
159 }
160 79 $s->{queued}{$direction} += $length;
161 79 ▶ my $tx = $direction eq 'up' ? $s->{upstream} : $s->{client};
Branch: 1 branch
BranchTrueFalseTotal
$direction eq 'up' ? : 59 20 79
162 79 ▶ return unless $tx;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx 0 79 79
163 79 ▶ $tx->send($frame => sub { $s->{queued}{$direction} -= $length });
Subroutine __ANON__: called CC=1 SCAR=0.0
164 79 return;
165 }
166  
167 4 ▶sub _idle {
Subroutine _idle: called CC=3 SCAR=11.0
168 4 my ($self, $s) = @_;
169 4 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 0 4 4
170 4 my $left = $s->{last} + $s->{limits}{idle_timeout} - time;
171 4 ▶ if ($left > 0.001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($left > "0.001") 3 1 4
172 3 weaken(my $weak = $self);
173 3 ▶ $s->{idle_timer} = Mojo::IOLoop->timer($left => sub { $weak->_idle($s) if $weak });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
if $weak 3 0 3
174 3 return;
175 }
176 1 $self->_end($s, code => 1000, reason => 'idle timeout');
177 1 return;
178 }
179  
180 # A close code that may be put on the wire (RFC 6455, 7.4): 1005, 1006 and 1015 may not.
181 80 ▶sub _sendable {
Subroutine _sendable: called CC=8 SCAR=21.0
182 80 my ($code) = @_;
183 80 ▶ return 0 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 80 80
184 80 ▶ return ($code >= 1000 && $code <= 1003) || ($code >= 1007 && $code <= 1014)
Branch: 1 branch
BranchTrueFalseTotal
$code >= 1000 && $code <= 1003 || $code >= 1007 && $code <= 1014 || $code >= 3000 && $code <= 4999 ? : 62 18 80
Condition86%13/15
$code >= 1000 and $code <= 1003 or $code >= 1007 and $code <= 1014 or $code >= 3000 and $code <= 4999
l!l&&r!l&&!r
61118
$code >= 1000 and $code <= 1003 or $code >= 1007 and $code <= 1014
l!l&&r!l&&!r
56519
$code >= 1000 and $code <= 1003
!ll&&!rl&&r
02456
$code >= 1007 and $code <= 1014
!ll&&!rl&&r
1815
$code >= 3000 and $code <= 4999
!ll&&!rl&&r
1801
MC/DC33%2/6
$code >= 1000 and $code <= 1003 or $code >= 1007 and $code <= 1014 or $code >= 3000 and $code <= 4999
$code >= 1000 $code <= 1003 $code >= 1007 $code <= 1014 $code >= 3000 $code <= 4999
Truth table26%4/15
A and B or C and D or E and F
A= $code >= 1000B= $code <= 1003C= $code >= 1007D= $code <= 1014E= $code >= 3000F= $code <= 4999
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
185 || ($code >= 3000 && $code <= 4999) ? 1 : 0;
186 }
187  
188 177 ▶sub _peer_closed {
Subroutine _peer_closed: called CC=13 SCAR=25.7
189 177 my ($self, $s, $who, $code, $reason) = @_;
190 177 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 97 80 177
191 80 ▶ my $vanished = !defined($code) || $code == 1006 || $code == 1015;
Condition66%4/6
!defined($code) || $code == 1006 || $code == 1015
l!l&&r!l&&!r
18062
not defined $code or $code == 1006
l!l&&r!l&&!r
01862
MC/DC33%1/3
!defined($code) || $code == 1006 || $code == 1015
not defined $code $code == 1006 $code == 1015
Truth table50%2/4
not(A) or B || C
A= not defined $codeB= $code == 1006C= $code == 1015
ABCresult
1XX1
01X1
0011
0000
192 80 ▶ my $send = _sendable($code) ? $code : 1000;
Branch: 1 branch
BranchTrueFalseTotal
_sendable($code) ? : 62 18 80
193  
194 80 ▶ if ($who eq 'client') {
Branch: 1 branch
BranchTrueFalseTotal
if ($who eq "client") 74 6 80
195 # A client that left without a close frame abandoned the session (the usage event says so,
196 # as it does for a request); one that closed it properly is not a failure, whatever the code.
197 74 ▶ return $self->_end($s, code => 1000, reason => 'client went away',
Branch: 1 branch
BranchTrueFalseTotal
if $vanished 17 57 74
198 ok => 0, aborted => 1, status_code => 499, error_type => 'client_abort',
199 error_message => 'Client closed the connection without closing the session')
200 if $vanished;
201 # 1009: a message above realtime.max_message_bytes -- the transaction closed the session
202 # itself, the client sent more than it may.
203 57 ▶ return $self->_end($s, code => 1009, reason => 'message too big',
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1009 2 55 57
204 ok => 0, rejected => 1, status_code => 413, error_type => 'invalid_request_error',
205 error_message => 'A message of the realtime session was larger than the limit')
206 if $code == 1009;
207 55 return $self->_end($s, code => $send, reason => $reason);
208 }
209  
210 # The node sent a message above the limit (the transaction closed the session itself): the
211 # client did nothing wrong and is told the node failed, not that its own message was big.
212 6 ▶ return $self->_end($s, code => 1011, reason => 'upstream message too big',
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1009 1 5 6
213 ok => 0, status_code => 502, error_type => 'upstream_error',
214 error_message => 'The node sent a message larger than realtime.max_message_bytes')
215 if $code == 1009;
216 5 ▶ return $self->_end($s, code => 1011, reason => 'upstream connection lost',
Branch: 1 branch
BranchTrueFalseTotal
if $vanished 1 4 5
217 ok => 0, status_code => 502, error_type => 'upstream_error',
218 error_message => 'The node closed the realtime connection without closing the session')
219 if $vanished;
220 return $self->_end($s, code => $send, reason => $reason,
221 ok => 0, status_code => 502, error_type => 'upstream_error',
222 error_message => 'The node sent an error and closed the realtime session')
223 4 ▶ if $s->{error_seen} || $send != 1000 && $send != 1001;
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"error_seen"} or $send != 1000 and $send != 1001 3 1 4
Condition83%5/6
$s->{"error_seen"} or $send != 1000 and $send != 1001
l!l&&r!l&&!r
121
$send != 1000 and $send != 1001
!ll&&!rl&&r
102
MC/DC66%2/3
$s->{"error_seen"} or $send != 1000 and $send != 1001
$s->{"error_seen"} $send != 1000 $send != 1001
Truth table75%3/4
A or B and C
A= $s->{"error_seen"}B= $send != 1000C= $send != 1001
ABCresult
1XX1
0111
00X0
0100
224 1 return $self->_end($s, code => $send, reason => $reason);
225 }
226  
227 # The one end of a session. Whatever called it, the finish callback runs first and exactly once;
228 # then the sides still open are closed with the code and reason given.
229 89 ▶sub _end {
Subroutine _end: called CC=19 SCAR=30.7
230 89 my ($self, $s, %o) = @_;
231 89 ▶ return if $s->{ended}++;
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"}++ 0 89 89
232 89 Mojo::IOLoop->remove($_) for grep { defined } delete @{$s}{qw( idle_timer life_timer )};
233 89 delete $self->sessions->{ $s->{id} };
234  
235 89 ▶ my $ok = exists $o{ok} ? $o{ok} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'ok'} ? : 29 60 89
236 my $result = {
237 ok => $ok ? 1 : 0,
238 status_code => $o{status_code} // 200,
239 error_type => $o{error_type} // '',
240 error_message => $o{error_message} // '',
241 aborted => $o{aborted} ? 1 : 0,
242 rejected => $o{rejected} ? 1 : 0,
243 duration_ms => int((time - $s->{started}) * 1000),
244 usage => $s->{usage},
245 transcriptions => $s->{count},
246 (defined $s->{seconds} ? (audio_seconds => $s->{seconds}) : ()),
247 89 ▶ (defined $s->{content_bytes} ? (content_bytes => $s->{content_bytes}) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$ok ? : 60 29 89
$o{'aborted'} ? : 17 72 89
$o{'rejected'} ? : 5 84 89
defined $s->{'seconds'} ? : 3 86 89
defined $s->{'content_bytes'} ? : 5 84 89
Condition100%6/6
$o{"status_code"} // 200
l!l
2960
$o{"error_type"} // ""
l!l
2960
$o{"error_message"} // ""
l!l
2960
MC/DC100%1/1
$o{"status_code"} // 200
$o{"status_code"}
MC/DC100%1/1
$o{"error_type"} // ""
$o{"error_type"}
MC/DC100%1/1
$o{"error_message"} // ""
$o{"error_message"}
Truth table100%2/2
A
A= $o{"status_code"}
Aresult
11
00
Truth table100%2/2
A
A= $o{"error_type"}
Aresult
11
00
Truth table100%2/2
A
A= $o{"error_message"}
Aresult
11
00
248 };
249 89 my $done = eval { $s->{finish}->($result); 1 };
250 89 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 89 89
251 0 ▶ my $err = $@ || 'unknown error';
Condition0%0/2
$@ || 'unknown error'
l!l
00
MC/DC0%0/1
$@ || 'unknown error'
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
252 0 $err =~ s/\s+\z//;
253 0 ▶ $self->log->error('realtime session finish failed: ' . $err) if $self->log;
Branch: 1 branch
BranchTrueFalseTotal
if $self->log 0 0 0
254 }
255  
256 # A close reason travels in a control frame of at most 125 bytes, two of them the code.
257 89 my $reason = $o{reason};
258 89 ▶ if (defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 89 0 89
259 89 my $bytes = substr(encode('UTF-8', $reason), 0, 120);
260 89 $reason = Encode::decode('UTF-8', $bytes, Encode::FB_QUIET);
261 }
262 89 my ($client, $up) = delete @{$s}{qw( client upstream )};
263 89 for my $tx (grep { defined } $client, $up) {
264 178 ▶ next if $tx->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->is_finished 80 98 178
265 98 ▶ $tx->finish($o{code} // 1000, $reason);
Condition50%1/2
$o{"code"} // 1000
l!l
980
MC/DC0%0/1
$o{"code"} // 1000
$o{"code"}
Truth table50%1/2
A
A= $o{"code"}
Aresult
11
00
266 }
267 89 return;
268 }
269  
270  
271 99 1;
272  
273 __END__
« lib/Langertha/Skeid/Protocol/RawBody.pm ↑ Summary lib/Langertha/Skeid/Protocol.pm »