Skip to content

Commit

Permalink
feat: add no_std async mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
S0c5 committed Jul 29, 2024
1 parent 3f72d23 commit 25e643e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
5 changes: 3 additions & 2 deletions sube/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ futures-channel = { version = "0.3.21", default-features = false, features = [
futures-util = { version = "0.3.21", default-features = false, features = [
"sink",
], optional = true }
async-mutex = { version = "1.4.0", optional = true }

async-tls = { version = "0.11.0", default-features = false, optional = true }

# bin target
Expand All @@ -57,6 +57,7 @@ anyhow = { version = "1.0.40", optional = true }
rand_core = { version = "0.6.3", optional = true }
ewebsock = { git = "https://github.com/S0c5/ewebsock.git", optional = true, branch = "enhacement/aviod-blocking-operations-with-mpsc-futures" }
env_logger = "0.11.3"
no-std-async = "1.1.2"


[dev-dependencies]
Expand All @@ -81,9 +82,9 @@ json = ["scales/json"]
std = []
no_std = []


v14 = ["dep:scale-info", "frame-metadata/current"]
ws = [
"dep:async-mutex",
"dep:async-std",
"dep:ewebsock",
"dep:futures-channel",
Expand Down
28 changes: 18 additions & 10 deletions sube/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use crate::{prelude::*, Offline, StorageChangeSet};

use core::future::{Future, IntoFuture};
use url::Url;
use async_mutex::Mutex;

pub struct SubeBuilder<'a, Body, Signer> {
url: Option<&'a str>,
Expand Down Expand Up @@ -115,7 +114,6 @@ where
let path = url.path();
let body = body.ok_or(Error::BadInput)?;


let (backend, meta) = get_multi_backend_by_url(url.clone(), metadata).await?;

Ok(match path {
Expand All @@ -131,13 +129,15 @@ where
}

use heapless::FnvIndexMap as Map;
use no_std_async::Mutex;

static INSTANCE_BACKEND: async_once_cell::OnceCell<async_mutex::Mutex<Map<String, &'static AnyBackend, 16>>> =
async_once_cell::OnceCell::new();

static INSTANCE_METADATA: async_once_cell::OnceCell<async_mutex::Mutex<Map<String, &'static Metadata, 16>>> =
async_once_cell::OnceCell::new();
static INSTANCE_BACKEND: async_once_cell::OnceCell<
Mutex<Map<String, Mutex<&'static AnyBackend>, 16>>,
> = async_once_cell::OnceCell::new();

static INSTANCE_METADATA: async_once_cell::OnceCell<
Mutex<Map<String, Mutex<&'static Metadata>, 16>>,
> = async_once_cell::OnceCell::new();

async fn get_metadata(backend: &AnyBackend, metadata: Option<Metadata>) -> SubeResult<Metadata> {
match metadata {
Expand Down Expand Up @@ -173,17 +173,25 @@ async fn get_multi_backend_by_url<'a>(
let cached_m = instance_metadata.get(&base_path);

match (cached_b, cached_m) {
(Some(b), Some(m)) => Ok((b, m)),
(Some(b), Some(m)) => {
let b = *b.lock().await;
let m = *m.lock().await;
Ok((b, m))
}
_ => {
let backend = Box::new(get_backend_by_url(url.clone()).await?);
let backend = Box::leak::<'static>(backend);

instance_backend.insert(base_path.clone(), backend).map_err(|_| Error::CantInitBackend)?;
instance_backend
.insert(base_path.clone(), Mutex::new(backend))
.map_err(|_| Error::CantInitBackend)?;

let metadata = Box::new(get_metadata(backend, metadata).await?);
let metadata = Box::leak::<'static>(metadata);

instance_metadata.insert(base_path.clone(), metadata).map_err(|_| Error::BadMetadata)?;
instance_metadata
.insert(base_path.clone(), Mutex::new(metadata))
.map_err(|_| Error::BadMetadata)?;

Ok((backend, metadata))
}
Expand Down
2 changes: 1 addition & 1 deletion sube/src/ws.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::{collections::BTreeMap, sync::Arc};

use async_mutex::Mutex;
use no_std_async::Mutex;
use ewebsock::{WsEvent, WsMessage as Message, WsReceiver as Rx, WsSender as Tx};
use futures_channel::{mpsc, oneshot};
use futures_util::StreamExt as _;
Expand Down

0 comments on commit 25e643e

Please sign in to comment.