refactor(#1474): extract remaining dispatch_shared + hive-priv arms, drop their too_many_lines allows
This commit is contained in:
parent
02dcf4d028
commit
5804e986ce
2 changed files with 120 additions and 88 deletions
|
|
@ -113,14 +113,6 @@ pub(crate) fn recv_timeout(wait_seconds: Option<u64>) -> std::time::Duration {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(
|
||||
clippy::too_many_lines,
|
||||
reason = "flat routing table over the shared request variants — the \
|
||||
logic-bearing arms (recv / wake / set_status / get_agent_meta) \
|
||||
are extracted to handlers; what's left is one short broker \
|
||||
dispatch per variant, kept inline so the routing map reads \
|
||||
top-to-bottom"
|
||||
)]
|
||||
/// Handle the subset of `Request` variants that are identical on both
|
||||
/// the agent socket and the manager socket. Returns `Some(response)` for
|
||||
/// every variant it handles; returns `None` for variants with socket-specific
|
||||
|
|
@ -134,7 +126,6 @@ pub(crate) async fn dispatch_shared(
|
|||
agent: &str,
|
||||
coord: &Arc<Coordinator>,
|
||||
) -> Option<hive_sh4re::Response> {
|
||||
let broker = &coord.broker;
|
||||
Some(match req {
|
||||
hive_sh4re::Request::Send {
|
||||
to,
|
||||
|
|
@ -144,34 +135,14 @@ pub(crate) async fn dispatch_shared(
|
|||
hive_sh4re::Request::Recv { wait_seconds, max } => {
|
||||
handle_recv(coord, agent, *wait_seconds, *max).await
|
||||
}
|
||||
hive_sh4re::Request::Status => match broker.count_pending(agent) {
|
||||
Ok(unread) => hive_sh4re::Response::Status { unread },
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
},
|
||||
hive_sh4re::Request::OperatorMsg { body } => match broker.send(&Message {
|
||||
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
|
||||
to: agent.to_owned(),
|
||||
body: body.clone(),
|
||||
in_reply_to: None,
|
||||
}) {
|
||||
Ok(()) => hive_sh4re::Response::Ok,
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
},
|
||||
hive_sh4re::Request::Status => handle_status(coord, agent),
|
||||
hive_sh4re::Request::OperatorMsg { body } => handle_operator_msg(coord, agent, body),
|
||||
hive_sh4re::Request::Wake {
|
||||
from,
|
||||
body,
|
||||
transient,
|
||||
} => handle_wake(coord, agent, from, body, *transient),
|
||||
hive_sh4re::Request::Recent { limit } => match broker.recent_for(agent, *limit) {
|
||||
Ok(rows) => hive_sh4re::Response::Recent { rows },
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
},
|
||||
hive_sh4re::Request::Recent { limit } => handle_recent(coord, agent, *limit),
|
||||
hive_sh4re::Request::Ask {
|
||||
question,
|
||||
options,
|
||||
|
|
@ -212,23 +183,8 @@ pub(crate) async fn dispatch_shared(
|
|||
|()| hive_sh4re::Response::Ok,
|
||||
)
|
||||
}
|
||||
hive_sh4re::Request::AckTurn => match broker.ack_turn(agent) {
|
||||
Ok(_n) => hive_sh4re::Response::Ok,
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
},
|
||||
hive_sh4re::Request::RequeueInflight => match broker.requeue_inflight(agent) {
|
||||
Ok(n) => {
|
||||
if n > 0 {
|
||||
tracing::info!(%agent, requeued = %n, "requeued in-flight messages");
|
||||
}
|
||||
hive_sh4re::Response::Ok
|
||||
}
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
},
|
||||
hive_sh4re::Request::AckTurn => handle_ack_turn(coord, agent),
|
||||
hive_sh4re::Request::RequeueInflight => handle_requeue_inflight(coord, agent),
|
||||
hive_sh4re::Request::GetHostJournal {
|
||||
unit,
|
||||
container,
|
||||
|
|
@ -355,6 +311,70 @@ async fn handle_get_agent_meta(
|
|||
}
|
||||
}
|
||||
|
||||
/// `Status` — count of pending (unread) inbox messages for `agent`.
|
||||
fn handle_status(coord: &Arc<Coordinator>, agent: &str) -> hive_sh4re::Response {
|
||||
match coord.broker.count_pending(agent) {
|
||||
Ok(unread) => hive_sh4re::Response::Status { unread },
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// `OperatorMsg` — deliver an operator-authored message into `agent`'s
|
||||
/// inbox (from the `operator` recipient).
|
||||
fn handle_operator_msg(coord: &Arc<Coordinator>, agent: &str, body: &str) -> hive_sh4re::Response {
|
||||
match coord.broker.send(&Message {
|
||||
from: hive_sh4re::OPERATOR_RECIPIENT.to_owned(),
|
||||
to: agent.to_owned(),
|
||||
body: body.to_owned(),
|
||||
in_reply_to: None,
|
||||
}) {
|
||||
Ok(()) => hive_sh4re::Response::Ok,
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// `Recent` — the last `limit` inbox rows for `agent` (read-only,
|
||||
/// doesn't consume).
|
||||
fn handle_recent(coord: &Arc<Coordinator>, agent: &str, limit: u64) -> hive_sh4re::Response {
|
||||
match coord.broker.recent_for(agent, limit) {
|
||||
Ok(rows) => hive_sh4re::Response::Recent { rows },
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// `AckTurn` — mark `agent`'s in-flight delivered messages acked so
|
||||
/// they don't redeliver on the next turn.
|
||||
fn handle_ack_turn(coord: &Arc<Coordinator>, agent: &str) -> hive_sh4re::Response {
|
||||
match coord.broker.ack_turn(agent) {
|
||||
Ok(_n) => hive_sh4re::Response::Ok,
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// `RequeueInflight` — resurface `agent`'s unacked in-flight messages
|
||||
/// (crash recovery on harness boot).
|
||||
fn handle_requeue_inflight(coord: &Arc<Coordinator>, agent: &str) -> hive_sh4re::Response {
|
||||
match coord.broker.requeue_inflight(agent) {
|
||||
Ok(n) => {
|
||||
if n > 0 {
|
||||
tracing::info!(%agent, requeued = %n, "requeued in-flight messages");
|
||||
}
|
||||
hive_sh4re::Response::Ok
|
||||
}
|
||||
Err(e) => hive_sh4re::Response::Err {
|
||||
message: format!("{e:#}"),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
async fn dispatch(req: &AgentRequest, agent: &str, coord: &Arc<Coordinator>) -> AgentResponse {
|
||||
if let Some(resp) = dispatch_shared(req, agent, coord).await {
|
||||
return resp;
|
||||
|
|
|
|||
Loading…
Reference in a new issue