Skip to content

Commit

Permalink
feat: fix max buffer to sender channel
Browse files Browse the repository at this point in the history
  • Loading branch information
S0c5 committed Jul 30, 2024
1 parent 25e643e commit 701d958
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 11 deletions.
5 changes: 4 additions & 1 deletion sube/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,10 @@ pub enum Error {
AccountNotFound,
ConstantNotFound(String),
Platform(String),
CantInitBackend
CantInitBackend,
CantDecodeReponseForMeta,
CantDecodeRawQueryResponse,
CantFindMethodInPallet,
}

impl fmt::Display for Error {
Expand Down
2 changes: 1 addition & 1 deletion sube/src/meta_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl StorageKey {
.storage
.as_ref()
.and_then(|s| s.entries.iter().find(|e| e.name == item))
.ok_or(crate::Error::StorageKeyNotFound)?;
.ok_or(crate::Error::CantFindMethodInPallet)?;
log::trace!("map_keys={}", map_keys.iter().map(|x| x.as_ref()).collect::<Vec<&str>>().join(", "));
entry.ty.key(registry, &meta.name, &entry.name, map_keys)
}
Expand Down
10 changes: 5 additions & 5 deletions sube/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<R: Rpc> Backend for RpcClient<R> {
)
.await
.map_err(|err| {
log::info!("error {:?}", err);
log::error!("error state_queryStorageAt {:?}", err);
crate::Error::StorageKeyNotFound
})
}
Expand All @@ -75,7 +75,7 @@ impl<R: Rpc> Backend for RpcClient<R> {
)
.await
.map_err(|err| {
log::info!("error {:?}", err);
log::error!("error paged {:?}", err);
crate::Error::StorageKeyNotFound
})?;
log::info!("rpc call {:?}", r);
Expand All @@ -91,12 +91,12 @@ impl<R: Rpc> Backend for RpcClient<R> {
.rpc("state_getStorage", &[&format!("\"{}\"", &key)])
.await
.map_err(|e| {
log::debug!("RPC failure: {}", e);
log::error!("RPC failure: {}", e);
// NOTE it could fail for more reasons
crate::Error::StorageKeyNotFound
})?;

let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::StorageKeyNotFound)?;
let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::CantDecodeRawQueryResponse)?;

Ok(response)
}
Expand All @@ -119,7 +119,7 @@ impl<R: Rpc> Backend for RpcClient<R> {
.rpc("state_getMetadata", &[])
.await
.map_err(|e| crate::Error::Node(e.to_string()))?;
let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::StorageKeyNotFound)?;
let response = hex::decode(&res[2..]).map_err(|_err| crate::Error::CantDecodeReponseForMeta)?;
let meta = from_bytes(&mut response.as_slice()).map_err(|_| crate::Error::BadMetadata)?;
log::trace!("Metadata {:#?}", meta);
Ok(meta)
Expand Down
16 changes: 12 additions & 4 deletions sube/src/ws.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use crate::{
Error,
};

const MAX_BUFFER: usize = usize::MAX >> 3;

type Id = u32;

pub struct Backend {
Expand Down Expand Up @@ -60,13 +62,19 @@ impl Rpc for Backend {
.lock()
.await
.try_send(Message::Text(msg))
.map_err(|_| standard_error(StandardError::InternalError, None))?;
.map_err(|err| {
log::error!("Error tx lock message: {:?}", err);
standard_error(StandardError::InternalError, None)
})?;

log::info!("sent CMD");
// wait for the matching response to arrive
let res = recv
.await
.map_err(|_| standard_error(StandardError::InternalError, None))?
.map_err(|err| {
log::error!("Error receiving message: {:?}", err);
standard_error(StandardError::InternalError, None)
})?
.result()?;

Ok(res)
Expand All @@ -84,8 +92,8 @@ impl Backend {

let (tx, rx) =
ewebsock::connect(url, ewebsock::Options::default()).map_err(Error::Platform)?;

let (sender, recv) = mpsc::channel::<Message>(0);
let (sender, recv) = mpsc::channel::<Message>(MAX_BUFFER);

let backend = Backend {
tx: Mutex::new(sender),
Expand Down

0 comments on commit 701d958

Please sign in to comment.