lib/Langertha/Skeid/Proxy/Realtime.pm

filter: Statementstmt 95.8% 163 / 170 Branchbran 75.0% 72 / 96 Conditioncond 80.0% 36 / 45 MC/DCmcdc 52.3% 11 / 21 Subroutinesub 96.9% 32 / 33 Podpod 75.0% 3 / 4 totaltotal 85.9% 317 / 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 86 our $VERSION = '0.004';
3 # ABSTRACT: One realtime session -- two WebSockets joined, bounded, ended once
4 86 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 86 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 86 ▶use Time::HiRes qw( time );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 86 ▶use Mojo::IOLoop;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 86 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 86 ▶use Mojo::Util qw( encode );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 86 ▶use Langertha::Skeid::Protocol::Realtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 86 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 86 has skeid => (is => 'ro', required => 1);
15 86 has log => (is => 'ro');
16  
17 # The sessions that are open, by id, so a shutdown can close them.
18 512 ▶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 86 has _watching => (is => 'rw');
23 512 ▶has _seq => (is => 'rw', default => sub { 0 });
Subroutine __ANON__: called CC=1 SCAR=0.0
24  
25 107 ▶sub protocol { return 'Langertha::Skeid::Protocol::Realtime' }
Subroutine protocol: called CC=1 SCAR=0.0
Pod undocumented
26  
27  
28 88 ▶sub start_session {
Subroutine start_session: called CC=5 SCAR=17.2
Pod documented
29 88 my ($self, %a) = @_;
30 88 my $limits = $a{limits};
31 88 my $id = $self->_seq + 1;
32 88 $self->_seq($id);
33 88 my $s = {
34 %a,
35 id => $id,
36 queued => { up => 0, down => 0 },
37 last => time,
38 ended => 0,
39 count => 0,
40 };
41 88 $self->sessions->{ $s->{id} } = $s;
42 88 $self->_watch_shutdown;
43  
44 88 weaken(my $weak = $self);
45 88 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 88 $_->max_websocket_size($limits->{max_message_bytes}) for $client, $up;
50 88 for my $tx ($client, $up) {
51 176 ▶ my $stream = Mojo::IOLoop->stream($tx->connection // '') or next;
Branch: 1 branch
BranchTrueFalseTotal
my $stream = "Mojo::IOLoop"->stream($tx->connection // "") or next 176 0 176
Condition50%1/2
$tx->connection // ""
l!l
1760
MC/DC0%0/1
$tx->connection // ""
$tx->connection
Truth table50%1/2
A
A= $tx->connection
Aresult
11
00
52 176 $stream->timeout($limits->{idle_timeout} + 5);
53 }
54  
55 72 ▶ $client->on(text => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
56 72 my (undef, $bytes) = @_;
57 72 ▶ $weak->_from_client($s, $bytes) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 72 0 72
58 88 });
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 88 });
64 88 ▶ $client->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
65 88 my (undef, $code, $reason) = @_;
66 88 ▶ $weak->_peer_closed($s, 'client', $code, $reason) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 88 0 88
67 88 });
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 88 });
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 88 });
79 87 ▶ $up->on(finish => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
80 87 my (undef, $code, $reason) = @_;
81 87 ▶ $weak->_peer_closed($s, 'upstream', $code, $reason) if $weak;
Branch: 1 branch
BranchTrueFalseTotal
if $weak 87 0 87
82 88 });
83  
84 88 ▶ $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 88 });
88 88 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 88 ▶sub _watch_shutdown {
Subroutine _watch_shutdown: called CC=3 SCAR=11.0
101 88 my ($self) = @_;
102 88 ▶ return if ($self->_watching // 0) == $$;
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_watching // 0) == $$ 55 33 88
Condition100%2/2
$self->_watching // 0
l!l
5533
MC/DC100%1/1
$self->_watching // 0
$self->_watching
Truth table100%2/2
A
A= $self->_watching
Aresult
11
00
103 33 $self->_watching($$);
104 33 weaken(my $weak = $self);
105 33 ▶ 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 33 return;
107 }
108  
109 72 ▶sub _from_client {
Subroutine _from_client: called CC=4 SCAR=13.9
110 72 my ($self, $s, $bytes) = @_;
111 72 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 1 71 72
112 71 $s->{last} = time;
113 71 my $frame = $self->protocol->client_frame($bytes, $s->{requested_model}, $s->{served_model});
114 71 ▶ if ($frame->{action} eq 'forward') {
Branch: 2 branches
BranchTrueFalseTotal
if ($frame->{'action'} eq 'forward') { } 67 4 71
elsif ($frame->{'action'} eq 'refuse') { } 2 2 4
115 # The protocol module's text is encoded already (UTF-8 bytes).
116 67 $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 71 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"} 0 20 20
132 20 $s->{last} = time;
133 20 my $info = $self->protocol->server_frame($bytes);
134 20 ▶ if ($info->{usage}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info->{"usage"}) 8 12 20
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 20 ▶ if (defined $info->{delta_text}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $info->{"delta_text"}) 8 12 20
141 8 ▶ $s->{content_bytes} = ($s->{content_bytes} // 0) + length encode('UTF-8', $info->{delta_text});
Condition100%2/2
$s->{"content_bytes"} // 0
l!l
35
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 20 ▶ $s->{error_seen} = 1 if $info->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $info->{"error"} 1 19 20
144 20 $self->_send($s, 'down', { text => $bytes }, length $bytes);
145 20 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 89 ▶sub _send {
Subroutine _send: called CC=5 SCAR=16.1
151 89 my ($self, $s, $direction, $frame, $length) = @_;
152 89 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 0 89 89
153 89 my $max = $s->{limits}{max_queued_bytes};
154 89 ▶ if ($s->{queued}{$direction} + $length > $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->{"queued"}{$direction} + $length > $max) 2 87 89
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 87 $s->{queued}{$direction} += $length;
161 87 ▶ my $tx = $direction eq 'up' ? $s->{upstream} : $s->{client};
Branch: 1 branch
BranchTrueFalseTotal
$direction eq 'up' ? : 66 21 87
162 87 ▶ return unless $tx;
Branch: 1 branch
BranchTrueFalseTotal
unless $tx 0 87 87
163 87 ▶ $tx->send($frame => sub { $s->{queued}{$direction} -= $length });
Subroutine __ANON__: called CC=1 SCAR=0.0
164 87 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 79 ▶sub _sendable {
Subroutine _sendable: called CC=8 SCAR=21.0
182 79 my ($code) = @_;
183 79 ▶ return 0 unless defined $code;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $code 0 79 79
184 79 ▶ 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 ? : 61 18 79
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
60118
$code >= 1000 and $code <= 1003 or $code >= 1007 and $code <= 1014
l!l&&r!l&&!r
55519
$code >= 1000 and $code <= 1003
!ll&&!rl&&r
02455
$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 175 ▶sub _peer_closed {
Subroutine _peer_closed: called CC=13 SCAR=25.7
189 175 my ($self, $s, $who, $code, $reason) = @_;
190 175 ▶ return if $s->{ended};
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"} 96 79 175
191 79 ▶ my $vanished = !defined($code) || $code == 1006 || $code == 1015;
Condition66%4/6
!defined($code) || $code == 1006 || $code == 1015
l!l&&r!l&&!r
18061
not defined $code or $code == 1006
l!l&&r!l&&!r
01861
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 79 ▶ my $send = _sendable($code) ? $code : 1000;
Branch: 1 branch
BranchTrueFalseTotal
_sendable($code) ? : 61 18 79
193  
194 79 ▶ if ($who eq 'client') {
Branch: 1 branch
BranchTrueFalseTotal
if ($who eq "client") 73 6 79
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 73 ▶ return $self->_end($s, code => 1000, reason => 'client went away',
Branch: 1 branch
BranchTrueFalseTotal
if $vanished 17 56 73
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 56 ▶ return $self->_end($s, code => 1009, reason => 'message too big',
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1009 2 54 56
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 54 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 88 ▶sub _end {
Subroutine _end: called CC=19 SCAR=30.7
230 88 my ($self, $s, %o) = @_;
231 88 ▶ return if $s->{ended}++;
Branch: 1 branch
BranchTrueFalseTotal
if $s->{"ended"}++ 0 88 88
232 88 Mojo::IOLoop->remove($_) for grep { defined } delete @{$s}{qw( idle_timer life_timer )};
233 88 delete $self->sessions->{ $s->{id} };
234  
235 88 ▶ my $ok = exists $o{ok} ? $o{ok} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'ok'} ? : 29 59 88
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 88 ▶ (defined $s->{content_bytes} ? (content_bytes => $s->{content_bytes}) : ()),
Branch: 5 branches
BranchTrueFalseTotal
$ok ? : 59 29 88
$o{'aborted'} ? : 17 71 88
$o{'rejected'} ? : 5 83 88
defined $s->{'seconds'} ? : 3 85 88
defined $s->{'content_bytes'} ? : 5 83 88
Condition100%6/6
$o{"status_code"} // 200
l!l
2959
$o{"error_type"} // ""
l!l
2959
$o{"error_message"} // ""
l!l
2959
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 88 my $done = eval { $s->{finish}->($result); 1 };
250 88 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 0 88 88
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 88 my $reason = $o{reason};
258 88 ▶ if (defined $reason) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $reason) 88 0 88
259 88 my $bytes = substr(encode('UTF-8', $reason), 0, 120);
260 88 $reason = Encode::decode('UTF-8', $bytes, Encode::FB_QUIET);
261 }
262 88 my ($client, $up) = delete @{$s}{qw( client upstream )};
263 88 for my $tx (grep { defined } $client, $up) {
264 176 ▶ next if $tx->is_finished;
Branch: 1 branch
BranchTrueFalseTotal
if $tx->is_finished 79 97 176
265 97 ▶ $tx->finish($o{code} // 1000, $reason);
Condition50%1/2
$o{"code"} // 1000
l!l
970
MC/DC0%0/1
$o{"code"} // 1000
$o{"code"}
Truth table50%1/2
A
A= $o{"code"}
Aresult
11
00
266 }
267 88 return;
268 }
269  
270  
271 86 1;
272  
273 __END__
« lib/Langertha/Skeid/Protocol/RawBody.pm ↑ Summary lib/Langertha/Skeid/Protocol.pm »