fp_runtime/asupersync/
transport.rs1use std::{
2 collections::BTreeMap,
3 sync::{Arc, Mutex},
4};
5
6use serde::{Deserialize, Serialize};
7
8use crate::asupersync::{
9 codec::EncodedArtifact,
10 config::{AsupersyncConfig, CapabilitySet, CxCapability},
11 error::AsupersyncError,
12 validate_capability_gate,
13};
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16#[serde(rename_all = "snake_case")]
17pub enum TransferStatus {
18 Completed,
19 RetryableFailure,
20 PermanentFailure,
21}
22
23#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
24pub struct TransferReport {
25 pub artifact_id: String,
26 pub bytes_transferred: usize,
27 pub status: TransferStatus,
28 pub detail: String,
29}
30
31pub trait TransportLayer {
32 fn send(
33 &self,
34 artifact: EncodedArtifact,
35 config: &AsupersyncConfig,
36 ) -> Result<TransferReport, AsupersyncError>;
37
38 fn receive(
39 &self,
40 artifact_id: &str,
41 config: &AsupersyncConfig,
42 ) -> Result<EncodedArtifact, AsupersyncError>;
43
44 fn required_capabilities(&self) -> CapabilitySet {
45 CapabilitySet::for_capability(CxCapability::Io)
46 .union(CapabilitySet::for_capability(CxCapability::Remote))
47 }
48}
49
50#[derive(Debug, Clone, Default)]
51pub struct InMemoryTransport {
52 storage: Arc<Mutex<BTreeMap<String, Arc<EncodedArtifact>>>>,
53}
54
55impl InMemoryTransport {
56 #[must_use]
57 pub fn new() -> Self {
58 Self::default()
59 }
60}
61
62impl TransportLayer for InMemoryTransport {
63 fn send(
64 &self,
65 artifact: EncodedArtifact,
66 config: &AsupersyncConfig,
67 ) -> Result<TransferReport, AsupersyncError> {
68 validate_capability_gate(config, self.required_capabilities())?;
69
70 let mut guard = self.storage.lock().map_err(|_| {
71 AsupersyncError::Transport("in-memory transport lock poisoned".to_string())
72 })?;
73 let bytes_transferred = artifact.encoded_bytes.len();
74 let artifact_id = artifact.artifact_id.clone();
75 guard.insert(artifact_id.clone(), Arc::new(artifact));
76
77 Ok(TransferReport {
78 artifact_id,
79 bytes_transferred,
80 status: TransferStatus::Completed,
81 detail: "stored in in-memory transport".to_string(),
82 })
83 }
84
85 fn receive(
86 &self,
87 artifact_id: &str,
88 config: &AsupersyncConfig,
89 ) -> Result<EncodedArtifact, AsupersyncError> {
90 validate_capability_gate(config, self.required_capabilities())?;
91
92 let artifact = {
93 let guard = self.storage.lock().map_err(|_| {
94 AsupersyncError::Transport("in-memory transport lock poisoned".to_string())
95 })?;
96 guard
97 .get(artifact_id)
98 .cloned()
99 .ok_or_else(|| AsupersyncError::ArtifactNotFound(artifact_id.to_string()))?
100 };
101
102 match std::panic::catch_unwind(|| (*artifact).clone()) {
103 Ok(artifact) => Ok(artifact),
104 Err(payload) => {
105 let _guard = self.storage.lock().map_err(|_| {
106 AsupersyncError::Transport("in-memory transport lock poisoned".to_string())
107 })?;
108 std::panic::resume_unwind(payload);
109 }
110 }
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use std::panic::{AssertUnwindSafe, catch_unwind};
117
118 use super::{InMemoryTransport, TransportLayer};
119 use crate::asupersync::{
120 codec::EncodedArtifact, config::AsupersyncConfig, error::AsupersyncError,
121 };
122
123 fn artifact() -> EncodedArtifact {
124 EncodedArtifact {
125 artifact_id: "transport-artifact-3nzz3".to_string(),
126 source_len: 7,
127 encoded_bytes: b"payload".to_vec(),
128 repair_symbols: 1,
129 }
130 }
131
132 #[test]
133 fn receive_returns_a_deep_copy_after_storing_an_arc_3nzz3() -> Result<(), AsupersyncError> {
134 let transport = InMemoryTransport::new();
135 let config = AsupersyncConfig::default();
136 transport.send(artifact(), &config)?;
137
138 let mut received = transport.receive("transport-artifact-3nzz3", &config)?;
139 received.encoded_bytes = b"changed".to_vec();
140
141 assert_eq!(
142 transport
143 .receive("transport-artifact-3nzz3", &config)?
144 .encoded_bytes,
145 b"payload"
146 );
147 Ok(())
148 }
149
150 #[test]
151 fn receive_preserves_lock_poisoning_failure_3nzz3() {
152 let transport = InMemoryTransport::new();
153 let storage = transport.storage.clone();
154 let _ = catch_unwind(AssertUnwindSafe(|| {
155 let Ok(_guard) = storage.lock() else {
156 return;
157 };
158 std::panic::resume_unwind(Box::new("poison in-memory transport lock"));
159 }));
160
161 let error = transport
162 .receive("transport-artifact-3nzz3", &AsupersyncConfig::default())
163 .expect_err("poisoned storage must be rejected");
164 assert!(
165 matches!(error, AsupersyncError::Transport(message) if message.contains("poisoned"))
166 );
167 }
168}