weir_client/lib.rs
1//! Synchronous blocking client for the weir daemon.
2//!
3//! Connects over a Unix domain socket (or TCP + mutual TLS behind the `tls`
4//! feature) and exchanges frames using the weir wire protocol v1. Each method
5//! issues one request and reads one response — no pipelining. For concurrent
6//! producers, create one [`WeirClient`] per thread.
7//!
8//! # Platforms
9//!
10//! The Unix-socket transport is, unsurprisingly, Unix-only. **The TCP + mutual
11//! TLS transport is not** — it builds wherever rustls does, Windows included,
12//! as of 2.0.3. Before then it was gated on `unix` by accident: it lives in its
13//! own module but imported two types that happened to sit in the Unix one.
14//!
15//! # Throughput: one record per round-trip
16//!
17//! [`push`](WeirClient::push) is synchronous: it sends one frame and blocks for
18//! the ack before returning, so one connection carries one record at a time.
19//! What that costs depends on the tier, and the two are bounded by different
20//! things:
21//!
22//! - **`Buffered`** is round-trip bound. The daemon acks from memory, so the
23//! ceiling is roughly `1 / RTT`.
24//! - **`Durable`** is fsync bound, not RTT bound. The ack waits on the daemon's
25//! fsync, which dominates the round trip by orders of magnitude — batching at
26//! the daemon amortises it across concurrent producers, which is why fanning
27//! out helps this tier *more* than it helps `Buffered`.
28//!
29//! Measured on one machine (Apple M3 Max, Unix socket, 256-byte records): a bare
30//! AF_UNIX round trip is **~7 µs**, and a single client reaches **~32,000 rec/s
31//! `Buffered`** and **~5,700 rec/s `Durable`**. On a 2-vCPU CI runner the same
32//! figures are ~13,900 and ~3,700. Treat these as the shape of the problem, not
33//! as a spec: they move with hardware, filesystem, and record size. An earlier
34//! version of this paragraph quoted "~50 µs RTT" and "~20k rec/s", which matched
35//! neither tier on any machine we have measured.
36//!
37//! To go faster, **fan out across connections**: create one `WeirClient` per
38//! producer thread (they're independent and the daemon handles many
39//! concurrently). Ordering is only guaranteed within a single connection's
40//! sequential pushes, not across connections.
41//!
42//! **A pooled client needs no protocol change at all** — the wire protocol
43//! already permits several frames in flight on one connection, and fanning out
44//! across connections is measurable today: roughly 5× on `Buffered` at 16
45//! connections and 9× on `Durable` at 48. A built-in batched-push *frame* is the
46//! part that needs new wire machinery. An earlier version of this paragraph
47//! attributed the protocol cost to both and so steered readers away from the one
48//! option that already works.
49//!
50//! # Example
51//!
52//! ```no_run
53//! # #[cfg(unix)] {
54//! use weir_client::{WeirClient, Durability}; // Durability is re-exported from weir-core
55//!
56//! let mut client = WeirClient::connect("/run/weir/weir.sock").unwrap();
57//! client.push(b"hello world", Durability::Durable).unwrap();
58//! # }
59//! ```
60//!
61//! # Ack vs. delivery
62//!
63//! A successful [`push`](WeirClient::push) means the record is **durably buffered
64//! at the requested [`Durability`] tier** — fsync'd to the write-ahead buffer for
65//! [`Durable`](Durability::Durable), in memory for
66//! [`Buffered`](Durability::Buffered). It does **not** mean the record has reached
67//! your downstream sink yet: the daemon drains buffered records to the sink in
68//! batches, only once a WAB segment seals (its size threshold, or daemon
69//! shutdown). For a small smoke test the sink may not be touched at all — watch
70//! `weir_records_ack_total` (acceptance), not the sink-commit metric, to confirm
71//! the daemon took your records.
72//!
73//! # Running the daemon
74//!
75//! This crate is the producer side; the daemon is the `weir-server` binary:
76//!
77//! ```text
78//! mkdir -p /run/weir/wab # the daemon does not create its directories
79//! weir-server --wab-dir /run/weir/wab --socket-path /run/weir/weir.sock
80//! ```
81//!
82//! `--wab-dir` must already exist. On macOS, do not place the socket directly in
83//! `/tmp` (it is a symlink the hardened bind rejects) — use a dedicated `0700`
84//! directory. Run `weir-server --help` for the full option list.
85//!
86//! # Observability
87//!
88//! The daemon serves Prometheus metrics at `127.0.0.1:9185/metrics` by default.
89//! The counters a producer cares about are labelled by tier/reason, e.g.
90//! `weir_records_accepted_total{tier="durable"}`,
91//! `weir_records_ack_total{tier="durable"}`, and
92//! `weir_records_nack_total{tier="durable",reason="empty_payload"}`.
93#![deny(missing_docs)]
94
95/// Transport-agnostic client core. Split out of `unix` so that `tls` — which
96/// needs a `TcpStream` and rustls, and nothing Unix-specific — is gated on the
97/// feature alone rather than on the platform.
98mod client;
99
100pub use client::{ClientError, DefaultTransport, WeirClient};
101
102#[cfg(unix)]
103mod unix;
104
105/// Re-export of [`weir_core::Durability`] so the common producer path needs a
106/// single crate import (`weir_client::Durability`).
107pub use weir_core::Durability;
108
109/// Re-export of [`weir_core::NackReason`] — the payload of [`ClientError::Nack`].
110/// Re-exported so consumers can match on the reason (e.g. to distinguish the
111/// connection-closing Nacks) without taking a direct dependency on `weir-core`.
112pub use weir_core::NackReason;
113
114#[cfg(feature = "tls")]
115mod tls;
116
117#[cfg(feature = "tls")]
118pub use tls::{ClientTlsConfig, TlsStream};