blib/lib/EV/Nats/JetStream.pm

filter: Statementstmt 88.4% 244 / 276 Branchbran 81.9% 118 / 144 Conditioncond 55.7% 58 / 104 MC/DCmcdc 36.6% 22 / 60 Subroutinesub 84.1% 53 / 63 Podpod 56.7% 21 / 37 totaltotal 75.4% 516 / 684 CC 117 SCAR 24.0
CC
117
Cov
80%
__ANON__
110.0
__ANON__
12.0
consumer_create
12.0
CRAP 11.1
« blib/lib/EV/Nats/KV.pm ↑ Summary blib/lib/EV/Nats/ObjectStore.pm »
1 package EV::Nats::JetStream;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 our $VERSION = '0.08';
5 5 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 46 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
8 46 my ($class, %opts) = @_;
9 46 ▶ my $nats = delete $opts{nats} || die "nats connection required";
Condition50%1/2
delete $opts{'nats'} || die('nats connection required')
l!l
460
MC/DC0%0/1
delete $opts{'nats'} || die('nats connection required')
delete $opts{'nats'}
Truth table50%1/2
A
A= delete $opts{'nats'}
Aresult
11
00
10 46 ▶ my $prefix = delete $opts{prefix} || '$JS.API';
Condition100%2/2
delete $opts{'prefix'} || '$JS.API'
l!l
145
MC/DC100%1/1
delete $opts{'prefix'} || '$JS.API'
delete $opts{'prefix'}
Truth table100%2/2
A
A= delete $opts{'prefix'}
Aresult
11
00
11 46 ▶ my $timeout = delete $opts{timeout} || 5000;
Condition100%2/2
delete $opts{'timeout'} || 5000
l!l
1333
MC/DC100%1/1
delete $opts{'timeout'} || 5000
delete $opts{'timeout'}
Truth table100%2/2
A
A= delete $opts{'timeout'}
Aresult
11
00
12 46 ▶ my $with_status = delete $opts{with_status} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $opts{'with_status'} ? : 3 43 46
13 46 warn "EV::Nats::JetStream::new: unknown option '$_'" for keys %opts;
14 46 bless {
15 nats => $nats,
16 prefix => $prefix,
17 timeout => $timeout,
18 with_status => $with_status,
19 }, $class;
20 }
21  
22 # the same client with another request timeout; the direct-get cache is shared
23 51 ▶sub with_timeout {
Subroutine with_timeout: called CC=4 SCAR=13.9
Pod undocumented
24 51 my ($self, $ms) = @_;
25 51 ▶ return $self if !$ms || $ms == $self->{timeout};
Branch: 1 branch
BranchTrueFalseTotal
if not $ms or $ms == $self->{"timeout"} 47 4 51
Condition66%2/3
not $ms or $ms == $self->{"timeout"}
l!l&&r!l&&!r
4704
MC/DC50%1/2
not $ms or $ms == $self->{"timeout"}
not $ms $ms == $self->{"timeout"}
Truth table66%2/3
not(A) or B
A= not $msB= $ms == $self->{"timeout"}
ABresult
1X1
011
000
26 4 ▶ return bless { %$self, direct => ($self->{direct} //= {}), timeout => $ms }, ref $self;
Condition100%2/2
$self->{"direct"} //= {}
l!l
13
MC/DC100%1/1
$self->{"direct"} //= {}
$self->{"direct"}
Truth table100%2/2
A
A= $self->{"direct"}
Aresult
11
00
27 }
28  
29 5 my $JSON = eval { require Cpanel::JSON::XS; Cpanel::JSON::XS->new }
30 0 ▶ || eval { require JSON::XS; JSON::XS->new }
Condition33%2/6
eval { do { require Cpanel::JSON::XS; 'Cpanel::JSON::XS'->new } } || eval { do { require JSON::XS; 'JSON::XS'->new } } || 'JSON::PP'->new
l!l&&r!l&&!r
500
eval { do { require Cpanel::JSON::XS; "Cpanel::JSON::XS"->new } } or eval { do { require JSON::XS; "JSON::XS"->new } }
l!l&&r!l&&!r
500
MC/DC0%0/3
eval { do { require Cpanel::JSON::XS; 'Cpanel::JSON::XS'->new } } || eval { do { require JSON::XS; 'JSON::XS'->new } } || 'JSON::PP'->new
eval { do { require Cpanel::JSON::XS; "Cpanel::JSON::XS"->new } } eval { do { require JSON::XS; "JSON::XS"->new } } 'JSON::PP'->new
Truth table25%1/4
A or B || C
A= eval { do { require Cpanel::JSON::XS; "Cpanel::JSON::XS"->new } }B= eval { do { require JSON::XS; "JSON::XS"->new } }C= 'JSON::PP'->new
ABCresult
1XX1
01X1
0011
0000
31 || JSON::PP->new;
32 5 $JSON->utf8->allow_nonref;
33  
34 46 ▶sub decode_json_or_error {
Subroutine decode_json_or_error: called CC=2 SCAR=6.9
Pod undocumented
35 46 my ($json) = @_;
36 46 local $@;
37 46 my $r = eval { $JSON->decode($json) };
38 46 ▶ return ($r, undef) unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 45 1 46
39 1 return (undef, "JSON decode error: $@");
40 }
41  
42 22 ▶sub msg_is_tombstone {
Subroutine msg_is_tombstone: called CC=4 SCAR=13.9
Pod undocumented
43 22 my ($msg) = @_;
44 22 ▶ return 0 unless $msg && defined $msg->{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless $msg and defined $msg->{"headers"} 7 15 22
Condition66%2/3
$msg and defined $msg->{"headers"}
!ll&&!rl&&r
0715
MC/DC50%1/2
$msg and defined $msg->{"headers"}
$msg defined $msg->{"headers"}
Truth table66%2/3
A and B
A= $msgB= defined $msg->{"headers"}
ABresult
0X0
100
111
45 # or a server TTL marker (nats-server 2.11+), as nats.go reads it
46 15 ▶ return $msg->{headers} =~ /KV-Operation:\s*(?:DEL|PURGE)|Nats-Marker-Reason:\s*(?:MaxAge|Purge|Remove)/i
Branch: 1 branch
BranchTrueFalseTotal
$msg->{'headers'} =~ /KV-Operation:\s*(?:DEL|PURGE)|Nats-Marker-Reason:\s*(?:MaxAge|Purge|Remove)/i ? : 1 14 15
47 ? 1 : 0;
48 }
49  
50 25 ▶sub json_api {
Subroutine json_api: called CC=2 SCAR=6.9
Pod undocumented
51 25 my ($self, $subj, $data, $cb) = @_;
52 25 ▶ my $payload = defined $data ? JSON::PP::encode_json($data) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $data ? : 13 12 25
53 25 $self->{nats}->request("$self->{prefix}.$subj", $payload, $self->decoded($cb), $self->{timeout});
54 }
55  
56 # a request callback decoding the reply for $cb; API errors become strings
57 58 ▶sub decoded {
Subroutine decoded: called CC=1 SCAR=0.0
Pod undocumented
58 58 my ($self, $cb) = @_;
59 47 ▶ return sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
60 47 my ($resp, $err) = @_;
61 47 ▶ return $cb->(undef, $err, $self->err_status($err)) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 26 21 47
62 21 my ($decoded, $derr) = decode_json_or_error($resp);
63 21 ▶ return $cb->(undef, $derr) if $derr;
Branch: 1 branch
BranchTrueFalseTotal
if $derr 0 21 21
64 21 ▶ my $e = ref $decoded eq 'HASH' ? $decoded->{error} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $decoded eq 'HASH' ? : 20 1 21
65 21 ▶ return $cb->(undef, "$e->{description} (code $e->{code})", $self->{with_status} ? $e : ())
Branch: 2 branches
BranchTrueFalseTotal
if $e 5 16 21
$self->{'with_status'} ? : 2 3 5
66 if $e;
67 16 $cb->($decoded, undef);
68 58 };
69 }
70  
71 27 ▶sub err_status {
Subroutine err_status: called CC=3 SCAR=11.1
Pod undocumented
72 27 my ($self, $err) = @_;
73 27 ▶ return $self->{with_status} && $err eq 'no responders' ? ({ code => 503, description => '' }) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'with_status'} && $err eq 'no responders' ? : 1 26 27
Condition66%2/3
$self->{"with_status"} and $err eq "no responders"
!ll&&!rl&&r
2601
MC/DC50%1/2
$self->{"with_status"} and $err eq "no responders"
$self->{"with_status"} $err eq "no responders"
Truth table66%2/3
A and B
A= $self->{"with_status"}B= $err eq "no responders"
ABresult
0X0
100
111
74 }
75  
76 8 ▶sub later {
Subroutine later: called CC=1 SCAR=0.0
Pod undocumented
77 8 my ($nats, $code) = @_;
78 8 my $t;
79 8 ▶ $t = $nats->timer(0, 0, sub { undef $t; $code->() });
Subroutine __ANON__: called CC=1 SCAR=0.0
80 8 return;
81 }
82  
83 # report a croak through $cb on the next loop iteration
84 5 ▶sub fail_later {
Subroutine fail_later: called CC=3 SCAR=11.6
Pod undocumented
85 5 my ($nats, $cb, $err) = @_;
86 5 ▶ $err = 'not connected' unless defined $err && length $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err and length $err 0 5 5
Condition33%1/3
defined $err and length $err
!ll&&!rl&&r
005
MC/DC0%0/2
defined $err and length $err
defined $err length $err
Truth table33%1/3
A and B
A= defined $errB= length $err
ABresult
0X0
100
111
87 5 $err =~ s/ at \S+ line \d+.*//s;
88 5 $err =~ s/\n\z//;
89 5 ▶ later($nats, sub { $cb->(undef, $err) });
Subroutine __ANON__: called CC=1 SCAR=0.0
90 }
91  
92 # the subjects under $prefix that hold messages, without the prefix; the
93 # server pages them (100k per reply), so follow offset up to total
94 3 ▶sub subjects_under {
Subroutine subjects_under: called CC=3 SCAR=11.0
Pod undocumented
95 3 my ($self, $stream, $prefix, $cb, $got, $offset) = @_;
96 3 ▶ $got //= {};
Condition100%2/2
$got //= {}
l!l
12
MC/DC100%1/1
$got //= {}
$got
Truth table100%2/2
A
A= $got
Aresult
11
00
97 3 ▶ $self->stream_info($stream, { subjects_filter => "$prefix>", ($offset ? (offset => $offset) : ()) }, sub {
Subroutine __ANON__: called CC=8 SCAR=21.0
98 3 my ($info, $err) = @_;
99 3 ▶ return $cb->(undef, $err) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 3 3
100 3 ▶ my $page = $info->{state}{subjects} || {};
Condition50%1/2
$info->{'state'}{'subjects'} || {}
l!l
30
MC/DC0%0/1
$info->{'state'}{'subjects'} || {}
$info->{'state'}{'subjects'}
Truth table50%1/2
A
A= $info->{'state'}{'subjects'}
Aresult
11
00
101 3 my $plen = length $prefix;
102 3 $got->{ substr $_, $plen } = 1 for grep { substr($_, 0, $plen) eq $prefix } keys %$page;
103 3 ▶ my $next = ($info->{offset} || 0) + keys %$page;
Condition100%2/2
$info->{'offset'} || 0
l!l
12
MC/DC100%1/1
$info->{'offset'} || 0
$info->{'offset'}
Truth table100%2/2
A
A= $info->{'offset'}
Aresult
11
00
104 return $self->subjects_under($stream, $prefix, $cb, $got, $next)
105 3 ▶ if %$page && $info->{total} && $next < $info->{total};
Branch: 1 branch
BranchTrueFalseTotal
if %$page and $info->{"total"} and $next < $info->{"total"} 1 2 3
Condition83%5/6
%$page and $info->{"total"} and $next < $info->{"total"}
!ll&&!rl&&r
111
%$page and $info->{"total"}
!ll&&!rl&&r
012
MC/DC66%2/3
%$page and $info->{"total"} and $next < $info->{"total"}
%$page $info->{"total"} $next < $info->{"total"}
Truth table75%3/4
A and B and C
A= %$pageB= $info->{"total"}C= $next < $info->{"total"}
ABCresult
0XX0
10X0
1100
1111
106 2 $cb->([ keys %$got ], undef);
107 3 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
$offset ? : 1 2 3
108 }
109  
110 # remember a stream's allow_direct from any reply carrying its config
111 9 ▶sub noting_direct {
Subroutine noting_direct: called CC=1 SCAR=0.0
Pod undocumented
112 9 my ($self, $name, $cb) = @_;
113 7 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
114 $self->{direct}{$name} = $_[0]{config}{allow_direct} ? 1 : 0
115 7 ▶ if ref $_[0] eq 'HASH' && $_[0]{config};
Branch: 2 branches
BranchTrueFalseTotal
if ref $_[0] eq "HASH" and $_[0]{"config"} 4 3 7
$_[0]{'config'}{'allow_direct'} ? : 2 2 4
Condition100%3/3
ref $_[0] eq "HASH" and $_[0]{"config"}
!ll&&!rl&&r
124
MC/DC100%2/2
ref $_[0] eq "HASH" and $_[0]{"config"}
ref $_[0] eq "HASH" $_[0]{"config"}
Truth table100%3/3
A and B
A= ref $_[0] eq "HASH"B= $_[0]{"config"}
ABresult
0X0
100
111
116 7 $cb->(@_);
117 9 };
118 }
119  
120 0 ▶sub stream_create {
Subroutine stream_create: not called CC=2 SCAR=17.9
Pod documented
121 0 my ($self, $config, $cb) = @_;
122 0 ▶ my $name = $config->{name} || die "stream name required";
Condition0%0/2
$config->{'name'} || die('stream name required')
l!l
00
MC/DC0%0/1
$config->{'name'} || die('stream name required')
$config->{'name'}
Truth table0%0/2
A
A= $config->{'name'}
Aresult
11
00
123 0 $self->json_api("STREAM.CREATE.$name", $config, $self->noting_direct($name, $cb));
124 }
125  
126 0 ▶sub stream_update {
Subroutine stream_update: not called CC=2 SCAR=17.9
Pod documented
127 0 my ($self, $config, $cb) = @_;
128 0 ▶ my $name = $config->{name} || die "stream name required";
Condition0%0/2
$config->{'name'} || die('stream name required')
l!l
00
MC/DC0%0/1
$config->{'name'} || die('stream name required')
$config->{'name'}
Truth table0%0/2
A
A= $config->{'name'}
Aresult
11
00
129 0 $self->json_api("STREAM.UPDATE.$name", $config, $self->noting_direct($name, $cb));
130 }
131  
132 # A KV or ObjectStore bucket made before allow_direct differs from $config
133 # only there: turn it on, keeping the stream's other settings, as nats.go
134 # does. A difference in a field the bucket sets is still refused. The
135 # server leaves false booleans out of the config it reports, and these
136 # limits when unset.
137 5 my %BUCKET_DEFAULT = (max_bytes => -1, max_age => 0, num_replicas => 1);
138  
139 1 ▶sub create_bucket_stream {
Subroutine create_bucket_stream: called CC=1 SCAR=6.2
Pod undocumented
140 1 my ($self, $config, $cb) = @_;
141 0 ▶ $self->stream_create($config, sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
142 0 my ($info, $err) = @_;
143 0 ▶ return $cb->($info, $err) unless $err && $err =~ /already in use with a different configuration/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $err and $err =~ /already in use with a different configuration/i 0 0 0
Condition0%0/3
$err and $err =~ /already in use with a different configuration/i
!ll&&!rl&&r
000
MC/DC0%0/2
$err and $err =~ /already in use with a different configuration/i
$err $err =~ /already in use with a different configuration/i
Truth table0%0/3
A and B
A= $errB= $err =~ /already in use with a different configuration/i
ABresult
0X0
100
111
144 0 ▶ $self->stream_info($config->{name}, sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
145 0 ▶ my $have = $_[0] && $_[0]{config} or return $cb->(undef, $err);
Branch: 1 branch
BranchTrueFalseTotal
my $have = $_[0] && $_[0]{"config"} or return &$cb(undef, $err) 0 0 0
Condition0%0/3
$_[0] && $_[0]{'config'}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] && $_[0]{'config'}
$_[0] $_[0]{'config'}
Truth table0%0/3
A && B
A= $_[0]B= $_[0]{'config'}
ABresult
0X0
100
111
146 0 for my $k (grep { $_ ne 'allow_direct' } keys %$config, keys %BUCKET_DEFAULT) {
147 my ($x, $y) = map {
148 0 ▶ ref($_) eq 'ARRAY' ? join("\0", @$_) : ref($_) || !defined($_) ? ($_ ? 1 : 0) : "$_"
Branch: 3 branches
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 0 0 0
ref $_ || !defined($_) ? : 0 0 0
$_ ? : 0 0 0
Condition0%0/3
ref $_ or not defined $_
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_ or not defined $_
ref $_ not defined $_
Truth table0%0/3
A or not(B)
A= ref $_B= not defined $_
ABresult
1X1
011
000
149 0 ▶ } map { $_->{$k} // $BUCKET_DEFAULT{$k} } $have, $config;
Condition0%0/3
$_->{$k} // $BUCKET_DEFAULT{$k}
l!l&&r!l&&!r
000
MC/DC0%0/2
$_->{$k} // $BUCKET_DEFAULT{$k}
$_->{$k} $BUCKET_DEFAULT{$k}
Truth table0%0/3
A // B
A= $_->{$k}B= $BUCKET_DEFAULT{$k}
ABresult
1X1
011
000
150 0 ▶ return $cb->(undef, $err) if $x ne $y;
Branch: 1 branch
BranchTrueFalseTotal
if $x ne $y 0 0 0
151 }
152 0 ▶ $self->stream_update({ %$have, allow_direct => JSON::PP::true() }, sub { $cb->(@_[0, 1]) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
153 0 });
154 1 });
155 }
156  
157 1 ▶sub stream_delete {
Subroutine stream_delete: called CC=1 SCAR=0.0
Pod documented
158 1 my ($self, $name, $cb) = @_;
159 1 delete $self->{direct}{$name};
160 1 $self->json_api("STREAM.DELETE.$name", undef, $cb);
161 }
162  
163 9 ▶sub stream_info {
Subroutine stream_info: called CC=1 SCAR=0.0
Pod documented
164 9 my ($self, $name, @rest) = @_;
165 9 my $cb = pop @rest;
166 9 my $opts = $rest[0];
167 9 $self->json_api("STREAM.INFO.$name", $opts, $self->noting_direct($name, $cb));
168 }
169  
170 # $leader: read through the stream leader. A Direct Get may be answered by
171 # a replica that has not applied the latest write yet.
172 79 ▶sub get_msg {
Subroutine get_msg: called CC=6 SCAR=17.9
Pod undocumented
173 79 my ($self, $stream, $opts, $cb, $leader) = @_;
174 79 ▶ return $self->get_msg_using($stream, $opts, 0, $cb) if $leader;
Branch: 1 branch
BranchTrueFalseTotal
if $leader 57 22 79
175 22 my $direct = $self->{direct}{$stream};
176 22 ▶ return $self->get_msg_using($stream, $opts, $direct, $cb) if defined $direct;
Branch: 1 branch
BranchTrueFalseTotal
if defined $direct 2 20 22
177 20 ▶ my $waiting = $self->{direct_wait}{$stream} //= [];
Condition100%2/2
$self->{"direct_wait"}{$stream} //= []
l!l
218
MC/DC100%1/1
$self->{"direct_wait"}{$stream} //= []
$self->{"direct_wait"}{$stream}
Truth table100%2/2
A
A= $self->{"direct_wait"}{$stream}
Aresult
11
00
178 20 push @$waiting, [$opts, $cb];
179 20 ▶ return if @$waiting > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$waiting > 1 2 18 20
180 18 my $asked = eval {
181 16 ▶ $self->stream_info($stream, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
182 16 my ($info, $err) = @_;
183 16 for (@{ delete $self->{direct_wait}{$stream} }) {
184 18 my ($o, $c) = @$_;
185 18 ▶ $err ? $c->(undef, $err) : $self->get_msg_using($stream, $o, $self->{direct}{$stream}, $c);
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 1 17 18
186 }
187 18 });
188 16 1;
189 };
190 18 ▶ return if $asked;
Branch: 1 branch
BranchTrueFalseTotal
if $asked 16 2 18
191 2 delete $self->{direct_wait}{$stream};
192 2 die $@;
193 }
194  
195 77 ▶sub get_msg_using {
Subroutine get_msg_using: called CC=2 SCAR=6.9
Pod undocumented
196 77 my ($self, $stream, $opts, $direct, $cb) = @_;
197 77 ▶ if ($direct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($direct) 3 74 77
198 3 ▶ return $self->direct_get($stream, $opts, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
199 3 my ($msg, $err) = @_;
200 3 ▶ return $cb->($msg, $err) unless defined $err && $err eq 'no responders';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err and $err eq "no responders" 2 1 3
Condition66%2/3
defined $err and $err eq "no responders"
!ll&&!rl&&r
201
MC/DC50%1/2
defined $err and $err eq "no responders"
defined $err $err eq "no responders"
Truth table66%2/3
A and B
A= defined $errB= $err eq "no responders"
ABresult
0X0
100
111
201 1 $self->{direct}{$stream} = 0;
202 1 $self->get_msg_using($stream, $opts, 0, $cb);
203 3 });
204 }
205 74 ▶ $self->json_api("STREAM.MSG.GET.$stream", $opts, sub {
Subroutine __ANON__: called CC=6 SCAR=18.0
206 74 my ($resp, $err) = @_;
207 74 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 42 32 74
208 42 ▶ return $cb->(undef, undef) if $err =~ /no message found|\(code 10037\)/;
Branch: 1 branch
BranchTrueFalseTotal
if $err =~ /no message found|\(code 10037\)/ 41 1 42
209 1 return $cb->(undef, $err);
210 }
211 32 ▶ my $m = $resp->{message} or return $cb->(undef, undef);
Branch: 1 branch
BranchTrueFalseTotal
my $m = $resp->{"message"} or return &$cb(undef, undef) 32 0 32
212 32 require MIME::Base64;
213 $cb->({
214 subject => $m->{subject},
215 seq => $m->{seq},
216 time => $m->{time},
217 data => MIME::Base64::decode_base64($m->{data} // ''),
218 32 ▶ headers => defined $m->{hdrs} ? MIME::Base64::decode_base64($m->{hdrs}) : undef,
Branch: 1 branch
BranchTrueFalseTotal
defined $m->{'hdrs'} ? : 15 17 32
Condition50%1/2
$m->{"data"} // ""
l!l
320
MC/DC0%0/1
$m->{"data"} // ""
$m->{"data"}
Truth table50%1/2
A
A= $m->{"data"}
Aresult
11
00
219 }, undef);
220 74 });
221 }
222  
223 5 ▶sub direct_get {
Subroutine direct_get: called CC=1 SCAR=0.0
Pod documented
224 5 my ($self, $stream, $opts, $cb) = @_;
225 5 ▶ $self->{nats}->request("$self->{prefix}.DIRECT.GET.$stream", JSON::PP::encode_json($opts), sub {
Subroutine __ANON__: called CC=10 SCAR=23.6
226 5 my ($data, $err, $hdr) = @_;
227 5 ▶ return $cb->(undef, $err, $self->err_status($err)) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 4 5
228 4 ▶ $hdr //= '';
Condition50%1/2
$hdr //= ""
l!l
40
MC/DC0%0/1
$hdr //= ""
$hdr
Truth table50%1/2
A
A= $hdr
Aresult
11
00
229 4 ▶ if ($hdr =~ m{\ANATS/1\.0 (\d{3})(?: ([^\r\n]*))?}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ m[\ANATS/1\.0 (\d{3})(?: ([^\r\n]*))?]) 2 2 4
230 2 ▶ my ($code, $desc) = ($1, $2 // '');
Condition50%1/2
$2 // ""
l!l
20
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
231 2 ▶ return $cb->(undef, undef) if $code == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 404 1 1 2
232 return $cb->(undef, "direct get failed: NATS/1.0 $code" . (length $desc ? " $desc" : ''),
233 1 ▶ $self->{with_status} ? ({ code => 0 + $code, description => $desc }) : ());
Branch: 2 branches
BranchTrueFalseTotal
length $desc ? : 1 0 1
$self->{'with_status'} ? : 0 1 1
234 }
235 2 ▶ my %nats_hdr = map { /\ANats-([\w-]+):\s*(.*)\z/i ? (lc $1 => $2) : () } split /\r\n/, $hdr;
Branch: 1 branch
BranchTrueFalseTotal
/\ANats-([\w-]+):\s*(.*)\z/i ? : 8 3 11
236 $cb->({
237 subject => $nats_hdr{subject},
238 seq => defined $nats_hdr{sequence} ? 0 + $nats_hdr{sequence} : undef,
239 2 ▶ time => $nats_hdr{'time-stamp'},
Branch: 1 branch
BranchTrueFalseTotal
defined $nats_hdr{'sequence'} ? : 2 0 2
240 data => $data,
241 headers => $hdr,
242 }, undef);
243 5 }, $self->{timeout});
244 }
245  
246 1 ▶sub stream_list {
Subroutine stream_list: called CC=1 SCAR=0.0
Pod documented
247 1 my ($self, $cb) = @_;
248 1 $self->all_pages("STREAM.LIST", 'streams', $cb);
249 }
250  
251 # the API pages its lists (256 per reply): one reply with every page merged
252 2 ▶sub all_pages {
Subroutine all_pages: called CC=3 SCAR=11.2
Pod undocumented
253 2 my ($self, $subj, $key, $cb, $have) = @_;
254 2 ▶ $have //= [];
Condition100%2/2
$have //= []
l!l
11
MC/DC100%1/1
$have //= []
$have
Truth table100%2/2
A
A= $have
Aresult
11
00
255 2 ▶ $self->json_api($subj, @$have ? { offset => scalar @$have } : undef, sub {
Subroutine __ANON__: called CC=6 SCAR=19.1
256 2 my ($resp, $err) = @_;
257 2 ▶ return $cb->(@_) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 2 2
258 2 ▶ my $page = $resp->{$key} || [];
Condition50%1/2
$resp->{$key} || []
l!l
20
MC/DC0%0/1
$resp->{$key} || []
$resp->{$key}
Truth table50%1/2
A
A= $resp->{$key}
Aresult
11
00
259 2 push @$have, @$page;
260 return $self->all_pages($subj, $key, $cb, $have)
261 2 ▶ if @$page && $resp->{total} && @$have < $resp->{total};
Branch: 1 branch
BranchTrueFalseTotal
if @$page and $resp->{"total"} and @$have < $resp->{"total"} 1 1 2
Condition50%3/6
@$page and $resp->{"total"} and @$have < $resp->{"total"}
!ll&&!rl&&r
011
@$page and $resp->{"total"}
!ll&&!rl&&r
002
MC/DC33%1/3
@$page and $resp->{"total"} and @$have < $resp->{"total"}
@$page $resp->{"total"} @$have < $resp->{"total"}
Truth table50%2/4
A and B and C
A= @$pageB= $resp->{"total"}C= @$have < $resp->{"total"}
ABCresult
0XX0
10X0
1100
1111
262 1 $cb->({ %$resp, $key => $have, offset => 0, limit => scalar @$have }, undef);
263 2 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
@$have ? : 1 1 2
264 }
265  
266 0 ▶sub stream_purge {
Subroutine stream_purge: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($self, $name, $cb) = @_;
268 0 $self->json_api("STREAM.PURGE.$name", undef, $cb);
269 }
270  
271 0 ▶sub stream_msg_get {
Subroutine stream_msg_get: not called CC=1 SCAR=6.9
Pod documented
272 0 my ($self, $stream, $opts, $cb) = @_;
273 0 $self->json_api("STREAM.MSG.GET.$stream", $opts, $cb);
274 }
275  
276 0 ▶sub consumer_create {
Subroutine consumer_create: not called CC=3 SCAR=24.8
Pod documented
277 0 my ($self, $stream, $config, $cb) = @_;
278 0 ▶ my $name = $config->{durable_name} || $config->{name};
Condition0%0/3
$config->{'durable_name'} || $config->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->{'durable_name'} || $config->{'name'}
$config->{'durable_name'} $config->{'name'}
Truth table0%0/3
A || B
A= $config->{'durable_name'}B= $config->{'name'}
ABresult
1X1
011
000
279 0 ▶ my $subj = $name
Branch: 1 branch
BranchTrueFalseTotal
$name ? : 0 0 0
280 ? "CONSUMER.CREATE.$stream.$name"
281 : "CONSUMER.CREATE.$stream";
282 0 $self->json_api($subj, { stream_name => $stream, config => $config }, $cb);
283 }
284  
285 0 ▶sub consumer_delete {
Subroutine consumer_delete: not called CC=1 SCAR=6.9
Pod documented
286 0 my ($self, $stream, $consumer, $cb) = @_;
287 0 $self->json_api("CONSUMER.DELETE.$stream.$consumer", undef, $cb);
288 }
289  
290 3 ▶sub consumer_info {
Subroutine consumer_info: called CC=1 SCAR=0.0
Pod documented
291 3 my ($self, $stream, $consumer, $cb) = @_;
292 3 $self->json_api("CONSUMER.INFO.$stream.$consumer", undef, $cb);
293 }
294  
295 0 ▶sub consumer_list {
Subroutine consumer_list: not called CC=1 SCAR=6.9
Pod documented
296 0 my ($self, $stream, $cb) = @_;
297 0 $self->all_pages("CONSUMER.LIST.$stream", 'consumers', $cb);
298 }
299  
300 10 ▶sub js_publish {
Subroutine js_publish: called CC=1 SCAR=0.0
Pod documented
301 10 my ($self, $subject, $payload, $cb) = @_;
302 10 $self->{nats}->request($subject, $payload, $self->decoded($cb), $self->{timeout});
303 }
304  
305 23 ▶sub js_publish_h {
Subroutine js_publish_h: called CC=1 SCAR=0.0
Pod documented
306 23 my ($self, $subject, $headers, $payload, $cb) = @_;
307 23 $self->{nats}->hrequest($subject, $headers, $payload, $self->decoded($cb), $self->{timeout});
308 }
309  
310 # The server serves a pull until it expires even after a cancel or a
311 # reconnect, so the inbox stays open until then and late messages are -NAKed.
312 13 ▶sub fetch {
Subroutine fetch: called CC=9 SCAR=22.0
Pod documented
313 13 my ($self, $stream, $consumer, $opts, $cb) = @_;
314 13 ▶ $opts //= {};
Condition50%1/2
$opts //= {}
l!l
130
MC/DC0%0/1
$opts //= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
315 # numified: JSON::PP sends a string the server rejects for an integer
316 13 ▶ my $batch = 0 + ($opts->{batch} || 1);
Condition100%2/2
$opts->{'batch'} || 1
l!l
103
MC/DC100%1/1
$opts->{'batch'} || 1
$opts->{'batch'}
Truth table100%2/2
A
A= $opts->{'batch'}
Aresult
11
00
317 13 ▶ my $expires_ns = 0 + ($opts->{expires} || 5_000_000_000);
Condition100%2/2
$opts->{'expires'} || 5000000000
l!l
85
MC/DC100%1/1
$opts->{'expires'} || 5000000000
$opts->{'expires'}
Truth table100%2/2
A
A= $opts->{'expires'}
Aresult
11
00
318 13 ▶ my $no_wait = $opts->{no_wait} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'no_wait'} ? : 1 12 13
319 13 ▶ my $wait = $no_wait ? $self->{timeout} / 1000 : $expires_ns / 1e9 + 1;
Branch: 1 branch
BranchTrueFalseTotal
$no_wait ? : 1 12 13
320  
321 13 my $nats = $self->{nats};
322 13 my $inbox = $nats->new_inbox;
323  
324 13 my (@messages, $sid, $timer, $watch, $answered, $cancelled);
325 13 my $seen = 0;
326  
327 13 ▶ my $nak = sub { $self->nak($_) for grep { defined $_->{reply} } @_ };
Subroutine __ANON__: called CC=2 SCAR=6.9
328 12 ▶ my $close = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
329 12 ▶ return unless $timer;
Branch: 1 branch
BranchTrueFalseTotal
unless $timer 0 12 12
330 12 $timer->stop;
331 12 undef $timer;
332 12 ▶ $nats->unwatch_disconnect($watch) if defined $watch;
Branch: 1 branch
BranchTrueFalseTotal
if defined $watch 9 3 12
333 12 undef $watch;
334 12 eval { $nats->unsubscribe($sid) };
335 13 };
336 11 ▶ my $answer = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
337 11 ▶ return if $answered++;
Branch: 1 branch
BranchTrueFalseTotal
if $answered++ 4 7 11
338 7 $cb->(@_);
339 13 };
340  
341 8 ▶ $sid = $nats->subscribe($inbox, sub {
Subroutine __ANON__: called CC=11 SCAR=24.1
342 8 my ($subject, $payload, $reply, $headers) = @_;
343 # Status lines are exactly "NATS/1.0 <3 digits>"; a loose \s+ would
344 # swallow a headered DATA message's first header as a status frame.
345 8 ▶ if ($headers && $headers =~ m{\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($headers and $headers =~ m[\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)]) 5 3 8
Condition66%2/3
$headers and $headers =~ m[\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)]
!ll&&!rl&&r
305
MC/DC50%1/2
$headers and $headers =~ m[\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)]
$headers $headers =~ m[\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)]
Truth table66%2/3
A and B
A= $headersB= $headers =~ m[\A(NATS/1\.0 (\d{3})(?: ([^\r\n]*))?)(?:[\r\n]|\z)]
ABresult
0X0
100
111
346 5 ▶ my ($line, $code, $desc) = ($1, $2, $3 // '');
Condition50%1/2
$3 // ""
l!l
50
MC/DC0%0/1
$3 // ""
$3
Truth table50%1/2
A
A= $3
Aresult
11
00
347 # 100 is a heartbeat; 404/408 end the batch; anything else
348 # (409, 503, 400) is a failure the caller must see.
349 5 ▶ return if $code == 100;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 100 0 5 5
350 5 $close->();
351 5 ▶ return if $answered;
Branch: 1 branch
BranchTrueFalseTotal
if $answered 1 4 5
352 my @status = $self->{with_status}
353 4 ▶ ? ({ code => 0 + $code, description => $desc }) : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->{'with_status'} ? : 2 2 4
354 4 ▶ return $answer->([splice @messages], undef, @status)
Branch: 1 branch
BranchTrueFalseTotal
if $code == 404 or $code == 408 2 2 4
Condition66%2/3
$code == 404 or $code == 408
l!l&&r!l&&!r
202
MC/DC50%1/2
$code == 404 or $code == 408
$code == 404 $code == 408
Truth table66%2/3
A or B
A= $code == 404B= $code == 408
ABresult
1X1
011
000
355 if $code == 404 || $code == 408;
356 2 $nak->(splice @messages);
357 2 return $answer->(undef, "fetch failed: $line", @status);
358 }
359 3 my $msg = {
360 subject => $subject,
361 payload => $payload,
362 reply => $reply,
363 headers => $headers,
364 };
365 3 ▶ if ($answered) { $nak->($msg) } else { push @messages, $msg }
Branch: 1 branch
BranchTrueFalseTotal
if ($answered) { } 2 1 3
366 3 ▶ return if ++$seen < $batch;
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen < $batch 2 1 3
367 1 $close->();
368 1 $answer->([splice @messages]);
369 13 });
370  
371 13 my %req = (batch => $batch);
372 # no_wait with an expires is pointless: the server waits the expires
373 # out instead of answering immediately.
374 13 ▶ $req{expires} = $expires_ns unless $no_wait;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_wait 12 1 13
375 13 ▶ $req{no_wait} = JSON::PP::true() if $no_wait;
Branch: 1 branch
BranchTrueFalseTotal
if $no_wait 1 12 13
376 13 my $req_body = JSON::PP::encode_json(\%req);
377 13 my $subj = "$self->{prefix}.CONSUMER.MSG.NEXT.$stream.$consumer";
378 13 ▶ if (!eval { $nats->publish($subj, $req_body, $inbox); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $nats->publish($subj, $req_body, $inbox); 1 } }) 1 12 13
379 1 my $err = $@;
380 1 eval { $nats->unsubscribe($sid) };
381 1 die $err;
382 }
383  
384 6 ▶ $timer = $nats->timer($wait, 0, sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
385 6 $close->();
386 6 $answer->([splice @messages]);
387 12 });
388 3 ▶ $watch = $nats->watch_disconnect(sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
389 3 undef $watch;
390 3 ▶ return if $answered++;
Branch: 1 branch
BranchTrueFalseTotal
if $answered++ 0 3 3
391 3 my @undelivered = splice @messages;
392 3 ▶ later($nats, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
393 3 $nak->(@undelivered);
394 3 ▶ $cb->(undef, 'disconnected') unless $cancelled;
Branch: 1 branch
BranchTrueFalseTotal
unless $cancelled 3 0 3
395 3 });
396 12 });
397  
398 3 ▶ return bless sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
399 3 $cancelled = 1;
400 3 ▶ return if $answered++;
Branch: 1 branch
BranchTrueFalseTotal
if $answered++ 1 2 3
401 2 $nak->(splice @messages);
402 12 }, 'EV::Nats::JetStream::Fetch';
403 }
404  
405 3 ▶sub ack { $_[0]->ack_reply($_[1], '+ACK') }
Subroutine ack: called CC=1 SCAR=0.0
Pod documented
406 1 ▶sub in_progress { $_[0]->ack_reply($_[1], '+WPI') }
Subroutine in_progress: called CC=1 SCAR=0.0
Pod documented
407  
408 6 ▶sub nak {
Subroutine nak: called CC=3 SCAR=11.0
Pod documented
409 6 my ($self, $msg, $delay) = @_;
410 6 ▶ return $self->ack_reply($msg, '-NAK') unless defined $delay;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $delay 4 2 6
411 2 ▶ die "invalid nak delay '$delay'" unless $delay =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $delay =~ /\A[0-9]+\z/ 1 1 2
412 1 $self->ack_reply($msg, qq{-NAK {"delay":$delay}});
413 }
414  
415 2 ▶sub term {
Subroutine term: called CC=3 SCAR=11.1
Pod documented
416 2 my ($self, $msg, $reason) = @_;
417 2 ▶ $self->ack_reply($msg, defined $reason && length $reason ? "+TERM $reason" : '+TERM');
Branch: 1 branch
BranchTrueFalseTotal
defined $reason && length $reason ? : 1 1 2
Condition66%2/3
defined $reason and length $reason
!ll&&!rl&&r
101
MC/DC50%1/2
defined $reason and length $reason
defined $reason length $reason
Truth table66%2/3
A and B
A= defined $reasonB= length $reason
ABresult
0X0
100
111
418 }
419  
420 3 ▶sub ack_sync {
Subroutine ack_sync: called CC=3 SCAR=11.0
Pod documented
421 3 my ($self, $msg, $cb, $timeout_ms) = @_;
422 3 my $reply = reply_of($msg);
423 3 my $sent = eval {
424 2 ▶ $self->{nats}->request($reply, '+ACK', sub { $cb->($_[1] ? (undef, $_[1]) : (1, undef)) },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 1 1 2
425 3 ▶ $timeout_ms // $self->{timeout});
Condition66%2/3
$timeout_ms // $self->{"timeout"}
l!l&&r!l&&!r
120
MC/DC0%0/2
$timeout_ms // $self->{"timeout"}
$timeout_ms $self->{"timeout"}
Truth table66%2/3
A // B
A= $timeout_msB= $self->{"timeout"}
ABresult
1X1
011
000
426 2 1;
427 };
428 3 ▶ return 1 if $sent;
Branch: 1 branch
BranchTrueFalseTotal
if $sent 2 1 3
429 1 fail_later($self->{nats}, $cb, $@);
430 1 return 0;
431 }
432  
433 14 ▶sub reply_of {
Subroutine reply_of: called CC=4 SCAR=13.9
Pod undocumented
434 14 my ($msg) = @_;
435 14 ▶ my $reply = ref $msg ? $msg->{reply} : $msg;
Branch: 1 branch
BranchTrueFalseTotal
ref $msg ? : 9 5 14
436 14 ▶ die "message has no reply subject" unless defined $reply && length $reply;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $reply and length $reply 1 13 14
Condition66%2/3
defined $reply and length $reply
!ll&&!rl&&r
1013
MC/DC50%1/2
defined $reply and length $reply
defined $reply length $reply
Truth table66%2/3
A and B
A= defined $replyB= length $reply
ABresult
0X0
100
111
437 13 return $reply;
438 }
439  
440 11 ▶sub ack_reply {
Subroutine ack_reply: called CC=2 SCAR=6.9
Pod undocumented
441 11 my ($self, $msg, $body) = @_;
442 11 my $reply = reply_of($msg);
443 10 ▶ return eval { $self->{nats}->publish($reply, $body); 1 } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $self->{'nats'}->publish($reply, $body); 1 } } ? : 9 1 10
444 }
445  
446 package EV::Nats::JetStream::Fetch;
447  
448 3 ▶sub cancel { $_[0]->(); return }
Subroutine cancel: called CC=1 SCAR=0.0
449  
450 5 1;
451  
452 =head1 NAME
453  
454 EV::Nats::JetStream - JetStream API client for L<EV::Nats>
455  
456 =head1 SYNOPSIS
457  
458 use EV;
459 use EV::Nats;
460 use EV::Nats::JetStream;
461  
462 my $nats = EV::Nats->new(host => '127.0.0.1');
463 my $js = EV::Nats::JetStream->new(nats => $nats);
464  
465 $js->stream_create({ name => 'ORDERS', subjects => ['orders.>'] },
466 sub {
467 my ($info, $err) = @_;
468 die $err if $err;
469 $js->js_publish('orders.new', '{"item":"widget"}', sub {
470 my ($ack, $err) = @_;
471 print "stored at seq=$ack->{seq}\n";
472 });
473 });
474  
475 EV::run;
476  
477 =head1 DESCRIPTION
478  
479 Thin async wrapper over the JetStream C<$JS.API.*> request/reply
480 endpoints. Each method is a single request whose callback is invoked
481 with the decoded JSON response (or an error string). The C<$nats>
482 connection passed to L</new> handles all the actual I/O.
483  
484 L<EV::Nats::KV> and L<EV::Nats::ObjectStore> build on top of this
485 module -- see those for higher-level KV / blob APIs.
486  
487 =head1 METHODS
488  
489 All methods are async. Callbacks fire on the L<EV> loop.
490  
491 =head2 new
492  
493 new(%opts)
494  
495 my $js = EV::Nats::JetStream->new(
496 nats => $nats,
497 prefix => '$JS.API', # default API subject prefix
498 timeout => 5000, # ms; default 5000
499 with_status => 0, # see below
500 );
501  
502 With C<with_status>, callbacks receive a third argument when the server
503 reported a status: a JetStream API error's object,
504 C<< { code => 404, err_code => 10014, description => 'consumer not found' } >>,
505 C<< { code => 503, description => '' } >> for C<"no responders">, or the status that ended
506 a L</fetch>.
507  
508 =head2 Stream management
509  
510 =head3 stream_create
511  
512 stream_create($config, $cb)
513  
514 Create a stream. C<$config> is passed verbatim as the
515 C<StreamConfig> request body. Callback: C<($info, $err)>.
516  
517 =head3 stream_update
518  
519 stream_update($config, $cb)
520  
521 Update an existing stream. Same shape as C<stream_create>.
522  
523 =head3 stream_delete
524  
525 stream_delete($name, $cb)
526  
527 Delete the stream by name.
528  
529 =head3 stream_info
530  
531 stream_info($name, [\%opts], $cb)
532  
533 Fetch stream config + state. Optional C<\%opts> may include
534 C<subjects_filter> (e.g. C<E<gt>>) to populate C<state.subjects>;
535 without it the server omits that field for performance.
536  
537 =head3 stream_list
538  
539 stream_list($cb)
540  
541 List all streams' info, every page of the API's reply merged.
542  
543 =head3 stream_purge
544  
545 stream_purge($name, $cb)
546  
547 Purge all messages from the stream.
548  
549 =head3 stream_msg_get
550  
551 stream_msg_get($stream, \%opts, $cb)
552  
553 Fetch a single message from C<$stream>. C<\%opts> selects the message:
554  
555 { seq => $n } # by sequence number
556 { last_by_subj => $subject } # latest matching subject
557 { next_by_subj => $subject, seq => $start } # next at-or-after $start
558  
559 The message body and headers in the response are base64-encoded
560 under C<< $resp->{message}{data} >> and C<< $resp->{message}{hdrs} >>,
561 so a message larger than about 3/4 of C<max_payload> cannot be read
562 this way; see L</direct_get>.
563  
564 =head3 direct_get
565  
566 direct_get($stream, \%opts, $cb)
567  
568 Fetch a single message with Direct Get, from a stream created with
569 C<< allow_direct => true >>. Any replica may answer, so on a replicated
570 stream the result can trail a write that came just before.
571 C<\%opts> selects the message as for L</stream_msg_get>. Callback:
572 C<($msg, $err)>, with C<$msg>
573  
574 {
575 subject => 'orders.new',
576 seq => 42,
577 time => '2026-11-26T11:36:54.412Z',
578 data => '...', # raw bytes
579 headers => "...", # raw NATS/1.0 block, Nats-* headers included
580 }
581  
582 or C<undef> for a missing message.
583  
584 =head2 Consumer management
585  
586 =head3 consumer_create
587  
588 consumer_create($stream, $config, $cb)
589  
590 Create a consumer (push or pull). C<$config> is the consumer config
591 hashref; C<durable_name> makes it durable, C<ack_policy> controls
592 ack semantics.
593  
594 =head3 consumer_delete
595  
596 consumer_delete($stream, $consumer, $cb)
597  
598 =head3 consumer_info
599  
600 consumer_info($stream, $consumer, $cb)
601  
602 =head3 consumer_list
603  
604 consumer_list($stream, $cb)
605  
606 Every page of the API's reply merged, as for L</stream_list>.
607  
608 =head2 Publishing and fetching
609  
610 =head3 js_publish
611  
612 js_publish($subject, $payload, $cb)
613  
614 Publish with JetStream acknowledgment. Callback: C<($ack, $err)>
615 where C<$ack> is C<{ stream, seq, duplicate }>.
616  
617 =head3 js_publish_h
618  
619 js_publish_h($subject, $headers, $payload, $cb)
620  
621 Like L</js_publish> but sends C<$headers> (a full C<NATS/1.0> header
622 block, e.g. C<"NATS/1.0\r\nNats-Rollup: sub\r\n\r\n">) with the
623 message, waiting for the PubAck on a one-shot reply inbox. Used for
624 rollup publishes where the acknowledged sequence still matters
625 (L<EV::Nats::ObjectStore> metadata). Callback: C<($ack, $err)>.
626  
627 =head3 fetch
628  
629 my $fetch = fetch($stream, $consumer, \%opts, $cb)
630  
631 Pull messages from a pull-mode consumer. Options:
632  
633 =over
634  
635 =item C<batch>
636  
637 Maximum number of messages to fetch (default 1).
638  
639 =item C<expires>
640  
641 Server-side wait time in nanoseconds (default 5_000_000_000 = 5s).
642 Ignored with C<no_wait>.
643  
644 =item C<no_wait>
645  
646 If true, the server answers at once with what is available; a fetch
647 nobody answers ends after C<timeout>.
648  
649 =back
650  
651 Callback: C<(\@messages, $err)>. Each message is a hashref:
652  
653 {
654 subject => 'orders.new',
655 payload => '...',
656 reply => '$JS.ACK....', # for ack/nak/in_progress/term
657 headers => "...", # raw NATS/1.0 header block, or undef
658 }
659  
660 A 404 (no messages) or 408 (expired) status ends the batch; any other
661 status fails the fetch (C<"fetch failed: NATS/1.0 409 Server Shutdown">)
662 and C<-NAK>s the messages received so far. A dropped connection fails it
663 with C<"disconnected">. With C<with_status> the ending status is passed
664 as C<< { code => 409, description => 'Server Shutdown' } >>. An unknown
665 stream or consumer may get no answer at all: the fetch then ends with an
666 empty batch.
667  
668 Returns a handle: C<< $fetch->cancel >> ends the fetch without calling
669 C<$cb>; messages that still arrive for the pull before it expires are
670 C<-NAK>ed.
671  
672 =head3 ack
673  
674 ack($msg)
675  
676 =head3 nak
677  
678 nak($msg, [$delay_ns])
679  
680 Redeliver, after C<$delay_ns> nanoseconds if given.
681  
682 =head3 in_progress
683  
684 in_progress($msg)
685  
686 Extend the message's C<ack_wait>.
687  
688 =head3 term
689  
690 term($msg, [$reason])
691  
692 Never redeliver the message.
693  
694 These take a fetched message hashref or its reply subject, and return
695 true once the acknowledgement is sent or queued, or false when it
696 cannot be (C<$@> holds why). They die on a message without a reply
697 subject.
698  
699 =head3 ack_sync
700  
701 ack_sync($msg, $cb, [$timeout_ms])
702  
703 Acknowledge and wait for the server to confirm it applied the ack.
704 Callback: C<($ok, $err)>; C<$err> is C<"request timeout"> when nothing
705 answers (e.g. a server in lame duck mode, or a consumer that is gone),
706 C<"no responders"> for a gone consumer when the connection has
707 C<no_responders> enabled, or C<"disconnected">. C<$timeout_ms> defaults
708 to the JetStream C<timeout>.
709  
710 =head1 SEE ALSO
711  
712 L<EV::Nats>, L<EV::Nats::KV>, L<EV::Nats::ObjectStore>,
713 L<JetStream API reference|https://docs.nats.io/reference/reference-protocols/nats_api_reference>.
714  
715 =cut
« blib/lib/EV/Nats/KV.pm ↑ Summary blib/lib/EV/Nats/ObjectStore.pm »