Skip to content

Commit

Permalink
core: disable trace logging of handled mercury responses
Browse files Browse the repository at this point in the history
  • Loading branch information
photovoltex committed Nov 24, 2024
1 parent 209146a commit b5530bb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
12 changes: 12 additions & 0 deletions core/src/dealer/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ impl DealerManager {
})
}

pub fn handles(&self, uri: &str) -> bool {
self.lock(|inner| {
if let Some(dealer) = inner.dealer.get() {
dealer.handles(uri)
} else if let Some(builder) = inner.builder.get() {
builder.handles(uri)
} else {
false
}
})
}

pub async fn start(&self) -> Result<(), Error> {
debug!("Launching dealer");

Expand Down
23 changes: 21 additions & 2 deletions core/src/dealer/maps.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::collections::HashMap;

use thiserror::Error;

use crate::Error;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum HandlerMapError {
Expand All @@ -28,6 +27,10 @@ impl<T> Default for HandlerMap<T> {
}

impl<T> HandlerMap<T> {
pub fn contains(&self, path: &str) -> bool {
matches!(self, HandlerMap::Branch(map) if map.contains_key(path))
}

pub fn insert<'a>(
&mut self,
mut path: impl Iterator<Item = &'a str>,
Expand Down Expand Up @@ -107,6 +110,22 @@ impl<T> SubscriberMap<T> {
}
}

pub fn contains<'a>(&self, mut path: impl Iterator<Item = &'a str>) -> bool {
if !self.subscribed.is_empty() {
return true;
}

if let Some(next) = path.next() {
if let Some(next_map) = self.children.get(next) {
return next_map.contains(path);
}
} else {
return !self.is_empty();
}

false
}

pub fn is_empty(&self) -> bool {
self.children.is_empty() && self.subscribed.is_empty()
}
Expand Down
27 changes: 27 additions & 0 deletions core/src/dealer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ fn subscribe(
Ok(Subscription(rx))
}

fn handles(
req_map: &HandlerMap<Box<dyn RequestHandler>>,
msg_map: &SubscriberMap<MessageHandler>,
uri: &str,
) -> bool {
if req_map.contains(uri) {
return true;
}

match split_uri(uri) {
None => false,
Some(mut split) => msg_map.contains(&mut split),
}
}

#[derive(Default)]
struct Builder {
message_handlers: SubscriberMap<MessageHandler>,
Expand Down Expand Up @@ -277,6 +292,10 @@ impl Builder {
subscribe(&mut self.message_handlers, uris)
}

pub fn handles(&self, uri: &str) -> bool {
handles(&self.request_handlers, &self.message_handlers, uri)
}

pub fn launch_in_background<Fut, F>(self, get_url: F, proxy: Option<Url>) -> Dealer
where
Fut: Future<Output = GetUrlResult> + Send + 'static,
Expand Down Expand Up @@ -416,6 +435,14 @@ impl Dealer {
subscribe(&mut self.shared.message_handlers.lock(), uris)
}

pub fn handles(&self, uri: &str) -> bool {
handles(
&self.shared.request_handlers.lock(),
&self.shared.message_handlers.lock(),
uri,
)
}

pub async fn close(mut self) {
debug!("closing dealer");

Expand Down
9 changes: 6 additions & 3 deletions core/src/mercury/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,12 +276,15 @@ impl MercuryManager {
});
});

if !found {
if found {
Ok(())
} else if self.session().dealer().handles(&response.uri) {
trace!("mercury response <{}> is handled by dealer", response.uri);
Ok(())
} else {
debug!("unknown subscription uri={}", &response.uri);
trace!("response pushed over Mercury: {:?}", response);
Err(MercuryError::Response(response).into())
} else {
Ok(())
}
} else if let Some(cb) = pending.callback {
cb.send(Ok(response)).map_err(|_| MercuryError::Channel)?;
Expand Down

0 comments on commit b5530bb

Please sign in to comment.