Skip to content
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

Stop publication cache task on disconnected channels #1676

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 41 additions & 37 deletions zenoh-ext/src/publication_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,34 +260,53 @@ impl PublicationCache {
tokio::select! {
// on publication received by the local subscriber, store it
sample = sub_recv.recv_async() => {
if let Ok(sample) = sample {
let queryable_key_expr: KeyExpr<'_> = if let Some(prefix) = &queryable_prefix {
prefix.join(&sample.key_expr()).unwrap().into()
} else {
sample.key_expr().clone()
};

if let Some(queue) = cache.get_mut(queryable_key_expr.as_keyexpr()) {
if queue.len() >= history {
queue.pop_front();
}
queue.push_back(sample);
} else if cache.len() >= limit {
tracing::error!("PublicationCache on {}: resource_limit exceeded - can't cache publication for a new resource",
pub_key_expr);
} else {
let mut queue: VecDeque<Sample> = VecDeque::new();
queue.push_back(sample);
cache.insert(queryable_key_expr.into(), queue);
let Ok(sample) = sample else {
return;
};

let queryable_key_expr: KeyExpr<'_> = if let Some(prefix) = &queryable_prefix {
prefix.join(&sample.key_expr()).unwrap().into()
} else {
sample.key_expr().clone()
};

if let Some(queue) = cache.get_mut(queryable_key_expr.as_keyexpr()) {
if queue.len() >= history {
queue.pop_front();
}
queue.push_back(sample);
} else if cache.len() >= limit {
tracing::error!("PublicationCache on {}: resource_limit exceeded - can't cache publication for a new resource",
pub_key_expr);
} else {
let mut queue: VecDeque<Sample> = VecDeque::new();
queue.push_back(sample);
cache.insert(queryable_key_expr.into(), queue);
}
},

// on query, reply with cached content
query = quer_recv.recv_async() => {
if let Ok(query) = query {
if !query.key_expr().as_str().contains('*') {
if let Some(queue) = cache.get(query.key_expr().as_keyexpr()) {
let Ok(query) = query else {
return;
};

if !query.key_expr().as_str().contains('*') {
if let Some(queue) = cache.get(query.key_expr().as_keyexpr()) {
for sample in queue {
if let (Some(Ok(time_range)), Some(timestamp)) = (query.parameters().time_range(), sample.timestamp()) {
if !time_range.contains(timestamp.get_time().to_system_time()){
continue;
}
}
if let Err(e) = query.reply_sample(sample.clone()).await {
tracing::warn!("Error replying to query: {}", e);
}
}
}
} else {
for (key_expr, queue) in cache.iter() {
if query.key_expr().intersects(unsafe{ keyexpr::from_str_unchecked(key_expr) }) {
for sample in queue {
if let (Some(Ok(time_range)), Some(timestamp)) = (query.parameters().time_range(), sample.timestamp()) {
if !time_range.contains(timestamp.get_time().to_system_time()){
Expand All @@ -299,21 +318,6 @@ impl PublicationCache {
}
}
}
} else {
for (key_expr, queue) in cache.iter() {
if query.key_expr().intersects(unsafe{ keyexpr::from_str_unchecked(key_expr) }) {
for sample in queue {
if let (Some(Ok(time_range)), Some(timestamp)) = (query.parameters().time_range(), sample.timestamp()) {
if !time_range.contains(timestamp.get_time().to_system_time()){
continue;
}
}
if let Err(e) = query.reply_sample(sample.clone()).await {
tracing::warn!("Error replying to query: {}", e);
}
}
}
}
}
}
},
Expand Down
Loading