EV-Nats
view release on metacpan or search on metacpan
eg/health_checker.pl view on Meta::CPAN
my $mode = shift || 'both';
my $name = shift || "svc$$";
my $nats;
$nats = EV::Nats->new(
host => $ENV{NATS_HOST} // '127.0.0.1',
port => $ENV{NATS_PORT} // 4222,
on_error => sub { warn "nats: @_\n" },
on_connect => sub {
if ($mode eq 'service' || $mode eq 'both') {
# Register health endpoint
$nats->subscribe("health.$name", sub {
my ($subj, $payload, $reply) = @_;
return unless $reply;
# Simulate health: 90% healthy, 10% degraded
my $status = rand() < 0.9 ? 'healthy' : 'degraded';
$nats->publish($reply, "$name:$status:$$:" . int(rand(100)) . "ms");
});
print "service '$name' registered health endpoint\n";
if ($mode eq 'both') {
# Also register a second service for demo
$nats->subscribe("health.db", sub {
my ($subj, $payload, $reply) = @_;
return unless $reply;
$nats->publish($reply, "db:healthy:$$:2ms");
});
$nats->subscribe("health.cache", sub {
my ($subj, $payload, $reply) = @_;
lib/EV/Nats/JetStream.pm view on Meta::CPAN
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.
( run in 0.601 second using v1.01-cache-2.11-cpan-524268b4103 )