Skip to content

Commit

Permalink
Adding the ability to return an error from the interceptors factory (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sashacmc authored Feb 14, 2024
1 parent dfe3d4a commit 40efd99
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
14 changes: 10 additions & 4 deletions zenoh/src/net/routing/dispatcher/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use zenoh_config::unwrap_or_default;
use zenoh_config::Config;
use zenoh_protocol::core::{ExprId, WhatAmI, ZenohId};
use zenoh_protocol::network::Mapping;
use zenoh_result::ZResult;
// use zenoh_collections::Timer;
use zenoh_sync::get_mut_unchecked;

Expand Down Expand Up @@ -76,15 +77,20 @@ pub struct Tables {
}

impl Tables {
pub fn new(zid: ZenohId, whatami: WhatAmI, hlc: Option<Arc<HLC>>, config: &Config) -> Self {
pub fn new(
zid: ZenohId,
whatami: WhatAmI,
hlc: Option<Arc<HLC>>,
config: &Config,
) -> ZResult<Self> {
let drop_future_timestamp =
unwrap_or_default!(config.timestamping().drop_future_timestamp());
let router_peers_failover_brokering =
unwrap_or_default!(config.routing().router().peers_failover_brokering());
// let queries_default_timeout =
// Duration::from_millis(unwrap_or_default!(config.queries_default_timeout()));
let hat_code = hat::new_hat(whatami, config);
Tables {
Ok(Tables {
zid,
whatami,
face_counter: 0,
Expand All @@ -96,11 +102,11 @@ impl Tables {
faces: HashMap::new(),
mcast_groups: vec![],
mcast_faces: vec![],
interceptors: interceptor_factories(config),
interceptors: interceptor_factories(config)?,
pull_caches_lock: Mutex::new(()),
hat: hat_code.new_tables(router_peers_failover_brokering),
hat_code: hat_code.into(),
}
})
}

#[doc(hidden)]
Expand Down
5 changes: 3 additions & 2 deletions zenoh/src/net/routing/interceptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use super::RoutingContext;
use zenoh_config::Config;
use zenoh_protocol::network::NetworkMessage;
use zenoh_result::ZResult;
use zenoh_transport::{multicast::TransportMulticast, unicast::TransportUnicast};

pub(crate) trait InterceptorTrait {
Expand All @@ -44,11 +45,11 @@ pub(crate) trait InterceptorFactoryTrait {

pub(crate) type InterceptorFactory = Box<dyn InterceptorFactoryTrait + Send + Sync>;

pub(crate) fn interceptor_factories(_config: &Config) -> Vec<InterceptorFactory> {
pub(crate) fn interceptor_factories(_config: &Config) -> ZResult<Vec<InterceptorFactory>> {
// Add interceptors here
// @TODO build the list of intercetors with the correct order from the config
// vec![Box::new(LoggerInterceptor {})]
vec![]
Ok(vec![])
}

pub(crate) struct InterceptorsChain {
Expand Down
13 changes: 9 additions & 4 deletions zenoh/src/net/routing/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,20 @@ pub struct Router {
}

impl Router {
pub fn new(zid: ZenohId, whatami: WhatAmI, hlc: Option<Arc<HLC>>, config: &Config) -> Self {
Router {
pub fn new(
zid: ZenohId,
whatami: WhatAmI,
hlc: Option<Arc<HLC>>,
config: &Config,
) -> ZResult<Self> {
Ok(Router {
// whatami,
tables: Arc::new(TablesLock {
tables: RwLock::new(Tables::new(zid, whatami, hlc, config)),
tables: RwLock::new(Tables::new(zid, whatami, hlc, config)?),
ctrl_lock: Mutex::new(hat::new_hat(whatami, config)),
queries_lock: RwLock::new(()),
}),
}
})
}

#[allow(clippy::too_many_arguments)]
Expand Down
2 changes: 1 addition & 1 deletion zenoh/src/net/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl Runtime {
let hlc = (*unwrap_or_default!(config.timestamping().enabled().get(whatami)))
.then(|| Arc::new(HLCBuilder::new().with_id(uhlc::ID::from(&zid)).build()));

let router = Arc::new(Router::new(zid, whatami, hlc.clone(), &config));
let router = Arc::new(Router::new(zid, whatami, hlc.clone(), &config)?);

let handler = Arc::new(RuntimeTransportEventHandler {
runtime: std::sync::RwLock::new(None),
Expand Down
12 changes: 8 additions & 4 deletions zenoh/src/net/tests/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ fn base_test() {
WhatAmI::Client,
Some(Arc::new(HLC::default())),
&config,
);
)
.unwrap();
let tables = router.tables.clone();

let primitives = Arc::new(DummyPrimitives {});
Expand Down Expand Up @@ -133,7 +134,8 @@ fn match_test() {
WhatAmI::Client,
Some(Arc::new(HLC::default())),
&config,
);
)
.unwrap();
let tables = router.tables.clone();

let primitives = Arc::new(DummyPrimitives {});
Expand Down Expand Up @@ -172,7 +174,8 @@ fn clean_test() {
WhatAmI::Client,
Some(Arc::new(HLC::default())),
&config,
);
)
.unwrap();
let tables = router.tables.clone();

let primitives = Arc::new(DummyPrimitives {});
Expand Down Expand Up @@ -478,7 +481,8 @@ fn client_test() {
WhatAmI::Client,
Some(Arc::new(HLC::default())),
&config,
);
)
.unwrap();
let tables = router.tables.clone();

let sub_info = SubscriberInfo {
Expand Down

0 comments on commit 40efd99

Please sign in to comment.