rust-async-mesh-patterns

Implements async Rust patterns for NATS JetStream messaging, retries, and circuit breakers.

3|1|Updated Nov 30, 2025
One-click install
npx skills add https://github.com/PALabs-v1/AI_friend --skill rust-async-mesh-patterns-palabs-v1
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rust-async-mesh-patterns
Source: https://github.com/PALabs-v1/AI_friend/tree/main/.claude/skills/rust-async-mesh-patterns
Command: npx skills add https://github.com/PALabs-v1/AI_friend --skill rust-async-mesh-patterns-palabs-v1

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Building reliable async Rust services that communicate over NATS JetStream and call flaky downstream HTTP dependencies is error-prone: messages get redelivered spuriously, circuit breakers trip on bad input instead of real outages, and health checks miss servers that return 200 with garbage bodies. This Skill encodes the correct patterns for each of these failure modes. ## Core Features & Use Cases - JetStream vs core NATS guidance: Explains when a raw core publish is sufficient to exercise a JetStream consumer, and how ack timing (AckWait) must be tuned for long-running handlers to avoid spurious redelivery. - Circuit breaker and retry patterns: Provides a concrete CircuitBreaker struct with half-open trial logic, plus retry-with-backoff that distinguishes non-retryable validation rejections via typed errors instead of string matching. - Readiness probes and shared state: Shows payload-checking background probes that catch "up but broken" servers, and safe Arc<Mutex<T>> usage across tokio tasks without holding locks across awaits. - Use Case: When modifying the voice-agent or stt-agent crates, apply these patterns to add a retry loop around a local inference server call without tripping the circuit breaker on deterministic input rejections. ## Quick Start Ask the assistant to review or write the retry and circuit-breaker logic for a Rust service that calls a downstream HTTP inference server over NATS.

Frequently Asked Questions about rust-async-mesh-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I implement a circuit breaker in async Rust?

Track a failure count, opened_at timestamp, and cooldown in a struct with is_open, record_success, and record_failure methods. Check is_open before each call, route Ok/Err into the record methods, and after cooldown allow exactly one half-open trial that re-arms the full cooldown on failure.

How do I retry HTTP requests with backoff in Rust tokio?

Loop up to MAX_ATTEMPTS, sleeping for increasing delays from a backoff array between attempts. Distinguish non-retryable errors with a typed marker struct checked via downcast_ref, and return immediately on deterministic rejections instead of wasting retry budget.

What is the difference between JetStream publish and core NATS publish?

JetStream publish gives durable, at-least-once delivery with replay and consumer groups, while core NATS publish is fire-and-forget. However, if a stream's subject wildcard covers the subject, a core publish still reaches the JetStream consumer, which is useful for one-off test messages.

Why does my NATS consumer redeliver messages that are still being processed?

The handler is outliving the consumer's AckWait, so the broker assumes failure and redelivers. Raise AckWait on that specific consumer to exceed the callback's worst-case runtime, or ack manually once work is durably queued elsewhere.

How do I test async Rust HTTP clients without a real server?

Use wiremock: start a MockServer, mount expected request/response pairs, and point the code under test at server.uri(). The .expect(N) setting asserts the exact call count, proving a retry fired or did not fire.

When should a circuit breaker not trip on downstream errors?

A deterministic validation rejection, such as malformed input, should not open the circuit or be retried because it will fail identically every time. Only errors indicating the downstream service is actually unhealthy should count toward the failure threshold.