maplibre/platform/noweb/
mod.rs

1//! Module which is used target platform is not web related.
2
3use std::{
4    future::Future,
5    sync::atomic::{AtomicUsize, Ordering},
6};
7
8use crate::{
9    environment::{OffscreenKernel, OffscreenKernelConfig},
10    io::source_client::{HttpSourceClient, SourceClient},
11    platform::http_client::ReqwestHttpClient,
12};
13
14pub mod http_client;
15pub mod scheduler;
16pub mod trace;
17
18pub fn run_multithreaded<F: Future>(future: F) -> F::Output {
19    tokio::runtime::Builder::new_multi_thread()
20        .enable_io()
21        .enable_time()
22        .thread_name_fn(|| {
23            static ATOMIC_ID: AtomicUsize = AtomicUsize::new(0);
24            let id = ATOMIC_ID.fetch_add(1, Ordering::SeqCst);
25            format!("maplibre-rs-pool-{id}")
26        })
27        .on_thread_start(|| {
28            #[cfg(feature = "trace")]
29            tracing_tracy::client::set_thread_name!("tokio-runtime-worker");
30
31            log::info!("Worker thread started")
32        })
33        .build()
34        .unwrap()
35        .block_on(future)
36}
37
38pub struct ReqwestOffscreenKernelEnvironment(OffscreenKernelConfig);
39
40impl OffscreenKernel for ReqwestOffscreenKernelEnvironment {
41    type HttpClient = ReqwestHttpClient;
42
43    fn create(config: OffscreenKernelConfig) -> Self {
44        ReqwestOffscreenKernelEnvironment(config)
45    }
46
47    fn source_client(&self) -> SourceClient<Self::HttpClient> {
48        SourceClient::new(HttpSourceClient::new(ReqwestHttpClient::new::<String>(
49            self.0.cache_directory.clone(),
50        )))
51    }
52}