tawasuyu

tawasuyu/brahman

Rust 582TOML 222Markdown 83JSON 198635 Definitions

⎇ 3d55f189c0568ab8e600da66d7a42cd55dd22e2f crates/apps/brahman-demo/src/bin/broker.rs Rust · 68 lines · 2.3 KB History Who touched what raw

feat(brahman-demo): bootstrap script reproducible — broker + producer + consumer + 4 explorers 3d55f189c · Sergio ·

1//! `brahman-demo-broker` — broker standalone para demos.
2//!
3//! ente-zero (PID 1) es el broker "real" pero pesa toneladas
4//! (kernel surface, child subreaper, bus, brain, audit, etc). Este
5//! binario sólo arma `brahman_handshake::Server::bind` con un broker
6//! configurado, escucha en el socket default, y corre forever.
7//!
8//! Suficiente para que el script de bootstrap (y los 4 explorers)
9//! tengan algo a qué conectarse sin necesitar el bootstrap PID 1
10//! completo.
11//!
12//! Variables de entorno respetadas:
13//! - `BRAHMAN_INIT_SOCKET` — path del Unix socket. Default: el
14//!   resuelto por `brahman_handshake::transport::default_socket_path`.
15//! - `BRAHMAN_BROKER_CONTEXT` — context bias del broker (igual que
16//!   ente-zero); afecta priority_contexts si las Cards lo declaran.
17//! - `RUST_LOG` — filtro de tracing (default `info`).
18
19use std::sync::Arc;
20
21use brahman_broker::{Broker, BrokerConfig, MatchStrategy};
22use brahman_handshake::server::{Server, ServerConfig};
23use brahman_handshake::transport;
24use tokio::sync::Mutex;
25
26#[tokio::main(flavor = "current_thread")]
27async fn main() -> std::io::Result<()> {
28    tracing_subscriber::fmt()
29        .with_env_filter(
30            tracing_subscriber::EnvFilter::try_from_default_env()
31                .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")),
32        )
33        .init();
34
35    let context = std::env::var("BRAHMAN_BROKER_CONTEXT").ok();
36    let broker = Arc::new(Mutex::new(Broker::new(BrokerConfig {
37        strategy: MatchStrategy::default(),
38        current_context: context.clone(),
39    })));
40
41    let socket = transport::default_socket_path();
42    tracing::info!(
43        socket = %socket.display(),
44        context = ?context,
45        "brahman-demo-broker arranca"
46    );
47
48    let server = Server::bind(
49        &socket,
50        ServerConfig {
51            init_attached: false,
52            broker: Some(broker),
53            net: None,
54            policy: None,
55        },
56    )?;
57
58    // Loop accept-forever. Cada conexión va a su propia tokio task —
59    // sesiones independientes, ninguna bloquea a las otras.
60    loop {
61        let session = server.accept_one().await?;
62        tokio::spawn(async move {
63            if let Err(e) = session.handle().await {
64                tracing::warn!(?e, "session terminó con error");
65            }
66        });
67    }
68}