mirror of
https://github.com/Dasemu/Synapse.git
synced 2026-11-26 15:26:17 +00:00
fix(sync): robust blob handling, op validation and quarantine
- upload_blob resumes until the server confirms the blob (202/409 are not success) - missing local blob skips its PutAsset instead of blocking all later ops - failed blob downloads are counted and retried, not fatal to the cycle - server validates pushed ops (hash, hlc, clock skew) and returns per-op rejections - pulled ops that can never apply are quarantined; storage errors stay fatal - SYNAPSE_MAX_CLOCK_SKEW_MINUTES; document Arc<Mutex<Graph>> handle for the FFI Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3266ddda3a
commit
9ce35f90ff
11 files changed
+433
-52
No files matched your search
@@ -82,25 +82,43 @@ pub async fn push_ops(
|
||||
));
|
||||
}
|
||||
let h = graph_for(&s, &who, &id, false).await?;
|
||||
let (applied, last_seq) = h
|
||||
let max_future_ms = s.max_clock_skew.as_millis() as i64;
|
||||
let (applied, last_seq, rejected) = h
|
||||
.run(move |g| {
|
||||
let mut applied = 0u32;
|
||||
for w in &req.ops {
|
||||
if hlc_actor(&w.hlc).is_none() || synapse_core::hlc::decode(&w.hlc).is_none() {
|
||||
return Err(synapse_core::Error::Invalid(format!("bad hlc {:?}", w.hlc)));
|
||||
let (mut applied, mut rejected) = (0u32, Vec::new());
|
||||
let now = g.now_ms();
|
||||
for (i, w) in req.ops.iter().enumerate() {
|
||||
let reject = |reason: String| Rejection {
|
||||
index: i as u32,
|
||||
reason,
|
||||
};
|
||||
if let Err(reason) = validate_op(&w.hlc, &w.op, now, max_future_ms) {
|
||||
rejected.push(reject(reason));
|
||||
continue;
|
||||
}
|
||||
if g.apply_remote(&w.hlc, &w.op)? {
|
||||
applied += 1;
|
||||
match g.apply_remote(&w.hlc, &w.op) {
|
||||
Ok(true) => applied += 1,
|
||||
Ok(false) => {}
|
||||
// Unappliable op: refuse it, keep the rest of the batch.
|
||||
Err(e) if is_poison(&e) => rejected.push(reject(e.to_string())),
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
}
|
||||
Ok((applied, g.last_seq()?))
|
||||
Ok((applied, g.last_seq()?, rejected))
|
||||
})
|
||||
.await?;
|
||||
if applied > 0 {
|
||||
h.publish_seq(last_seq);
|
||||
}
|
||||
if !rejected.is_empty() {
|
||||
tracing::warn!(graph = %id, principal = %who.name, rejected = rejected.len(), first = %rejected[0].reason, "push: ops rejected");
|
||||
}
|
||||
tracing::info!(graph = %id, principal = %who.name, applied, "push");
|
||||
Ok(Json(PushResponse { applied, last_seq }))
|
||||
Ok(Json(PushResponse {
|
||||
applied,
|
||||
last_seq,
|
||||
rejected,
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use std::net::SocketAddr;
|
||||
use std::path::PathBuf;
|
||||
use std::time::Duration;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Config {
|
||||
@@ -10,6 +11,8 @@ pub struct Config {
|
||||
/// Token for `/admin/update`; the endpoint is disabled when unset.
|
||||
pub update_token: Option<String>,
|
||||
pub update_script: PathBuf,
|
||||
/// Ops whose HLC is further ahead of the server clock than this are rejected.
|
||||
pub max_clock_skew: Duration,
|
||||
}
|
||||
|
||||
pub const MIN_TOKEN_LEN: usize = 16;
|
||||
@@ -36,6 +39,13 @@ impl Config {
|
||||
update_script: var("SYNAPSE_UPDATE_SCRIPT")
|
||||
.unwrap_or_else(|| "/opt/synapse/scripts/update.sh".into())
|
||||
.into(),
|
||||
max_clock_skew: Duration::from_secs(
|
||||
60 * var("SYNAPSE_MAX_CLOCK_SKEW_MINUTES")
|
||||
.map(|v| v.parse::<u64>())
|
||||
.transpose()
|
||||
.map_err(|e| format!("SYNAPSE_MAX_CLOCK_SKEW_MINUTES: {e}"))?
|
||||
.unwrap_or(10),
|
||||
),
|
||||
};
|
||||
cfg.validate()?;
|
||||
Ok(cfg)
|
||||
|
||||
@@ -28,6 +28,7 @@ pub struct AppState {
|
||||
pub(crate) data_dir: Arc<std::path::PathBuf>,
|
||||
pub(crate) updater: Arc<admin::Updater>,
|
||||
pub(crate) shutdown: Arc<Notify>,
|
||||
pub(crate) max_clock_skew: std::time::Duration,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -44,6 +45,7 @@ impl AppState {
|
||||
data_dir: Arc::new(cfg.data_dir.clone()),
|
||||
updater: Arc::new(admin::Updater::default()),
|
||||
shutdown: Arc::new(Notify::new()),
|
||||
max_clock_skew: cfg.max_clock_skew,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user