Watch
0
0
Fork
You've already forked hyperhive
0

swarm_kv_put takes the bytes, not a one-variant enum

This commit is contained in:
iris 2026-11-16 23:21:17 +01:00 • committed by mara
commit f6a265421f
3 changed files with 8 additions and 15 deletions

View file

@ -26,7 +26,7 @@ use serde_json::Value;
use swarm_wire::agent_commands::{self, Skill, Skills}; use swarm_wire::agent_commands::{self, Skill, Skills};
use tokio::sync::watch; use tokio::sync::watch;
use crate::swarm_kv_put::{Failure, Put, put_retrying}; use crate::swarm_kv_put::{Failure, put_retrying};
/// The newest list claude reported, for [`run`] to publish. `None` until the /// The newest list claude reported, for [`run`] to publish. `None` until the
/// first init event of this process. /// first init event of this process.
@ -102,7 +102,7 @@ async fn run(agent: String) {
continue; continue;
} }
}; };
match put_retrying(&client, &subject, Put::Set(&payload), "the agent's skills").await { match put_retrying(&client, &subject, &payload, "the agent's skills").await {
Ok(()) => tracing::info!( Ok(()) => tracing::info!(
subject, subject,
count = skills.skills.len(), count = skills.skills.len(),

View file

@ -17,7 +17,7 @@ use swarm_wire::agent_todos::{
self, MAX_LABEL_CHARS, MAX_SUMMARY_CHARS, MAX_TODOS, Todo as WireTodo, Todos as WireTodos, self, MAX_LABEL_CHARS, MAX_SUMMARY_CHARS, MAX_TODOS, Todo as WireTodo, Todos as WireTodos,
}; };
use crate::swarm_kv_put::{Failure, Put, put_retrying}; use crate::swarm_kv_put::{Failure, put_retrying};
use crate::todos::{Todo, Todos}; use crate::todos::{Todo, Todos};
/// How often the open todos are read. /// How often the open todos are read.
@ -85,7 +85,7 @@ async fn publish(client: &async_nats::Client, subject: &str, list: &WireTodos) {
return; return;
} }
}; };
match put_retrying(client, subject, Put::Set(&payload), "the agent's todos").await { match put_retrying(client, subject, &payload, "the agent's todos").await {
Ok(()) => { Ok(()) => {
tracing::debug!( tracing::debug!(
subject, subject,

View file

@ -20,12 +20,6 @@ const FIRST_RETRY: Duration = Duration::from_secs(5);
/// Longest wait between retries. /// Longest wait between retries.
const MAX_RETRY: Duration = Duration::from_mins(5); const MAX_RETRY: Duration = Duration::from_mins(5);
/// What to write under a key.
pub enum Put<'a> {
/// These bytes as the key's value.
Set(&'a [u8]),
}
/// Why a write did not land. /// Why a write did not land.
pub enum Failure { pub enum Failure {
/// The payload can never fit; retrying cannot help. /// The payload can never fit; retrying cannot help.
@ -34,19 +28,18 @@ pub enum Failure {
Retry(String), Retry(String),
} }
/// One acked write of `put` to `subject`. /// One acked write of `bytes` to `subject`.
async fn put_once( async fn put_once(
client: &async_nats::Client, client: &async_nats::Client,
js: &async_nats::jetstream::Context, js: &async_nats::jetstream::Context,
subject: &str, subject: &str,
put: &Put<'_>, bytes: &[u8],
) -> Result<(), Failure> { ) -> Result<(), Failure> {
// An unconnected client does not fail a publish, it buffers it, and the // An unconnected client does not fail a publish, it buffers it, and the
// payload limit it checks against is the library default until the // payload limit it checks against is the library default until the
// server has announced its own. // server has announced its own.
swarm_queue_client::ensure_connected(client) swarm_queue_client::ensure_connected(client)
.map_err(|e| Failure::Retry(swarm_queue_client::chain(&e)))?; .map_err(|e| Failure::Retry(swarm_queue_client::chain(&e)))?;
let Put::Set(bytes) = put;
let sent = js.publish(subject.to_owned(), bytes.to_vec().into()).await; let sent = js.publish(subject.to_owned(), bytes.to_vec().into()).await;
let ack = sent.map_err(|e| match e.kind() { let ack = sent.map_err(|e| match e.kind() {
PublishErrorKind::MaxPayloadExceeded => Failure::TooLarge(e), PublishErrorKind::MaxPayloadExceeded => Failure::TooLarge(e),
@ -62,11 +55,11 @@ async fn put_once(
pub async fn put_retrying( pub async fn put_retrying(
client: &async_nats::Client, client: &async_nats::Client,
subject: &str, subject: &str,
put: Put<'_>, bytes: &[u8],
what: &str, what: &str,
) -> Result<(), Failure> { ) -> Result<(), Failure> {
let js = async_nats::jetstream::new(client.clone()); let js = async_nats::jetstream::new(client.clone());
(|| put_once(client, &js, subject, &put)) (|| put_once(client, &js, subject, bytes))
.retry( .retry(
ExponentialBuilder::default() ExponentialBuilder::default()
.with_min_delay(FIRST_RETRY) .with_min_delay(FIRST_RETRY)