EV-Nats

 view release on metacpan or  search on metacpan

lib/EV/Nats/JetStream.pm  view on Meta::CPAN

sub ack_sync {
    my ($self, $msg, $cb, $timeout_ms) = @_;
    my $reply = reply_of($msg);
    my $sent = eval {
        $self->{nats}->request($reply, '+ACK', sub { $cb->($_[1] ? (undef, $_[1]) : (1, undef)) },
            $timeout_ms // $self->{timeout});
        1;
    };
    return 1 if $sent;
    fail_later($self->{nats}, $cb, $@);
    return 0;
}

sub reply_of {
    my ($msg) = @_;
    my $reply = ref $msg ? $msg->{reply} : $msg;
    die "message has no reply subject" unless defined $reply && length $reply;
    return $reply;
}

sub ack_reply {
    my ($self, $msg, $body) = @_;
    my $reply = reply_of($msg);
    return eval { $self->{nats}->publish($reply, $body); 1 } ? 1 : 0;
}

package EV::Nats::JetStream::Fetch;

sub cancel { $_[0]->(); return }

1;

=head1 NAME

EV::Nats::JetStream - JetStream API client for L<EV::Nats>

=head1 SYNOPSIS

    use EV;
    use EV::Nats;
    use EV::Nats::JetStream;

    my $nats = EV::Nats->new(host => '127.0.0.1');
    my $js   = EV::Nats::JetStream->new(nats => $nats);

    $js->stream_create({ name => 'ORDERS', subjects => ['orders.>'] },
                       sub {
        my ($info, $err) = @_;
        die $err if $err;
        $js->js_publish('orders.new', '{"item":"widget"}', sub {
            my ($ack, $err) = @_;
            print "stored at seq=$ack->{seq}\n";
        });
    });

    EV::run;

=head1 DESCRIPTION

Thin async wrapper over the JetStream C<$JS.API.*> request/reply
endpoints. Each method is a single request whose callback is invoked
with the decoded JSON response (or an error string). The C<$nats>
connection passed to L</new> handles all the actual I/O.

L<EV::Nats::KV> and L<EV::Nats::ObjectStore> build on top of this
module -- see those for higher-level KV / blob APIs.

=head1 METHODS

All methods are async. Callbacks fire on the L<EV> loop.

=head2 new

    new(%opts)

    my $js = EV::Nats::JetStream->new(
        nats        => $nats,
        prefix      => '$JS.API',   # default API subject prefix
        timeout     => 5000,        # ms; default 5000
        with_status => 0,           # see below
    );

With C<with_status>, callbacks receive a third argument when the server
reported a status: a JetStream API error's object,
C<< { code => 404, err_code => 10014, description => 'consumer not found' } >>,
C<< { code => 503, description => '' } >> for C<"no responders">, or the status that ended
a L</fetch>.

=head2 Stream management

=head3 stream_create

    stream_create($config, $cb)

Create a stream. C<$config> is passed verbatim as the
C<StreamConfig> request body. Callback: C<($info, $err)>.

=head3 stream_update

    stream_update($config, $cb)

Update an existing stream. Same shape as C<stream_create>.

=head3 stream_delete

    stream_delete($name, $cb)

Delete the stream by name.

=head3 stream_info

    stream_info($name, [\%opts], $cb)

Fetch stream config + state. Optional C<\%opts> may include
C<subjects_filter> (e.g. C<E<gt>>) to populate C<state.subjects>;
without it the server omits that field for performance.

=head3 stream_list

    stream_list($cb)



( run in 1.117 second using v1.01-cache-2.11-cpan-eb134014566 )