Skip to main content

tessera/
resolver.rs

1//! Concurrent presigned-URL resolution.
2//!
3//! Each `(asset, coin, month)` partition needs its own short-lived presigned
4//! URL(s), minted via the download endpoint — one for a finalised month, one
5//! per day for the day-backed open month. For multi-partition reads we resolve
6//! them concurrently — a scoped thread pool for the sync client,
7//! `futures::try_join_all` for async.
8
9use std::sync::atomic::{AtomicUsize, Ordering};
10use std::sync::{Mutex, PoisonError};
11
12use futures::future::try_join_all;
13
14use crate::error::TesseraError;
15use crate::models::PartitionRef;
16use crate::readers::ResolvedPartition;
17
18/// Cap on worker threads / concurrent URL fetches.
19pub const MAX_WORKERS: usize = 8;
20
21/// Resolve presigned URLs for `refs` concurrently, preserving order.
22///
23/// A single ref is fetched inline; multiple refs are fanned out over at most
24/// [`MAX_WORKERS`] worker threads with short-circuit on the first error.
25///
26/// # Errors
27///
28/// Returns the first fetch error, if any.
29/// # Panics
30///
31/// Panics if internal bookkeeping is inconsistent (a slot is unresolved
32/// although no error was recorded) — unreachable by construction.
33pub fn resolve_sync(
34    fetch_urls: impl Fn(&PartitionRef) -> Result<Vec<String>, TesseraError> + Sync,
35    refs: &[PartitionRef],
36) -> Result<Vec<ResolvedPartition>, TesseraError> {
37    if let [partition] = refs {
38        return Ok(vec![(partition.clone(), fetch_urls(partition)?)]);
39    }
40
41    let workers = MAX_WORKERS.min(refs.len());
42    let next_index = AtomicUsize::new(0);
43    let urls: Mutex<Vec<Option<Vec<String>>>> = Mutex::new(vec![None; refs.len()]);
44    let first_error: Mutex<Option<TesseraError>> = Mutex::new(None);
45
46    std::thread::scope(|scope| {
47        for _ in 0..workers {
48            scope.spawn(|| {
49                loop {
50                    if first_error
51                        .lock()
52                        .unwrap_or_else(PoisonError::into_inner)
53                        .is_some()
54                    {
55                        break;
56                    }
57                    let index = next_index.fetch_add(1, Ordering::Relaxed);
58                    if index >= refs.len() {
59                        break;
60                    }
61                    match fetch_urls(&refs[index]) {
62                        Ok(url) => {
63                            urls.lock().unwrap_or_else(PoisonError::into_inner)[index] = Some(url);
64                        }
65                        Err(err) => {
66                            let mut guard =
67                                first_error.lock().unwrap_or_else(PoisonError::into_inner);
68                            if guard.is_none() {
69                                *guard = Some(err);
70                            }
71                            break;
72                        }
73                    }
74                }
75            });
76        }
77    });
78
79    if let Some(err) = first_error
80        .into_inner()
81        .unwrap_or_else(PoisonError::into_inner)
82    {
83        return Err(err);
84    }
85    let resolved = urls.into_inner().unwrap_or_else(PoisonError::into_inner);
86    Ok(refs
87        .iter()
88        .zip(resolved)
89        .map(|(partition, url)| {
90            (
91                partition.clone(),
92                url.expect("resolved when no error recorded"),
93            )
94        })
95        .collect())
96}
97
98/// Resolve presigned URLs for `refs` concurrently, preserving order.
99///
100/// # Errors
101///
102/// Returns the first fetch error, if any.
103pub async fn resolve_async<F, Fut>(
104    fetch_urls: F,
105    refs: &[PartitionRef],
106) -> Result<Vec<ResolvedPartition>, TesseraError>
107where
108    F: Fn(PartitionRef) -> Fut,
109    Fut: Future<Output = Result<Vec<String>, TesseraError>>,
110{
111    let urls = try_join_all(refs.iter().map(|partition| fetch_urls(partition.clone()))).await?;
112    Ok(refs.iter().cloned().zip(urls).collect())
113}