Skip to content

Commit 70ac658

Browse files
committed
TRY SPAWN
1 parent a8ba4ed commit 70ac658

1 file changed

Lines changed: 41 additions & 4 deletions

File tree

src/io/vss_store.rs

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ type CustomRetryPolicy = FilteredRetryPolicy<
4444
>;
4545

4646
const INTERNAL_RUNTIME_WORKERS: usize = 2;
47+
const VSS_IO_TIMEOUT: Duration = Duration::from_secs(5);
4748

4849
/// A [`KVStoreSync`] implementation that writes to and reads from a [VSS](https://github.com/lightningdevkit/vss-server/blob/main/README.md) backend.
4950
pub struct VssStore {
@@ -120,7 +121,16 @@ impl KVStoreSync for VssStore {
120121
let inner = Arc::clone(&self.inner);
121122
let fut =
122123
async move { inner.read_internal(primary_namespace, secondary_namespace, key).await };
123-
tokio::task::block_in_place(move || internal_runtime.block_on(fut))
124+
// TODO: We could drop the timeout here once we ensured vss-client's Retry logic always
125+
// times out.
126+
let spawned_fut = internal_runtime.spawn(async move {
127+
tokio::time::timeout(VSS_IO_TIMEOUT, fut).await.map_err(|_| {
128+
let msg = "VssStore::read timed out";
129+
Error::new(ErrorKind::Other, msg)
130+
})
131+
});
132+
tokio::task::block_in_place(move || internal_runtime.block_on(spawned_fut))
133+
.expect("We should always finish")?
124134
}
125135

126136
fn write(
@@ -150,7 +160,16 @@ impl KVStoreSync for VssStore {
150160
)
151161
.await
152162
};
153-
tokio::task::block_in_place(move || internal_runtime.block_on(fut))
163+
// TODO: We could drop the timeout here once we ensured vss-client's Retry logic always
164+
// times out.
165+
let spawned_fut = internal_runtime.spawn(async move {
166+
tokio::time::timeout(VSS_IO_TIMEOUT, fut).await.map_err(|_| {
167+
let msg = "VssStore::write timed out";
168+
Error::new(ErrorKind::Other, msg)
169+
})
170+
});
171+
tokio::task::block_in_place(move || internal_runtime.block_on(spawned_fut))
172+
.expect("We should always finish")?
154173
}
155174

156175
fn remove(
@@ -179,7 +198,16 @@ impl KVStoreSync for VssStore {
179198
)
180199
.await
181200
};
182-
tokio::task::block_in_place(move || internal_runtime.block_on(fut))
201+
// TODO: We could drop the timeout here once we ensured vss-client's Retry logic always
202+
// times out.
203+
let spawned_fut = internal_runtime.spawn(async move {
204+
tokio::time::timeout(VSS_IO_TIMEOUT, fut).await.map_err(|_| {
205+
let msg = "VssStore::remove timed out";
206+
Error::new(ErrorKind::Other, msg)
207+
})
208+
});
209+
tokio::task::block_in_place(move || internal_runtime.block_on(spawned_fut))
210+
.expect("We should always finish")?
183211
}
184212

185213
fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result<Vec<String>> {
@@ -192,7 +220,16 @@ impl KVStoreSync for VssStore {
192220
let secondary_namespace = secondary_namespace.to_string();
193221
let inner = Arc::clone(&self.inner);
194222
let fut = async move { inner.list_internal(primary_namespace, secondary_namespace).await };
195-
tokio::task::block_in_place(move || internal_runtime.block_on(fut))
223+
// TODO: We could drop the timeout here once we ensured vss-client's Retry logic always
224+
// times out.
225+
let spawned_fut = internal_runtime.spawn(async move {
226+
tokio::time::timeout(VSS_IO_TIMEOUT, fut).await.map_err(|_| {
227+
let msg = "VssStore::list timed out";
228+
Error::new(ErrorKind::Other, msg)
229+
})
230+
});
231+
tokio::task::block_in_place(move || internal_runtime.block_on(spawned_fut))
232+
.expect("We should always finish")?
196233
}
197234
}
198235

0 commit comments

Comments
 (0)