Skip to content

Commit

Permalink
Try to share XenStore instance.
Browse files Browse the repository at this point in the history
  • Loading branch information
TSnake41 committed Jul 31, 2023
1 parent 6e56939 commit 7cfad2a
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions plugins/xcp-metrics-plugin-xenstored/src/watch_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ async fn watch_task(
mut watch_channel: mpsc::UnboundedReceiver<String>,
mut unwatch_channel: mpsc::UnboundedReceiver<String>,
) {
let xs = Arc::new(Xs::new(XsOpenFlags::ReadOnly).unwrap());

let watch_task = task::spawn((|| {
let cache = cache.clone();
let xs = xs.clone();

async move {
let xs = Xs::new(XsOpenFlags::ReadOnly).unwrap();

let mut stream = xs.get_stream().unwrap();

while let Some(entry) = stream.next().await {
Expand All @@ -44,24 +45,28 @@ async fn watch_task(
}
})());

let watch_channel_task = task::spawn(async move {
let xs = Xs::new(XsOpenFlags::ReadOnly).unwrap();
let watch_channel_task = task::spawn((|| {
let xs = xs.clone();

while let Some(to_watch) = watch_channel.recv().await {
println!("Watching {to_watch}");
xs.watch(&to_watch, "xcp-metrics-xenstored").ok();
async move {
while let Some(to_watch) = watch_channel.recv().await {
println!("Watching {to_watch}");
xs.watch(&to_watch, "xcp-metrics-xenstored").ok();
}
}
});
})());

let unwatch_channel_task = task::spawn(async move {
let xs = Xs::new(XsOpenFlags::ReadOnly).unwrap();
let unwatch_channel_task = task::spawn((|| {
let xs = xs.clone();

while let Some(to_unwatch) = unwatch_channel.recv().await {
println!("Unwatching {to_unwatch}");
xs.unwatch(&to_unwatch, "xcp-metrics-xenstored").ok();
cache.remove(&to_unwatch);
async move {
while let Some(to_unwatch) = unwatch_channel.recv().await {
println!("Unwatching {to_unwatch}");
xs.unwatch(&to_unwatch, "xcp-metrics-xenstored").ok();
cache.remove(&to_unwatch);
}
}
});
})());

select! {
_ = watch_task => {},
Expand Down

0 comments on commit 7cfad2a

Please sign in to comment.