agents wake on send: broker.recv_blocking + 30s long-poll on Recv

This commit is contained in:
müde 2026-05-15 16:00:31 +02:00
parent f1fd787f17
commit dfbcf2b9d1
3 changed files with 50 additions and 4 deletions

View file

@ -65,7 +65,7 @@ async fn serve(stream: UnixStream, agent: String, broker: Arc<Broker>) -> Result
return Ok(());
}
let resp = match serde_json::from_str::<AgentRequest>(line.trim()) {
Ok(req) => dispatch(&req, &agent, &broker),
Ok(req) => dispatch(&req, &agent, &broker).await,
Err(e) => AgentResponse::Err {
message: format!("parse error: {e}"),
},
@ -77,7 +77,11 @@ async fn serve(stream: UnixStream, agent: String, broker: Arc<Broker>) -> Result
}
}
fn dispatch(req: &AgentRequest, agent: &str, broker: &Broker) -> AgentResponse {
/// How long the long-poll `Recv` holds a connection open waiting for new
/// mail. Set well below typical TCP/proxy idle limits.
const RECV_LONG_POLL: std::time::Duration = std::time::Duration::from_secs(30);
async fn dispatch(req: &AgentRequest, agent: &str, broker: &Broker) -> AgentResponse {
match req {
AgentRequest::Send { to, body } => {
match broker.send(&Message {
@ -91,7 +95,7 @@ fn dispatch(req: &AgentRequest, agent: &str, broker: &Broker) -> AgentResponse {
},
}
}
AgentRequest::Recv => match broker.recv(agent) {
AgentRequest::Recv => match broker.recv_blocking(agent, RECV_LONG_POLL).await {
Ok(Some(msg)) => AgentResponse::Message {
from: msg.from,
body: msg.body,