NATS.pm

filter: Statementstmt 56.3% 143 / 254 Branchbran 31.3% 37 / 118 Conditioncond 39.5% 19 / 48 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 61.3% 27 / 44 Podpod 100% 15 / 15 totaltotal 48.0% 242 / 504 CC 85 SCAR 29.2
CC
85
Cov
47%
publish
42.0
subscribe
42.0
_on_read
30.1
CRAP 18.5
↑ Summary NATS/Subscription.pm »
1 package Net::Async::NATS;
2 # ABSTRACT: Async NATS client for IO::Async
3 4 our $VERSION = '0.002';
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use parent 'IO::Async::Notifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use IO::Async::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 4 ▶use JSON::MaybeXS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 4 ▶use Net::Async::NATS::Subscription;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 sub configure {
19 2 ▶ my ($self, %params) = @_;
Subroutine configure: called CC=19 SCAR=32.9
Pod documented
20  
21 2 for my $key (qw(host port name user pass auth_token
22 verbose pedantic reconnect max_reconnect_attempts
23 reconnect_wait on_disconnect on_error on_connect)) {
24 28 ▶ $self->{$key} = delete $params{$key} if exists $params{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $params{$key} 7 21 28
25 }
26  
27 # Defaults
28 2 ▶ $self->{host} //= 'localhost';
Condition50%1/2
$self->{"host"} //= "localhost"
l!l
20
MC/DC0%0/1
$self->{"host"} //= "localhost"
$self->{"host"}
Truth table50%1/2
A
A= $self->{"host"}
Aresult
11
00
29 2 ▶ $self->{port} //= 4222;
Condition50%1/2
$self->{"port"} //= 4222
l!l
20
MC/DC0%0/1
$self->{"port"} //= 4222
$self->{"port"}
Truth table50%1/2
A
A= $self->{"port"}
Aresult
11
00
30 2 ▶ $self->{name} //= 'net-async-nats-perl';
Condition50%1/2
$self->{"name"} //= "net-async-nats-perl"
l!l
20
MC/DC0%0/1
$self->{"name"} //= "net-async-nats-perl"
$self->{"name"}
Truth table50%1/2
A
A= $self->{"name"}
Aresult
11
00
31 2 ▶ $self->{verbose} //= 0;
Condition50%1/2
$self->{"verbose"} //= 0
l!l
02
MC/DC0%0/1
$self->{"verbose"} //= 0
$self->{"verbose"}
Truth table50%1/2
A
A= $self->{"verbose"}
Aresult
11
00
32 2 ▶ $self->{pedantic} //= 0;
Condition50%1/2
$self->{"pedantic"} //= 0
l!l
02
MC/DC0%0/1
$self->{"pedantic"} //= 0
$self->{"pedantic"}
Truth table50%1/2
A
A= $self->{"pedantic"}
Aresult
11
00
33 2 ▶ $self->{reconnect} //= 1;
Condition100%2/2
$self->{"reconnect"} //= 1
l!l
11
MC/DC100%1/1
$self->{"reconnect"} //= 1
$self->{"reconnect"}
Truth table100%2/2
A
A= $self->{"reconnect"}
Aresult
11
00
34 2 ▶ $self->{max_reconnect_attempts} //= 10;
Condition50%1/2
$self->{"max_reconnect_attempts"} //= 10
l!l
02
MC/DC0%0/1
$self->{"max_reconnect_attempts"} //= 10
$self->{"max_reconnect_attempts"}
Truth table50%1/2
A
A= $self->{"max_reconnect_attempts"}
Aresult
11
00
35 2 ▶ $self->{reconnect_wait} //= 2;
Condition50%1/2
$self->{"reconnect_wait"} //= 2
l!l
02
MC/DC0%0/1
$self->{"reconnect_wait"} //= 2
$self->{"reconnect_wait"}
Truth table50%1/2
A
A= $self->{"reconnect_wait"}
Aresult
11
00
36  
37 # Internal state
38 2 ▶ $self->{_sid_counter} //= 0;
Condition50%1/2
$self->{"_sid_counter"} //= 0
l!l
02
MC/DC0%0/1
$self->{"_sid_counter"} //= 0
$self->{"_sid_counter"}
Truth table50%1/2
A
A= $self->{"_sid_counter"}
Aresult
11
00
39 2 ▶ $self->{_subscriptions} //= {};
Condition50%1/2
$self->{"_subscriptions"} //= {}
l!l
02
MC/DC0%0/1
$self->{"_subscriptions"} //= {}
$self->{"_subscriptions"}
Truth table50%1/2
A
A= $self->{"_subscriptions"}
Aresult
11
00
40 2 ▶ $self->{_pending} //= [];
Condition50%1/2
$self->{"_pending"} //= []
l!l
02
MC/DC0%0/1
$self->{"_pending"} //= []
$self->{"_pending"}
Truth table50%1/2
A
A= $self->{"_pending"}
Aresult
11
00
41 2 ▶ $self->{_connected} //= 0;
Condition50%1/2
$self->{"_connected"} //= 0
l!l
02
MC/DC0%0/1
$self->{"_connected"} //= 0
$self->{"_connected"}
Truth table50%1/2
A
A= $self->{"_connected"}
Aresult
11
00
42 2 ▶ $self->{_server_info} //= {};
Condition50%1/2
$self->{"_server_info"} //= {}
l!l
02
MC/DC0%0/1
$self->{"_server_info"} //= {}
$self->{"_server_info"}
Truth table50%1/2
A
A= $self->{"_server_info"}
Aresult
11
00
43 2 ▶ $self->{_ping_future} //= undef;
Condition50%1/2
$self->{"_ping_future"} //= undef
l!l
02
MC/DC0%0/1
$self->{"_ping_future"} //= undef
$self->{"_ping_future"}
Truth table50%1/2
A
A= $self->{"_ping_future"}
Aresult
11
00
44 2 ▶ $self->{_connect_future} //= undef;
Condition50%1/2
$self->{"_connect_future"} //= undef
l!l
02
MC/DC0%0/1
$self->{"_connect_future"} //= undef
$self->{"_connect_future"}
Truth table50%1/2
A
A= $self->{"_connect_future"}
Aresult
11
00
45 2 ▶ $self->{_inbox_prefix} //= '_INBOX.' . _random_id();
Condition50%1/2
$self->{"_inbox_prefix"} //= '_INBOX.' . _random_id()
l!l
02
MC/DC0%0/1
$self->{"_inbox_prefix"} //= '_INBOX.' . _random_id()
$self->{"_inbox_prefix"}
Truth table50%1/2
A
A= $self->{"_inbox_prefix"}
Aresult
11
00
46  
47 2 $self->SUPER::configure(%params);
48 }
49  
50  
51 1 ▶sub host { $_[0]->{host} }
Subroutine host: called CC=1 SCAR=0.0
Pod documented
52 1 ▶sub port { $_[0]->{port} }
Subroutine port: called CC=1 SCAR=0.0
Pod documented
53 1 ▶sub name { $_[0]->{name} }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
54 1 ▶sub verbose { $_[0]->{verbose} }
Subroutine verbose: called CC=1 SCAR=0.0
Pod documented
55 1 ▶sub pedantic { $_[0]->{pedantic} }
Subroutine pedantic: called CC=1 SCAR=0.0
Pod documented
56  
57  
58 4 ▶sub server_info { $_[0]->{_server_info} }
Subroutine server_info: called CC=1 SCAR=0.0
Pod documented
59 2 ▶sub is_connected { $_[0]->{_connected} }
Subroutine is_connected: called CC=1 SCAR=0.0
Pod documented
60  
61  
62 1 ▶async sub connect {
Subroutine connect: called CC=2 SCAR=8.6
Pod documented
63 1 my ($self) = @_;
64  
65 1 ▶ croak "Already connected" if $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connected"} 0 1 1
66  
67 1 $self->{_connect_future} = $self->loop->new_future;
68  
69 my $stream = IO::Async::Stream->new(
70 handle => undef,
71 1 ▶ on_read => sub { $self->_on_read(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
72 0 ▶ on_read_eof => sub { $self->_on_disconnect('read_eof') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73 0 ▶ on_write_eof => sub { $self->_on_disconnect('write_eof') },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 0 ▶ on_read_error => sub { $self->_on_error("Read error: $_[1]") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 0 ▶ on_write_error => sub { $self->_on_error("Write error: $_[1]") },
Subroutine __ANON__: not called CC=1 SCAR=6.9
76 1 );
77 1 $self->{_stream} = $stream;
78 1 $self->add_child($stream);
79  
80 # Retain the TCP connect future — if GC'd before resolution, the
81 # stream never gets its handle and on_read never fires.
82 $self->{_tcp_connect_future} = $stream->connect(
83 host => $self->{host},
84 service => $self->{port},
85 )->on_fail(sub {
86 0 ▶ my $err = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
87 $self->{_connect_future}->fail("Connection failed: $err")
88 0 ▶ unless $self->{_connect_future}->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connect_future"}->is_ready 0 0 0
89 1 });
90  
91 1 return await $self->{_connect_future};
92 4 }
93  
94  
95 0 ▶async sub publish {
Subroutine publish: not called CC=6 SCAR=37.4
Pod documented
96 0 my ($self, $subject, $payload, %opts) = @_;
97  
98 0 ▶ croak "Not connected" unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 0 0
99 0 ▶ croak "Subject required" unless defined $subject && length $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject and length $subject 0 0 0
Condition0%0/3
defined $subject and length $subject
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subject and length $subject
defined $subject length $subject
Truth table0%0/3
A and B
A= defined $subjectB= length $subject
ABresult
0X0
100
111
100  
101 0 ▶ $payload //= '';
Condition0%0/2
$payload //= ""
l!l
00
MC/DC0%0/1
$payload //= ""
$payload
Truth table0%0/2
A
A= $payload
Aresult
11
00
102 0 my $bytes = length($payload);
103 0 my $reply = $opts{reply_to};
104  
105 0 ▶ my $cmd = defined $reply
Branch: 1 branch
BranchTrueFalseTotal
defined $reply ? : 0 0 0
106 ? "PUB $subject $reply $bytes\r\n$payload\r\n"
107 : "PUB $subject $bytes\r\n$payload\r\n";
108  
109 0 $self->_write($cmd);
110 0 return;
111 4 }
112  
113  
114 0 ▶async sub subscribe {
Subroutine subscribe: not called CC=6 SCAR=37.4
Pod documented
115 0 my ($self, $subject, $callback, %opts) = @_;
116  
117 0 ▶ croak "Not connected" unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 0 0
118 0 ▶ croak "Subject required" unless defined $subject && length $subject;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $subject and length $subject 0 0 0
Condition0%0/3
defined $subject and length $subject
!ll&&!rl&&r
000
MC/DC0%0/2
defined $subject and length $subject
defined $subject length $subject
Truth table0%0/3
A and B
A= defined $subjectB= length $subject
ABresult
0X0
100
111
119 0 ▶ croak "Callback required" unless ref $callback eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $callback eq "CODE" 0 0 0
120  
121 0 my $sid = ++$self->{_sid_counter};
122 0 my $queue = $opts{queue};
123  
124 0 my $sub = Net::Async::NATS::Subscription->new(
125 sid => $sid,
126 subject => $subject,
127 queue => $queue,
128 callback => $callback,
129 );
130 0 $self->{_subscriptions}{$sid} = $sub;
131  
132 0 ▶ my $cmd = defined $queue
Branch: 1 branch
BranchTrueFalseTotal
defined $queue ? : 0 0 0
133 ? "SUB $subject $queue $sid\r\n"
134 : "SUB $subject $sid\r\n";
135  
136 0 $self->_write($cmd);
137 0 return $sub;
138 4 }
139  
140  
141 0 ▶async sub unsubscribe {
Subroutine unsubscribe: not called CC=5 SCAR=34.0
Pod documented
142 0 my ($self, $sub, %opts) = @_;
143  
144 0 ▶ croak "Not connected" unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 0 0
145  
146 0 ▶ my $sid = ref $sub ? $sub->sid : $sub;
Branch: 1 branch
BranchTrueFalseTotal
ref $sub ? : 0 0 0
147 0 my $max = $opts{max_msgs};
148  
149 0 ▶ my $cmd = defined $max
Branch: 1 branch
BranchTrueFalseTotal
defined $max ? : 0 0 0
150 ? "UNSUB $sid $max\r\n"
151 : "UNSUB $sid\r\n";
152  
153 0 $self->_write($cmd);
154 0 ▶ delete $self->{_subscriptions}{$sid} unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
155 0 return;
156 4 }
157  
158  
159 0 ▶async sub request {
Subroutine request: not called CC=3 SCAR=24.8
Pod documented
160 0 my ($self, $subject, $payload, %opts) = @_;
161  
162 0 ▶ croak "Not connected" unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 0 0
163  
164 0 ▶ my $timeout = $opts{timeout} // 30;
Condition0%0/2
$opts{"timeout"} // 30
l!l
00
MC/DC0%0/1
$opts{"timeout"} // 30
$opts{"timeout"}
Truth table0%0/2
A
A= $opts{"timeout"}
Aresult
11
00
165 0 my $inbox = $self->{_inbox_prefix} . '.' . _random_id();
166 0 my $f = $self->loop->new_future;
167  
168 my $sub = await $self->subscribe($inbox, sub {
169 0 ▶ my ($subj, $data, $reply) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
170 0 ▶ $f->done($data, $subj) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
171 0 });
172  
173 # Auto-unsubscribe after 1 message
174 0 await $self->unsubscribe($sub, max_msgs => 1);
175  
176 # Publish with reply-to
177 0 await $self->publish($subject, $payload, reply_to => $inbox);
178  
179 # Apply timeout
180 0 my $timer = $self->loop->delay_future(after => $timeout);
181 0 my $result = await Future->wait_any($f, $timer->then_fail('Request timed out'));
182  
183 0 return $result;
184 4 }
185  
186  
187 0 ▶async sub ping {
Subroutine ping: not called CC=2 SCAR=17.9
Pod documented
188 0 my ($self) = @_;
189 0 ▶ croak "Not connected" unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 0 0
190  
191 0 $self->{_ping_future} = $self->loop->new_future;
192 0 $self->_write("PING\r\n");
193 0 return await $self->{_ping_future};
194 4 }
195  
196  
197 1 ▶async sub disconnect {
Subroutine disconnect: called CC=3 SCAR=11.1
Pod documented
198 1 my ($self) = @_;
199 1 ▶ return unless $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_connected"} 0 1 1
200  
201 1 $self->{_connected} = 0;
202 1 $self->{reconnect} = 0; # suppress auto-reconnect
203 1 $self->{_subscriptions} = {};
204  
205 1 ▶ if (my $stream = $self->{_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stream = $self->{"_stream"}) 1 0 1
206 1 $stream->close_when_empty;
207 }
208 1 return;
209 4 }
210  
211 # ── Wire protocol parsing ────────────────────────────────
212  
213 sub _on_read {
214 10 ▶ my ($self, $stream, $buffref, $eof) = @_;
Subroutine _on_read: called CC=17 SCAR=34.0
215  
216 10 ▶ while ($$buffref =~ s/\A([^\r\n]*)\r\n//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$buffref =~ s/\A([^\r\n]*)\r\n//) 11 10 21
217 11 my $line = $1;
218  
219 # MSG <subject> <sid> [reply-to] <#bytes>
220 11 ▶ if ($line =~ /\AMSG\s+(\S+)\s+(\S+)\s+(?:(\S+)\s+)?(\d+)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AMSG\s+(\S+)\s+(\S+)\s+(?:(\S+)\s+)?(\d+)\z/i) 5 6 11
221 5 my ($subject, $sid, $reply_to, $bytes) = ($1, $2, $3, $4);
222  
223 # Need to read payload + trailing CRLF
224 5 ▶ if (length($$buffref) < $bytes + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$buffref < $bytes + 2) 0 5 5
225 # Put line back, wait for more data
226 0 ▶ $$buffref = "MSG $subject $sid "
Branch: 1 branch
BranchTrueFalseTotal
defined $reply_to ? : 0 0 0
227 . (defined $reply_to ? "$reply_to " : '')
228 . "$bytes\r\n$$buffref";
229 0 return 0;
230 }
231  
232 5 my $payload = substr($$buffref, 0, $bytes, '');
233 5 $$buffref =~ s/\A\r\n//; # consume trailing CRLF
234  
235 5 $self->_dispatch_msg($subject, $sid, $reply_to, $payload);
236 5 next;
237 }
238  
239 # HMSG <subject> <sid> [reply-to] <#header_bytes> <#total_bytes>
240 6 ▶ if ($line =~ /\AHMSG\s+(\S+)\s+(\S+)\s+(?:(\S+)\s+)?(\d+)\s+(\d+)\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AHMSG\s+(\S+)\s+(\S+)\s+(?:(\S+)\s+)?(\d+)\s+(\d+)\z/i) 0 6 6
241 0 my ($subject, $sid, $reply_to, $hdr_bytes, $total_bytes) = ($1, $2, $3, $4, $5);
242  
243 0 ▶ if (length($$buffref) < $total_bytes + 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $$buffref < $total_bytes + 2) 0 0 0
244 0 ▶ $$buffref = "HMSG $subject $sid "
Branch: 1 branch
BranchTrueFalseTotal
defined $reply_to ? : 0 0 0
245 . (defined $reply_to ? "$reply_to " : '')
246 . "$hdr_bytes $total_bytes\r\n$$buffref";
247 0 return 0;
248 }
249  
250 0 my $raw = substr($$buffref, 0, $total_bytes, '');
251 0 $$buffref =~ s/\A\r\n//;
252  
253 0 my $payload_bytes = $total_bytes - $hdr_bytes;
254 0 ▶ my $payload = $payload_bytes > 0
Branch: 1 branch
BranchTrueFalseTotal
$payload_bytes > 0 ? : 0 0 0
255 ? substr($raw, $hdr_bytes)
256 : '';
257  
258 # For now, ignore headers — deliver payload only
259 0 $self->_dispatch_msg($subject, $sid, $reply_to, $payload);
260 0 next;
261 }
262  
263 # INFO {...}
264 6 ▶ if ($line =~ /\AINFO\s+(\{.*\})\s*\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\AINFO\s+(\{.*\})\s*\z/i) 2 4 6
265 2 ▶ my $info = eval { decode_json($1) } // {};
Condition50%1/2
eval { do { &decode_json($1) } } // {}
l!l
20
MC/DC0%0/1
eval { do { &decode_json($1) } } // {}
eval { do { &decode_json($1) } }
Truth table50%1/2
A
A= eval { do { &decode_json($1) } }
Aresult
11
00
266 2 $self->{_server_info} = $info;
267 2 $self->_handle_info($info);
268 2 next;
269 }
270  
271 # PING
272 4 ▶ if ($line =~ /\APING\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\APING\z/i) 1 3 4
273 1 $self->_write("PONG\r\n");
274 1 next;
275 }
276  
277 # PONG
278 3 ▶ if ($line =~ /\APONG\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\APONG\z/i) 1 2 3
279 1 ▶ if (my $f = delete $self->{_ping_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_ping_future"}) 1 0 1
280 1 ▶ $f->done if !$f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
281 }
282 1 next;
283 }
284  
285 # +OK
286 2 ▶ next if $line =~ /\A\+OK\z/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\A\+OK\z/i 1 1 2
287  
288 # -ERR
289 1 ▶ if ($line =~ /\A-ERR\s+'?(.+?)'?\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\A-ERR\s+'?(.+?)'?\z/i) 1 0 1
290 1 $self->_on_error($1);
291 1 next;
292 }
293 }
294  
295 10 return 0;
296 }
297  
298 sub _handle_info {
299 2 ▶ my ($self, $info) = @_;
Subroutine _handle_info: called CC=11 SCAR=28.7
300  
301 # If we haven't sent CONNECT yet, do it now
302 2 ▶ return if $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connected"} 0 2 2
303  
304 my %connect = (
305 verbose => $self->{verbose} ? \1 : \0,
306 pedantic => $self->{pedantic} ? \1 : \0,
307 tls_required => \0,
308 lang => 'perl',
309 version => ($Net::Async::NATS::VERSION // '0.001'),
310 name => $self->{name},
311 2 ▶ protocol => 1,
Branch: 2 branches
BranchTrueFalseTotal
$self->{'verbose'} ? : 0 2 2
$self->{'pedantic'} ? : 0 2 2
Condition50%1/2
$Net::Async::NATS::VERSION // "0.001"
l!l
20
MC/DC0%0/1
$Net::Async::NATS::VERSION // "0.001"
$Net::Async::NATS::VERSION
Truth table50%1/2
A
A= $Net::Async::NATS::VERSION
Aresult
11
00
312 echo => \1,
313 headers => \1,
314 no_responders => \1,
315 );
316  
317 2 ▶ if (defined $self->{auth_token}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"auth_token"}) 0 2 2
318 0 $connect{auth_token} = $self->{auth_token};
319 }
320 2 ▶ if (defined $self->{user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"user"}) 0 2 2
321 0 $connect{user} = $self->{user};
322 0 ▶ $connect{pass} = $self->{pass} // '';
Condition0%0/2
$self->{"pass"} // ""
l!l
00
MC/DC0%0/1
$self->{"pass"} // ""
$self->{"pass"}
Truth table0%0/2
A
A= $self->{"pass"}
Aresult
11
00
323 }
324  
325 2 my $json = encode_json(\%connect);
326 2 $self->_write("CONNECT $json\r\n");
327  
328 # Send initial PING to verify connection
329 2 $self->_write("PING\r\n");
330  
331 2 $self->{_connected} = 1;
332 2 delete $self->{_tcp_connect_future}; # no longer needed
333  
334 2 ▶ if (my $f = delete $self->{_connect_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = delete $self->{"_connect_future"}) 1 1 2
335 1 ▶ $f->done($info) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
336 }
337  
338 2 ▶ if (my $cb = $self->{on_connect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_connect"}) 0 2 2
339 0 $cb->($self, $info);
340 }
341 }
342  
343 sub _dispatch_msg {
344 5 ▶ my ($self, $subject, $sid, $reply_to, $payload) = @_;
Subroutine _dispatch_msg: called CC=4 SCAR=17.9
345  
346 5 my $sub = $self->{_subscriptions}{$sid};
347 5 ▶ return unless $sub;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub 0 5 5
348  
349 5 $sub->callback->($subject, $payload, $reply_to);
350  
351 5 ▶ if (defined $sub->max_msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub->max_msgs) 0 5 5
352 0 $sub->{_received}++;
353 0 ▶ if ($sub->{_received} >= $sub->max_msgs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sub->{"_received"} >= $sub->max_msgs) 0 0 0
354 0 delete $self->{_subscriptions}{$sid};
355 }
356 }
357 }
358  
359 sub _write {
360 4 ▶ my ($self, $data) = @_;
Subroutine _write: called CC=2 SCAR=6.9
361 4 ▶ if (my $stream = $self->{_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stream = $self->{"_stream"}) 2 2 4
362 2 $stream->write($data);
363 }
364 }
365  
366 sub _on_disconnect {
367 0 ▶ my ($self, $reason) = @_;
Subroutine _on_disconnect: not called CC=3 SCAR=24.8
368 0 $self->{_connected} = 0;
369  
370 0 ▶ if (my $cb = $self->{on_disconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_disconnect"}) 0 0 0
371 0 $cb->($self, $reason);
372 }
373  
374 0 ▶ if ($self->{reconnect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"reconnect"}) 0 0 0
375 0 $self->_reconnect;
376 }
377 }
378  
379 sub _on_error {
380 1 ▶ my ($self, $error) = @_;
Subroutine _on_error: called CC=2 SCAR=7.1
381 1 ▶ if (my $cb = $self->{on_error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = $self->{"on_error"}) 1 0 1
382 1 $cb->($self, $error);
383 }
384 }
385  
386 sub _reconnect {
387 0 ▶ my ($self) = @_;
Subroutine _reconnect: not called CC=1 SCAR=6.9
388 0 my $attempts = 0;
389 0 my $max = $self->{max_reconnect_attempts};
390 0 my $wait = $self->{reconnect_wait};
391  
392 0 weaken(my $weak_self = $self);
393  
394 0 my $try; $try = sub {
395 0 ▶ my $self = $weak_self or return;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak_self or return 0 0 0
396 0 ▶ return if $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connected"} 0 0 0
397 0 ▶ return if ++$attempts > $max;
Branch: 1 branch
BranchTrueFalseTotal
if ++$attempts > $max 0 0 0
398  
399 $self->loop->delay_future(after => $wait)->on_done(sub {
400 0 ▶ my $self = $weak_self or return;
Subroutine __ANON__: not called CC=5 SCAR=34.0
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak_self or return 0 0 0
401 0 ▶ return if $self->{_connected};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_connected"} 0 0 0
402  
403 # Remove old stream
404 0 ▶ if (my $old = delete $self->{_stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = delete $self->{"_stream"}) 0 0 0
405 0 ▶ $self->remove_child($old) if $old->parent;
Branch: 1 branch
BranchTrueFalseTotal
if $old->parent 0 0 0
406 }
407  
408 # Save subscriptions for replay
409 0 my %saved_subs = %{ $self->{_subscriptions} };
410  
411 0 eval {
412 0 my $f = $self->connect;
413 $f->on_done(sub {
414 0 ▶ my $self = $weak_self or return;
Subroutine __ANON__: not called CC=4 SCAR=30.0
Branch: 1 branch
BranchTrueFalseTotal
my $self = $weak_self or return 0 0 0
415 # Replay subscriptions
416 0 for my $sub (values %saved_subs) {
417 0 ▶ my $cmd = defined $sub->queue
Branch: 1 branch
BranchTrueFalseTotal
defined $sub->queue ? : 0 0 0
418 ? "SUB " . $sub->subject . " " . $sub->queue . " " . $sub->sid . "\r\n"
419 : "SUB " . $sub->subject . " " . $sub->sid . "\r\n";
420 0 $self->_write($cmd);
421 0 $self->{_subscriptions}{$sub->sid} = $sub;
422 }
423 0 });
424 0 ▶ $f->on_fail(sub { $try->() });
Subroutine __ANON__: not called CC=1 SCAR=6.9
425 };
426 0 });
427 0 };
428  
429 0 $try->();
430 }
431  
432 sub _random_id {
433 2 ▶ my $bytes = '';
Subroutine _random_id: called CC=2 SCAR=7.2
434 2 ▶ if (open my $fh, '<:raw', '/dev/urandom') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '<:raw', '/dev/urandom') { } 2 0 2
435 2 read $fh, $bytes, 12;
436 2 close $fh;
437 } else {
438 0 $bytes = pack('N3', rand(2**32), rand(2**32), rand(2**32));
439 }
440 2 return unpack('H*', $bytes);
441 }
442  
443  
444 4 1;
445  
446 __END__
↑ Summary NATS/Subscription.pm »