1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use futures::future::{FutureExt, LocalBoxFuture};
use futures::stream::StreamExt;

use crate::communicator::{Communicator, CoordNumOf, RoundNumOf};
use crate::event::ShutdownEventFor;
use crate::state::State;

use super::commits::Commits;
use super::state_keeper::EventStream;

/// A `Node` that is being [`shut_down`][crate::Node::shut_down].
pub trait Shutdown {
    type State: State;
    type Communicator: Communicator;

    /// Polls the node's event stream, driving the shutdown to conclusion.
    fn poll_shutdown(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<ShutdownEventFor<Self>>;
}

pub struct DefaultShutdown<S, C>
where
    S: State<LogEntry = <C as Communicator>::LogEntry, Node = <C as Communicator>::Node>,
    C: Communicator,
{
    trigger: LocalBoxFuture<'static, ()>,
    events: EventStream<S, RoundNumOf<C>, CoordNumOf<C>>,
    commits: Commits,
}

impl<S, C> DefaultShutdown<S, C>
where
    S: State<LogEntry = <C as Communicator>::LogEntry, Node = <C as Communicator>::Node>,
    C: Communicator,
{
    pub(crate) fn new(
        trigger: LocalBoxFuture<'static, ()>,
        events: EventStream<S, RoundNumOf<C>, CoordNumOf<C>>,
        commits: Commits,
    ) -> Self {
        Self {
            trigger,
            events,
            commits,
        }
    }
}

impl<S, C> super::Shutdown for DefaultShutdown<S, C>
where
    S: State<LogEntry = <C as Communicator>::LogEntry, Node = <C as Communicator>::Node>,
    C: Communicator,
{
    type State = S;
    type Communicator = C;

    fn poll_shutdown(
        &mut self,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<ShutdownEventFor<Self>> {
        let _ = self.trigger.poll_unpin(cx);

        while let std::task::Poll::Ready(Some(())) = self.commits.poll_next(cx) {
            // keep going
        }

        self.events
            .poll_next_unpin(cx)
            .map(|e| e.expect("Event stream ended"))
    }
}