Skip to main content

coven_sync/
network_follow.rs

1//! What the store does as its watch of the device's network hears a change
2//! (§6.7): each usable network that appears, or that the device moves to,
3//! is a network report to the store's retry waits, and the network as the
4//! watch now says it is is what coven's views show (§4). Each owner that
5//! holds transfers for a metered network watches it itself (§17.9).
6
7use crate::RetryWaits;
8use coven_database::StoreDatabase;
9use coven_foundation::network_watch::NetworkWatch;
10use std::sync::Arc;
11use tokio::sync::watch;
12
13/// Follows one open store's network watch from opening to closing: built
14/// as the store opens, already showing what the platform says, and closed
15/// before the database. It keeps the watch, whose platform registration
16/// ends with it.
17pub struct NetworkFollow {
18    _watch: NetworkWatch,
19    stop: watch::Sender<bool>,
20    task: tokio::sync::Mutex<Option<coven_foundation::task::OwnedTask>>,
21    /// Each time the follower has reported every change it was told of
22    /// and waits.
23    #[cfg(any(test, feature = "test-utils"))]
24    parks: crate::Parks,
25}
26
27impl NetworkFollow {
28    /// Show the network as the watch says it is now, report it if it is
29    /// usable, so a wait saved before the store opened ends now, then follow
30    /// each change.
31    pub fn start(watched: NetworkWatch, waits: Arc<RetryWaits>, database: StoreDatabase) -> Self {
32        let mut network = watched.view();
33        let first = network.read();
34        database.record_network(first);
35        if first.usable == Some(true) {
36            waits.report_network();
37        }
38        let (stop, mut stopping) = watch::channel(false);
39        let bugs = database.bugs().clone();
40        let parks = crate::Parks::new();
41        #[cfg(any(test, feature = "test-utils"))]
42        let follower_parks = parks.clone();
43        // A panic goes to the store's bug sink, and the network stays as
44        // last shown until the store closes.
45        let follow = async move {
46            let _running = parks.running();
47            let mut joined = first.joined;
48            loop {
49                tokio::select! {
50                    biased;
51                    never = parks.polling() => match never {},
52                    _ = stopping.changed() => break,
53                    () = network.changed() => {}
54                    never = parks.parked() => match never {},
55                }
56                let state = network.read();
57                database.record_network(state);
58                // Coalesced changes count once: each report ends the same
59                // waits (§6.7).
60                if state.joined != joined {
61                    joined = state.joined;
62                    waits.report_network();
63                }
64            }
65        };
66        let task =
67            coven_foundation::task::spawn_owned("the network follower", bugs, follow, |_| async {});
68        Self {
69            _watch: watched,
70            stop,
71            task: tokio::sync::Mutex::new(Some(task)),
72            #[cfg(any(test, feature = "test-utils"))]
73            parks: follower_parks,
74        }
75    }
76
77    /// Each time the follower has reported every network change it was
78    /// told of, and waits for the next.
79    #[cfg(any(test, feature = "test-utils"))]
80    pub fn parked_from_now(&self) -> impl std::future::Future<Output = ()> + Send + 'static {
81        self.parks.parked_from_now()
82    }
83
84    /// Stop following; nothing reports after this returns.
85    pub async fn close(&self) {
86        self.stop.send_replace(true);
87        if let Some(task) = self.task.lock().await.take() {
88            task.ended().await;
89        }
90    }
91}
92
93#[cfg(test)]
94#[path = "network_follow_tests.rs"]
95mod tests;