-
Notifications
You must be signed in to change notification settings - Fork 174
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add unstable background method to subscriber/queryable/matching listeners #1106
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cf1c653
feat: remove `alive` flag in session/undeclarable objects
wyfo b0ba472
fix: add missing `Publisher` refactoring
wyfo 05a56ce
fix: add comments
wyfo 3d4375e
fix: fix session clones being dropped
wyfo f101b44
fix: add comment
wyfo 631e7dd
feat: add unstable `background` method to subscriber/queryable/matchi…
wyfo 2720fe8
fix: remove dead code
wyfo 6e29422
fix: fix implementation
wyfo 94bd2e9
fix: rework everything
wyfo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,16 +13,17 @@ | |
// | ||
|
||
use std::{ | ||
collections::HashSet, | ||
convert::TryFrom, | ||
fmt, | ||
future::{IntoFuture, Ready}, | ||
pin::Pin, | ||
sync::{Arc, Mutex}, | ||
task::{Context, Poll}, | ||
}; | ||
|
||
use futures::Sink; | ||
use zenoh_core::{zread, Resolvable, Resolve, Wait}; | ||
use zenoh_keyexpr::keyexpr; | ||
use zenoh_protocol::{ | ||
core::CongestionControl, | ||
network::{push::ext, Push}, | ||
|
@@ -134,6 +135,8 @@ pub struct Publisher<'a> { | |
pub(crate) priority: Priority, | ||
pub(crate) is_express: bool, | ||
pub(crate) destination: Locality, | ||
pub(crate) matching_listeners: Arc<Mutex<HashSet<Id>>>, | ||
pub(crate) undeclare_on_drop: bool, | ||
} | ||
|
||
impl<'a> Publisher<'a> { | ||
|
@@ -160,28 +163,33 @@ impl<'a> Publisher<'a> { | |
} | ||
} | ||
|
||
#[inline] | ||
pub fn key_expr(&self) -> &KeyExpr<'a> { | ||
&self.key_expr | ||
} | ||
|
||
#[inline] | ||
/// Get the `congestion_control` applied when routing the data. | ||
pub fn congestion_control(&self) -> CongestionControl { | ||
self.congestion_control | ||
} | ||
|
||
/// Change the `congestion_control` to apply when routing the data. | ||
#[inline] | ||
pub fn set_congestion_control(&mut self, congestion_control: CongestionControl) { | ||
self.congestion_control = congestion_control; | ||
} | ||
|
||
/// Change the priority of the written data. | ||
/// Get the priority of the written data. | ||
#[inline] | ||
pub fn set_priority(&mut self, priority: Priority) { | ||
self.priority = priority; | ||
pub fn priority(&self) -> Priority { | ||
self.priority | ||
} | ||
|
||
/// Restrict the matching subscribers that will receive the published data | ||
/// to the ones that have the given [`Locality`](crate::prelude::Locality). | ||
#[zenoh_macros::unstable] | ||
/// Change the priority of the written data. | ||
#[inline] | ||
pub fn set_allowed_destination(&mut self, destination: Locality) { | ||
self.destination = destination; | ||
pub fn set_priority(&mut self, priority: Priority) { | ||
self.priority = priority; | ||
} | ||
|
||
/// Consumes the given `Publisher`, returning a thread-safe reference-counting | ||
|
@@ -350,6 +358,13 @@ impl<'a> Publisher<'a> { | |
pub fn undeclare(self) -> impl Resolve<ZResult<()>> + 'a { | ||
Undeclarable::undeclare_inner(self, ()) | ||
} | ||
|
||
fn undeclare_matching_listeners(&self) -> ZResult<()> { | ||
for id in zlock!(self.matching_listeners).drain() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code would be safer w.r.t. mutex deadlock if:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
self.session.undeclare_matches_listener_inner(id)? | ||
} | ||
Ok(()) | ||
} | ||
} | ||
|
||
/// Functions to create zenoh entities with `'static` lifetime. | ||
|
@@ -470,12 +485,12 @@ impl Resolvable for PublisherUndeclaration<'_> { | |
|
||
impl Wait for PublisherUndeclaration<'_> { | ||
fn wait(mut self) -> <Self as Resolvable>::To { | ||
let Publisher { | ||
session, id: eid, .. | ||
} = &self.publisher; | ||
session.undeclare_publisher_inner(*eid)?; | ||
self.publisher.key_expr = unsafe { keyexpr::from_str_unchecked("") }.into(); | ||
Ok(()) | ||
// set the flag first to avoid double panic if this function panic | ||
self.publisher.undeclare_on_drop = false; | ||
self.publisher.undeclare_matching_listeners()?; | ||
self.publisher | ||
.session | ||
.undeclare_publisher_inner(self.publisher.id) | ||
} | ||
} | ||
|
||
|
@@ -490,7 +505,8 @@ impl IntoFuture for PublisherUndeclaration<'_> { | |
|
||
impl Drop for Publisher<'_> { | ||
fn drop(&mut self) { | ||
if !self.key_expr.is_empty() { | ||
if self.undeclare_on_drop { | ||
let _ = self.undeclare_matching_listeners(); | ||
let _ = self.session.undeclare_publisher_inner(self.id); | ||
} | ||
} | ||
|
@@ -887,17 +903,19 @@ where | |
#[zenoh_macros::unstable] | ||
fn wait(self) -> <Self as Resolvable>::To { | ||
let (callback, receiver) = self.handler.into_handler(); | ||
self.publisher | ||
let state = self | ||
.publisher | ||
.session | ||
.declare_matches_listener_inner(&self.publisher, callback) | ||
.map(|listener_state| MatchingListener { | ||
listener: MatchingListenerInner { | ||
publisher: self.publisher, | ||
state: listener_state, | ||
alive: true, | ||
}, | ||
receiver, | ||
}) | ||
.declare_matches_listener_inner(&self.publisher, callback)?; | ||
zlock!(self.publisher.matching_listeners).insert(state.id); | ||
Ok(MatchingListener { | ||
listener: MatchingListenerInner { | ||
publisher: self.publisher, | ||
state, | ||
undeclare_on_drop: true, | ||
}, | ||
receiver, | ||
}) | ||
} | ||
} | ||
|
||
|
@@ -939,7 +957,7 @@ impl std::fmt::Debug for MatchingListenerState { | |
pub(crate) struct MatchingListenerInner<'a> { | ||
pub(crate) publisher: PublisherRef<'a>, | ||
pub(crate) state: std::sync::Arc<MatchingListenerState>, | ||
pub(crate) alive: bool, | ||
undeclare_on_drop: bool, | ||
} | ||
|
||
#[zenoh_macros::unstable] | ||
|
@@ -1007,6 +1025,14 @@ impl<'a, Receiver> MatchingListener<'a, Receiver> { | |
pub fn undeclare(self) -> MatchingListenerUndeclaration<'a> { | ||
self.listener.undeclare() | ||
} | ||
|
||
/// Make the matching listener run in background, until the publisher is undeclared. | ||
#[inline] | ||
#[zenoh_macros::unstable] | ||
pub fn background(mut self) { | ||
// The matching listener will be undeclared as part of publisher undeclaration. | ||
self.listener.undeclare_on_drop = false; | ||
} | ||
} | ||
|
||
#[zenoh_macros::unstable] | ||
|
@@ -1044,7 +1070,9 @@ impl Resolvable for MatchingListenerUndeclaration<'_> { | |
#[zenoh_macros::unstable] | ||
impl Wait for MatchingListenerUndeclaration<'_> { | ||
fn wait(mut self) -> <Self as Resolvable>::To { | ||
self.subscriber.alive = false; | ||
// set the flag first to avoid double panic if this function panic | ||
self.subscriber.undeclare_on_drop = false; | ||
zlock!(self.subscriber.publisher.matching_listeners).remove(&self.subscriber.state.id); | ||
self.subscriber | ||
.publisher | ||
.session | ||
|
@@ -1065,7 +1093,8 @@ impl IntoFuture for MatchingListenerUndeclaration<'_> { | |
#[zenoh_macros::unstable] | ||
impl Drop for MatchingListenerInner<'_> { | ||
fn drop(&mut self) { | ||
if self.alive { | ||
if self.undeclare_on_drop { | ||
zlock!(self.publisher.matching_listeners).remove(&self.state.id); | ||
let _ = self | ||
.publisher | ||
.session | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what happened with
set_allowed_destination
? Is it's removal planned?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I discussed about it with @OlivierHecart weeks ago, this thing is currently broken (the modification is not transmitted to the
PublisherState
stored in session, only the creation destination matter).There was maybe another reason, but I forgot. @OlivierHecart do you remember?