1use 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
18pub const MAX_WORKERS: usize = 8;
20
21pub 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
98pub 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}