SessionBuilder
SessionBuilder is the supported configuration entry point:
use std::sync::Arc;
use std::time::Duration;
use ergo_aeron_cluster::{SessionBuilder, StaticCredentials};
fn main() -> Result<(), ergo_aeron_cluster::ClusterError> {
let session = SessionBuilder::default()
.ingress_channel("aeron:udp?endpoint=localhost:9010")?
.egress_channel("aeron:udp?endpoint=localhost:9020")?
.credentials(Arc::new(StaticCredentials::from_utf8("user:pass")))
.message_timeout(Duration::from_secs(5))?;
session.validate()?;
let mut client = session.connect("/path/to/aeron-dir")?;
client.offer(b"application payload")?;
client.close()
}
Connection remains poll-driven internally; the crate does not require Tokio or
another async runtime. AsyncClusterConnect is a poll-driven Aeron state
machine, not a Rust Future. When the application owns the poll loop, the
shortest supported path calls SessionBuilder, connect_async, default_idle,
poll_connect_until_done and finish in that order:
#![allow(unused)]
fn main() {
use ergo_aeron_cluster::{default_idle, poll_connect_until_done, SessionBuilder};
fn connect(aeron_dir: &str) -> Result<ergo_aeron_cluster::AeronCluster, ergo_aeron_cluster::ClusterError> {
let builder = SessionBuilder::default()
.ingress_channel("aeron:udp?endpoint=localhost:9010")?
.egress_channel("aeron:udp?endpoint=localhost:9020")?;
let mut connecting = builder.connect_async(aeron_dir);
let mut idle = default_idle();
poll_connect_until_done(&mut connecting, &mut idle)?;
connecting.finish()
}
}
Drive poll / step yourself when you need to inspect connect steps or plug
in a custom idle strategy.
No poll waits on the media driver. AsyncClusterConnect::poll adds the egress
subscription and ingress publication, and the publication a REDIRECT asks
for, with Aeron’s async add and picks them up on later polls. poll
handles a NewLeaderEvent the same way: the session stays AwaitingNewLeader
until the new leader’s publication has connected, for up to the message
timeout. Only the first poll, which creates the Aeron client, can wait, and
only while the media driver is not running yet.
Check message sizes once, at start-up: offer takes payloads up to
ingress_max_message_length() and try_claim up to
ingress_max_payload_length() (when smaller), each less
AeronCluster::SESSION_HEADER_LENGTH. A REDIRECT or a new leader re-adds the
configured ingress channel, changing only a unicast endpoint (IPC and
multicast channels stay as they are), so the limits hold across failover.
Multi-member ingress: ingress_endpoints
For first-connect against a cluster whose current leader isn’t known ahead of
time, use ingress_endpoints instead of (or alongside) ingress_channel:
let session = SessionBuilder::default()
.ingress_endpoints("0=host-a:9010,1=host-b:9010,2=host-c:9010")?
.egress_channel("aeron:udp?endpoint=localhost:9020")?;
The grammar is member_id=host:port pairs separated by commas, with no aeron:
prefix, member IDs need not be sorted or contiguous. The setter parses and
validates eagerly: a missing =, non-numeric member ID, empty endpoint,
empty map, or duplicate member ID fails immediately at the call site rather
than at validate() or the first poll().
The fan-out sends one SessionConnectRequest, not one per member, as Java’s
client does: a second copy reaching the leader would leave a session behind.
The fan-out skips a member whose publication cannot be added and connects
through the others. The add’s error ends the connect only once no member is
left. A REDIRECT keeps only the leader’s member, so a leader that cannot be
added still ends the connect. Java’s client skips such a member too, but adds it
again after a “temporarily unavailable” error; this client does not. A connect
that times out names the skipped members in ClusterError::ConnectFailed, on
connect_async too, in place of its ClusterError::Timeout.
Animation, in words: with three endpoints, connect (and connect_async)
starts an async add of
one ingress publication per member. It takes the first publication that
connects (here member 0, a follower; illustrative) and sends one
SessionConnectRequest with a fresh correlation id (7) on it alone. The
follower answers SessionEvent(REDIRECT) echoing 7 and naming leader member 2.
The client drops member 0’s publication, closes member 1’s, and keeps member
2’s, which has been connecting since the start, so no new add is needed
(when the redirect names the same endpoint string as the map). It
sends a new request with id 8 once that publication connects; only a
SessionEvent or Challenge echoing the current id counts. The leader
answers SessionEvent(OK) echoing 8, and the client keeps only the leader’s
publication: one session, two round trips (one if the first member to
connect leads). When the leader is already known, a single
ingress_channel is cheaper: the other publications exist only to be closed.