Skip to content

Commit 494144f

Browse files
committed
Refactoring
1 parent ce58dee commit 494144f

File tree

3 files changed

+41
-34
lines changed

3 files changed

+41
-34
lines changed

zenoh/src/net/routing/dispatcher/face.rs

+20
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,26 @@ impl Face {
298298
interest.rejection_token.cancel();
299299
}
300300
}
301+
302+
pub(crate) fn load_egress_interceptors(
303+
&self,
304+
) -> Option<arc_swap::Guard<Arc<InterceptorsChain>>> {
305+
self.state
306+
.eg_interceptors
307+
.as_ref()
308+
.map(|i| i.load())
309+
.and_then(|i| i.is_empty().not().then_some(i))
310+
}
311+
312+
pub(crate) fn load_ingress_interceptors(
313+
&self,
314+
) -> Option<arc_swap::Guard<Arc<InterceptorsChain>>> {
315+
self.state
316+
.in_interceptors
317+
.as_ref()
318+
.map(|i| i.load())
319+
.and_then(|i| i.is_empty().not().then_some(i))
320+
}
301321
}
302322

303323
impl Primitives for Face {

zenoh/src/net/routing/dispatcher/pubsub.rs

+7-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
#[zenoh_macros::unstable]
1616
use std::collections::HashMap;
17-
use std::{ops::Not, sync::Arc};
17+
use std::sync::Arc;
1818

1919
use zenoh_core::zread;
2020
use zenoh_protocol::{
@@ -33,7 +33,6 @@ use super::{
3333
use crate::key_expr::KeyExpr;
3434
use crate::net::routing::{
3535
hat::{HatTrait, SendDeclare},
36-
interceptor::InterceptorTrait,
3736
router::get_or_set_route,
3837
RoutingContext,
3938
};
@@ -300,23 +299,15 @@ pub fn route_data(
300299
msg.wire_expr.suffix.as_ref()
301300
);
302301

303-
if let Some(interceptor) = face
304-
.state
305-
.in_interceptors
306-
.as_ref()
307-
.map(|i| i.load())
308-
.and_then(|i| i.is_empty().not().then_some(i))
309-
{
310-
let cache_guard = prefix.get_ingress_cache(face, &interceptor);
311-
let cache = cache_guard.as_ref().and_then(|c| c.get_ref().as_ref());
302+
if let Some(interceptor) = face.load_ingress_interceptors() {
312303
let ctx = &mut RoutingContext::new(NetworkMessageMut {
313304
body: NetworkBodyMut::Push(msg),
314305
reliability,
315306
#[cfg(feature = "stats")]
316307
size: None,
317308
});
318309

319-
if !interceptor.intercept(ctx, cache) {
310+
if !interceptor.intercept_with_face(ctx, face, &prefix) {
320311
return;
321312
}
322313
};
@@ -365,24 +356,15 @@ pub fn route_data(
365356
msg.wire_expr = key_expr.into();
366357
msg.ext_nodeid = ext::NodeIdType { node_id: *context };
367358

368-
if let Some(interceptor) = face
369-
.state
370-
.eg_interceptors
371-
.as_ref()
372-
.map(|i| i.load())
373-
.and_then(|i| i.is_empty().not().then_some(i))
374-
{
375-
let cache_guard = prefix.get_ingress_cache(face, &interceptor);
376-
let cache = cache_guard.as_ref().and_then(|c| c.get_ref().as_ref());
377-
359+
if let Some(interceptor) = face.load_egress_interceptors() {
378360
let ctx = &mut RoutingContext::new(NetworkMessageMut {
379361
body: NetworkBodyMut::Push(msg),
380362
reliability,
381363
#[cfg(feature = "stats")]
382364
size: None,
383365
});
384366

385-
if !interceptor.intercept(ctx, cache) {
367+
if !interceptor.intercept_with_face(ctx, face, &prefix) {
386368
return;
387369
}
388370
};
@@ -420,24 +402,15 @@ pub fn route_data(
420402
payload: msg.payload.clone(),
421403
};
422404

423-
if let Some(interceptor) = face
424-
.state
425-
.eg_interceptors
426-
.as_ref()
427-
.map(|i| i.load())
428-
.and_then(|i| i.is_empty().not().then_some(i))
429-
{
430-
let cache_guard = prefix.get_ingress_cache(face, &interceptor);
431-
let cache = cache_guard.as_ref().and_then(|c| c.get_ref().as_ref());
432-
405+
if let Some(interceptor) = face.load_egress_interceptors() {
433406
let ctx = &mut RoutingContext::new(NetworkMessageMut {
434407
body: NetworkBodyMut::Push(msg),
435408
reliability,
436409
#[cfg(feature = "stats")]
437410
size: None,
438411
});
439412

440-
if !interceptor.intercept(ctx, cache) {
413+
if !interceptor.intercept_with_face(ctx, face, &prefix) {
441414
continue;
442415
}
443416
};

zenoh/src/net/routing/interceptor/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ use zenoh_protocol::network::NetworkMessageMut;
3434
use zenoh_result::ZResult;
3535
use zenoh_transport::{multicast::TransportMulticast, unicast::TransportUnicast};
3636

37+
use super::dispatcher::face::Face;
38+
use super::router::Resource;
3739
use super::RoutingContext;
3840

3941
pub mod downsampling;
@@ -151,6 +153,18 @@ impl InterceptorsChain {
151153
version,
152154
}
153155
}
156+
157+
pub(crate) fn intercept_with_face(
158+
&self,
159+
ctx: &mut RoutingContext<NetworkMessageMut>,
160+
face: &Face,
161+
prefix: &Resource,
162+
) -> bool {
163+
let cache_guard = prefix.get_ingress_cache(face, self);
164+
let cache = cache_guard.as_ref().and_then(|c| c.get_ref().as_ref());
165+
166+
self.intercept(ctx, cache)
167+
}
154168
}
155169

156170
impl InterceptorTrait for InterceptorsChain {

0 commit comments

Comments
 (0)